liveBook · Manning

Heap snapshot

As explained in JavaScript in Depth

A point-in-time record of objects in a managed heap, including their sizes and reference paths from garbage-collection roots.

A rising memory graph does not, by itself, identify a leak. Garbage collection can produce a saw-tooth pattern of spikes and drops; a steadier climb with regularly timed plateaus raises a different question: which objects remain reachable? A heap snapshot captures the managed heap at one moment so you can investigate that question. section 11.0 section 11.1.4

Each object has a type, a shallow size, a retained size, and reference paths from garbage-collection roots. Shallow size is the object's own cost. Retained size is what would become unreachable if that object were removed. A small closure can therefore retain a large Buffer. Follow its retainer chain backward to find what keeps the closure—and the Buffer—alive. section 11.1.4

Retained size follows dominators, not every reference. If two independent objects reference the same Buffer, removing either one leaves it reachable; neither individually retains its size. This is why sorting by retained size is useful, but interpreting the reference graph still matters. section 11.1.4

A practical leak investigation uses snapshots in sequence:

  • Capture two snapshots during steady load and compare them in Chrome DevTools. Sort the Comparison view by delta count or delta size to find accumulating object types. section 11.2.3
  • Inspect the growing types' retainer chains. Check for collections that grow without bound, uncleared timer callbacks or event listeners, and pending Promises retaining captured scopes. section 11.2.3
  • If no type stands out, use allocation sampling to find code paths allocating heavily between snapshots. A snapshot shows what remains at capture time, not the full history of allocations. section 11.1.4 section 11.2.3

An uncleared timer

An interval callback captures an `active` array and repeatedly appends records, but the interval is never cleared. Across snapshots, the record count and each record's payload arrays climb together. The retainer path leads through the growing array and its callback closure: the records are not merely being created; they remain reachable. section 11.1.4

class SessionRecord {
  constructor(id) {
    this.id = id;
    this.token = 'tok_' + id.toString(16).padStart(8, '0');
    this.payload = new Array(8).fill(id);
  }
}
 
function startSessionSweeper() {
  const active = [];                 // captured in the callback's closure
  setInterval(() => {
    for (let i = 0; i < 400; i++) active.push(new SessionRecord(active.length));
  }, 10);                            // never cleared, so `active` grows forever
}
 
startSessionSweeper();
 

The interval and captured array make the retention path concrete: each callback invocation adds records to an array that remains reachable. section 11.1.4

For Node.js, `v8.writeHeapSnapshot()` writes a `.heapsnapshot` file that DevTools can open; an Inspector connection also allows interactive capture. Diagnostic access varies by runtime: Node.js uses V8, while Bun uses JSC, so do not assume identical snapshot data or tooling everywhere. section 11.1.3 section 11.1.4

How JavaScript in Depth covers it

How deep it goes
Defines shallow size, retained size, dominators, and retainer chains, then gives a two-snapshot comparison procedure.
Examples
Uses an uncleared interval whose captured array accumulates records and payload arrays across snapshots.
What it assumes
Assumes JavaScript object and reference familiarity while defining snapshot sizes, garbage-collection roots, and retainer chains.
What you can do after
Diagnose/debug with it by comparing snapshots, identifying growing types, and following retainer chains to what keeps them alive.
How it connects
Connects garbage-collection roots and dominators to closures, timers, collections, Promises, and allocation sampling.

Also explained in

Compare how 2 books explain heap snapshot →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of JavaScript in Depth. The section links point to the parts of the book it draws on.