Skip to content

Heap snapshot diffing

  1. Memory → Take snapshot (A)
  2. Reproduce leak N times
  3. Click Collect garbage
  4. Take snapshot (B)
  5. Select B → Summary dropdown → Comparison → compare to A

Sort by # Delta descending.

Terminal window
node --inspect examples/node/02-event-emitter-leak.js

Chrome → chrome://inspect → Memory → same A/B comparison.

Or write snapshots programmatically:

import v8 from 'node:v8';
import fs from 'node:fs';
function snap(name) {
const file = `/tmp/heap-${name}-${Date.now()}.heapsnapshot`;
v8.writeHeapSnapshot(file);
console.log('wrote', file);
}

Load .heapsnapshot files in DevTools Memory tab.

flowchart LR
  A[Snapshot A baseline] --> action[User actions]
  action --> GC[Force GC]
  GC --> B[Snapshot B]
  B --> diff[Comparison view]
Delta Action
+10,000 (string) Logging? Template cache?
+500 Detached HTMLDivElement DOM leak
+1 YourSingleton Expected — investigate children

Click growing type → Retainers panel (next lesson).