A framework for finding JavaScript memory leaks and analyzing heap snapshots
by facebookTypeScript
Last 12 weeks · 96 commits
4 of 6 standards met
Hi memlab devs, thanks for the great tool. I'm excited to try to integrate it into my current NodeJS app tests. I'm wondering what's current status or future road map is on supporting NodeJS memory leak detection. Currently, most test cases and demos are running in browsers detecting front-end side memory leaks. Would you plan to give more support on NodeJS?
Summary New package — a Playwright fixture that attaches memlab's leak detection to existing Playwright tests by destructuring a parameter. No scenario rewrite, no Puppeteer dependency. Motivation memlab's existing APIs (, ) drive the browser through a Puppeteer-based scenario runner. Teams already running Playwright e2e suites can't attach leak detection without rewriting their specs. This package bridges the gap by piggybacking on the user's existing page via a CDP session. Design notes Chromium only. Heap snapshots go over CDP, which Playwright only exposes for Chromium. On Firefox / WebKit the fixture becomes a no-op: the test still runs, no leak detection, a memlab-skip annotation is attached. README calls this out. Opt-in per test. Fixture setup runs only when memlab is destructured. Tests that don't use it pay no overhead 3-phase snapshot (baseline / target / final) wired to fixture lifecycle. via , no flag required. Configurable. for per-project GC tuning and custom filters. Merge semantics preserve prior fields. CI-friendly failure output. picks the hop (not the Window root), strips memlab-internal metadata from attached JSON, and heap snapshots only attach when leaks are detected (skips ~13 MB per clean test). API : Capture baseline heap snapshot / : Manual phase control : Per-test tuning : Returns : Hard assertion with triage-friendly error If neither nor is called, the fixture auto-captures missing phases and soft-asserts no leaks during teardown.
While looking through trace sampling and percentile calculations in , I noticed a few boundary edge cases in (): 1. 100th percentile returns : When , evaluates to . Since , it tries to return , which evaluates to (for example, accessing index on a length-5 array instead of the maximum element at index ). 2. Empty array returns (causing in callers): For an empty array , and it returns (). When this flows into via , produces , which causes subsequent `` Happy to submit a quick PR with unit tests if this looks good to you!
While exploring the programmatic API in , I noticed a small configuration mismatch in (). In (line 156) and (line 191), is initialized with the run's config instance: However, in (line 226), is constructed without arguments: Because is omitted, defaults its internal to the global singleton . When options like a transient working directory, custom , , or custom scenario settings are passed to , the scenario synthesizers instantiated inside receive instead of the newly created instance for this run. Proposed fix: Pass to in , matching and : I'd be happy to open a PR for this if this looks good to the maintainers!
Problem When is enabled, double-counts the representative trace of every cluster: The HAC union-find labels in point every trace at the largest index in its cluster, and exactly one trace per cluster satisfies (the representative). For that one trace, , so it is inserted by the seed and pushed again by the loop — every cluster ends up containing its representative twice. This propagates to , so and are inflated by 1 per cluster, and even a single leaked object reports . Fix Initialize each cluster list empty and let the loop add every trace exactly once. Test Added , which fails before the fix (single trace → 2) and passes after (each trace counted once). All core and heap-analysis tests pass.
Repository: facebook/memlab. Description: A framework for finding JavaScript memory leaks and analyzing heap snapshots Stars: 5029, Forks: 144. Primary language: TypeScript. Languages: TypeScript (62.8%), JavaScript (36.4%), CSS (0.4%), MDX (0.3%), HTML (0.1%). License: MIT. Homepage: https://facebook.github.io/memlab/ Topics: detector, e2e, facebook, heap, hermes, javascript, leak, memory, nodejs, perf-tools, performance, snapshot, v8. Open PRs: 2, open issues: 11. Last activity: 9h ago. Community health: 75%. Top contributors: JacksonGL, tulga1970, Drizzlecrj, mrsharpoblunto, LuciNyan, aelij, kleookku, arunsathiya, eltociear, mehmetmalli and others.