Skip to content
jshacks.dev
Debugging

Compare heap snapshots to find a leak

Find a growing JavaScript leak by taking two heap snapshots in Chrome DevTools and reading the Comparison view for objects that were added and not freed.

1 min read
Summarize in AI

Memory that only grows points to a leak, but not to its cause. Comparing two snapshots shows which kinds of objects accumulate.

leak.jsJavaScript
const leaks = [];
setInterval(() => leaks.push(new Array(1000).fill("x")), 1000);

In the Memory panel:

  1. Take snapshot 1 after the page has settled.

  2. Do the action you suspect, or wait for a few seconds.

  3. Click the trash can to run garbage collection, then take snapshot 2.

  4. Select snapshot 2, switch the view from Summary to Comparison and choose snapshot 1 as the base.

Sort by the Delta columns. Rows with a large positive count or size delta are growing:

Text
Constructor    # New    # Deleted    # Delta
(array)          3         0            +3

The pattern of added objects with nothing deleted is the signature of a leak. Click an entry and read its Retainers to see what keeps it alive, here the leaks array. The method is covered in the DevTools memory guide.

The table above shows the shape, and your counts will differ. Repeat the action several times between snapshots, so a real leak stands out from one-time allocations.

Share

More snippets

Debugging

Group related logs with console.group

Indent related console output under a label with console.group and console.groupEnd, so interleaved logs from one request stay readable in Node.js 24.

1 min read
Performance

Time code with performance.now

Time a block of code with performance.now, which returns a monotonic high-resolution timestamp in milliseconds, and avoid the clock jumps Date.now can have.

1 min read
Debugging

Inspect an object with console.dir

Show nested objects beyond the default depth of two with console.dir and the depth option, instead of the [Object] placeholders console.log prints.

1 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.