Find the action that grows memory#
A leak needs an action you can repeat. Before taking snapshots, watch memory while repeating candidates:
- Open DevTools' Performance monitor from the Command Menu. It shows JS heap size and DOM node count live.
- Repeat one action ten times, such as opening and closing a dialog or switching between two tabs of the app.
- If the DOM node count or heap size climbs and stays up after the action ends, that action is the candidate.
Chrome DevTools: Fix memory problems
Take three snapshots#
The three-snapshot technique separates the leak from memory that was allocated once and kept on purpose.
- Load the page and do the action once, so caches and lazy modules are warm.
- Snapshot 1 in the Memory panel.
- Do the action N times, for example ten.
- Snapshot 2.
- Do the action N more times.
- Snapshot 3.
Chrome runs garbage collection before each heap snapshot, so the snapshots hold only objects still reachable. In Snapshot 3, choose the summary view's filter for objects allocated between Snapshot 1 and Snapshot 2. What is still alive from that window, after N more repetitions, is what each repetition left behind. Sort by count, and look for a count that is a multiple of N. Chrome DevTools: Record heap snapshots
Read the retainers before you send it#
Select one leaked object and read the Retainers pane. It shows the chain of references that keeps the object alive, ending at a global, a closure or a DOM listener. That chain is usually the bug: an event listener added on open and never removed, a cache keyed by a component, or a closure held by a timer.
Put the chain in the report as text. A maintainer can read Window > listeners > resize > handler (Drawer.tsx) > context > drawerElement without loading a file.
The snapshot contains the page's data#
A heap snapshot is a copy of every reachable JavaScript object, including strings. In our Chromium fixture, a string we stored in a page variable, a fictional email address, appeared verbatim in the snapshot file, and a nearly empty page produced a 4.1 MB file. A snapshot from a real session can hold tokens, API responses and personal data.
- Take snapshots from a test account with synthetic data.
- Send the retainer chain and object counts as text first. Send the
.heapsnapshotfile only if the maintainer needs it, through a channel with access control. - The maintainer can load the file into their own Memory panel to browse it.
Worked example: a chart that never let go#
This example is fictional. A tester reports that a dashboard tab uses 2 GB after a day. The developer asks for three snapshots around switching between two dashboard tabs, ten switches each.
The comparison filter in Snapshot 3 shows 20 ResizeObserver instances and 20 detached canvas elements allocated in the first window. Twenty is two charts times ten switches. The retainer chain for one canvas ends at a ResizeObserver callback in the chart wrapper. The wrapper created an observer on mount and never called disconnect(). The report named the action, the count per switch and the retainer chain. The fix was one line in the unmount path, and the developer reran the same three snapshots to show the count stayed at zero.
Questions#
Do I need three snapshots, or will two do?
Two show growth, but include objects created once and kept on purpose, such as a cache filled on first use. The third snapshot confirms that the objects from the middle window survive further repetitions.
Does this work in Firefox?
Firefox's Memory tool takes heap snapshots and can compare two of them. Its views differ from Chrome's, so describe what you saw in terms of the objects and counts rather than panel names.
The heap is stable but the tab still grows. What else?
Memory outside the JavaScript heap, such as images, canvases, media and the GPU, does not appear in full in a heap snapshot. Chrome's Task Manager shows the tab's whole memory footprint. Report both numbers.
Return to reproducing what someone else saw for the other layers a replay needs.