$ show facebook/memlab
Memlab is a comprehensive JavaScript memory leak detection and heap analysis framework developed by Meta. It provides a CLI, a programmatic API, and an MCP (Model Context Protocol) server for AI-assisted debugging. The system captures heap snapshots from Chromium (via Puppeteer), Node.js, Electron.js, and Hermes, then parses and analyzes them to identify memory leaks, optimization opportunities, and retention patterns. It includes specialized detectors for detached DOM elements, React component leaks, event listener accumulation, duplicate strings, and global variable bloat. The architecture is a monorepo with packages for core parsing and algorithms, E2E browser automation, heap analysis plugins, a public API, a CLI, an in-browser visualizer (MemLens), and an MCP server that exposes over 100 analysis tools. Key data flows involve warmup, browser interaction, snapshot capture, parsing, leak filtering, trace clustering (heuristic and ML-based), and report generation. External dependencies include Puppeteer, Chromium, and the MCP SDK.
The LLM identified 5 files worth highlighting for reuse:
A sharded Set implementation for storing large numbers of numeric node IDs efficiently. Uses multiple internal Set instances to avoid V8's single-set performance degradation at high cardinalities. Provides add, has, delete, and iteration operations. Reusable in any context requiring large numeric ID tracking, such as graph traversal or deduplication.
Abstract base class for leak filter rules implementing the chain-of-responsibility pattern. Each rule can decide to filter (remove) a leak object, keep it, or defer to the next rule. Provides a clean interface for adding custom leak classification logic. Reusable in any system that needs a composable, ordered decision pipeline.
Provides WeakMapPlus, a drop-in replacement for WeakMap that falls back to a regular Map when WeakMap is unavailable (e.g., in certain sandboxed environments). Includes native WeakRef detection, periodic cleanup for the fallback, and a WeakSet variant. Demonstrates careful handling of memory safety across different JavaScript runtimes.
Implements a wall-clock budget for heap analysis operations. Provides a tick() function that throws ScanTimeoutError when the budget is exceeded, allowing long-running heap walks to be interrupted gracefully. Includes progress reporting via MCP notifications. Reusable in any system that needs bounded execution time for iterative algorithms.
Provides a withSnapshotAt() helper that temporarily loads a heap snapshot from disk, executes a callback, and then releases it. This avoids keeping multiple large snapshots resident simultaneously. Includes error handling and cleanup guarantees. Reusable in any application that needs to process large files one at a time without exhausting memory.