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.
Memory that only grows points to a leak, but not to its cause. Comparing two snapshots shows which kinds of objects accumulate.
const leaks = [];
setInterval(() => leaks.push(new Array(1000).fill("x")), 1000);In the Memory panel:
Take snapshot 1 after the page has settled.
Do the action you suspect, or wait for a few seconds.
Click the trash can to run garbage collection, then take snapshot 2.
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:
Constructor # New # Deleted # Delta
(array) 3 0 +3The 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.