{"id":18544,"library":"memlab","title":"MemLab - JavaScript Memory Leak Detection Framework","description":"MemLab is an end-to-end testing and analysis framework for identifying JavaScript memory leaks and optimization opportunities. Version 2.0.1 is the latest stable release, actively maintained by Meta. It provides automated browser memory leak detection using Puppeteer, a heap snapshot analysis API for Node.js/Electron/Hermes, and CLI tools for memory debugging. Key differentiators: supports custom leak detectors, object-oriented heap traversing, and integrates with unit testing. MemLab goes beyond simple heap dump analysis by automatically comparing snapshots and filtering leaks with retainer traces.","status":"active","version":"2.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/facebook/memlab","tags":["javascript","facebook","memlab","memory","leak","detector","heap","snapshot","typescript"],"install":[{"cmd":"npm install memlab","lang":"bash","label":"npm"},{"cmd":"yarn add memlab","lang":"bash","label":"yarn"},{"cmd":"pnpm add memlab","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Used for browser automation in leak detection scenarios","package":"puppeteer","optional":true}],"imports":[{"note":"MemLab is ESM-only since v2; CJS require will fail.","wrong":"const memlab = require('memlab')","symbol":"memlab","correct":"import memlab from 'memlab'"},{"note":"Type-only export; must use named import.","wrong":"import IScenario from 'memlab'","symbol":"IScenario","correct":"import { IScenario } from 'memlab'"},{"note":"Named export for heap snapshot API.","wrong":"import takeHeapSnapshot from 'memlab'","symbol":"takeHeapSnapshot","correct":"import { takeHeapSnapshot } from 'memlab'"}],"quickstart":{"code":"import memlab from 'memlab';\nimport fs from 'fs';\n\n// Define a scenario as an object with url, action, back functions\nconst scenario = {\n  url: () => 'https://example.com',\n  async action(page) {\n    await page.click('button#load-more');\n  },\n  async back(page) {\n    await page.click('button#reset');\n  }\n};\n\nasync function runLeakDetection() {\n  // Run leak detection with default options\n  const result = await memlab.run(scenario);\n  console.log(`Leaks found: ${result.leaks.length}`);\n  for (const leak of result.leaks) {\n    console.log(`Retained size: ${leak.retainedSize}`);\n  }\n}\n\nrunLeakDetection().catch(console.error);","lang":"typescript","description":"Demonstrates using MemLab's programmatic API to define a scenario and run browser memory leak detection."},"warnings":[{"fix":"Use import syntax instead of require().","message":"MemLab v2 drops CommonJS support; only ESM imports work.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Use '--runner puppeteer' instead of '--browser puppeteer'.","message":"The CLI 'memlab run' command changed its argument from --browser to --runner in v2.","severity":"breaking","affected_versions":">=2.0.0 <2.0.1"},{"fix":"Define scenario as {url, action, back} instead.","message":"Function 'createBrowserlessScenario' is deprecated in v2. Use the object-based scenario format.","severity":"deprecated","affected_versions":">=2.0.0"},{"fix":"Monitor disk usage; consider using --max-snapshots option to limit files.","message":"Heap snapshot files can be very large (multiple GB). Ensure sufficient disk space.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Set headless: false in Puppeteer launch options within scenario.","message":"MemLab runs Puppeteer in headless mode by default; some sites may require full browser.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure the website serves non-minified code or source maps for readable traces.","message":"Retainer traces may be unavailable if minified code is served; use source maps.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Replace const memlab = require('memlab') with import memlab from 'memlab'.","cause":"Using require() instead of import for ESM-only package.","error":"TypeError: memlab.run is not a function"},{"fix":"Run npm install --save-dev puppeteer to install it.","cause":"Puppeteer is an optional peer dependency not installed.","error":"Error: Cannot find module 'puppeteer'"},{"fix":"Define an async 'back' function that returns to the pre-action state.","cause":"Missing or invalid 'back' function in scenario object.","error":"Error: The 'back' action must be a function"},{"fix":"Override work directory with memlab.setWorkDir('/custom/path') or run with sudo.","cause":"MemLab uses /tmp for snapshot storage; may lack permissions.","error":"Error: EACCES: permission denied, mkdir '/tmp/memlab'"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}