memlab find-leaks
Find memory leaks in heap snapshots
There are three ways to specify inputs for the memlab find-leaks command:
--baseline,--target,--finalspecifies each heap snapshot input individually;--snapshot-dirspecifies the directory that contains all three heap snapshot files (without a manifest, MemLab will assign baseline, target, and final in file name order:s{n}.heapsnapshotnames by number, other names alphabetically; if the two naming styles are mixed, the order may not follow the numbers);--work-dirspecifies the output working directory of thememlab runor thememlab snapshotcommand;
Please only use one of the three ways to specify the input.
You can also manually take heap snapshots in Chrome Devtools, save them to disk. Then process them using this command with the CLI flags (either option 1 or option 2 mentioned above).
# check memory leaks in the default working directory generated by
# memlab run (without setting the --work-dir option)
memlab find-leaks
Examples
# specify the baseline, target, and final heap snapshot file path separately
memlab find-leaks --baseline /tmp/baseline.heapsnapshot --target /tmp/target.heapsnapshot --final /tmp/final.heapsnapshot
# specifies the directory that contains all three heap snapshot files
memlab find-leaks --snapshot-dir /dir/containing/heapsnapshot/files/
# specifies the output working directory of the `memlab run` or the `memlab snapshot` command
memlab find-leaks --work-dir /memlab/working/dir/generated/by/memlab/
Example output
$ memlab find-leaks --work-dir /tmp/memlab-example/leak
Memory usage across all steps:
2.0
1.7
1.5 ██ ██
1.2 ██ ██
1.0 ██ ██
0.7 ██ ██
0.5 ██ ██ ██
0.2 ██ ██ ██
0.0 ██ ██ ██
1 2 3
MemLab found 1 leak(s)
Number of clusters loaded: 0
--Similar leaks in this run: 386--
--Retained size of leaked objects: 45.3KB--
[Window / http://localhost:3000] (native) @33471 [7.6KB]
--global_object (shortcut)---> [Window [JSGlobalObject] / http://localhost:3000] (object) @33803 [46.1KB]
--openedDialogs (property)---> [Set] (object) @47967 [76 bytes]
--table (internal)---> [<array>] (array) @50805 [60 bytes]
--5 (internal)---> [Detached <div class="settings"> (native) @65915 [45.3KB]
--5 (element)---> [Detached <div> (native) @71885 [45KB]
--7 (element)---> [Detached <button id="close-settings"> (native) @72365 [812 bytes]
--9 (element)---> [Detached <div> (native) @72363 [184 bytes]
--4 (element)---> [Detached Text] (native) @15810 [80 bytes]How to read the output
memlab find-leaks runs the leak detection of memlab run again on snapshots that were already taken, here the ones in the --work-dir of an earlier memlab run.
The memory chart has one bar per step, showing the JS heap size in MB. Here the heap doubles when the dialog opens (step 2) and does not drop when it closes (step 3). Memory that closing the dialog should have freed is still in use.
MemLab found 1 leak(s) counts the groups of leaked objects. A leaked object is one that was allocated during the target step and is still alive in the final snapshot, and that memlab's leak filter flags. By default, that means detached DOM elements, unmounted React Fiber nodes, and objects with a large retained size.
Each leak is printed as a retainer trace: the chain of references that keeps the leaked objects alive, read top to bottom from a GC root to the leaked object.
- Each object line is
[name] (type) @id [retained size]. The retained size is how much memory would be freed if nothing else referenced that object. - Each arrow is
--name (type)--->, the reference from the line above to the line below.propertyis a named property,elementan array index (or a child DOM node),internalan engine field, andvariablea variable captured by a closure. Similar leaks in this run: 386is how many leaked objects have a trace like this one. memlab groups them and prints one representative.Retained size of leaked objectsis their total retained size.
In this example, closing the settings dialog removes its <div class="settings"> from the page, but the global openedDialogs Set still holds it, so the detached element and its 240 rows stay in memory. The fix is to delete the element from the Set when the dialog closes. Use the @ids with memlab trace or memlab view-heap to look closer.
Options
--baseline: set file path of the baseline heap snapshot--target: set file path of the target heap snapshot--final: set file path of the final heap snapshot--snapshot-dir: set directory path containing all heap snapshots under analysis--engine: set the JavaScript engine (default to V8)--leak-filter: specify a definition JS file for leak filter--trace-object-size-above: objects with retained size (bytes) bigger than the threshold will be considered as leaks--ignore-leak-cluster-size-below: ignore memory leaks with aggregated retained size smaller than the threshold (in bytes)--trace-all-objects: dump retainer trace for all allocated objects (ignore the leak filter), available option modes: --trace-all-objects=selected-js-objects, --trace-all-objects=default--save-trace-as-unclassified-cluster: dump each retainer trace as an unclassified trace cluster--ml-clustering: use machine learning algorithms for clustering leak traces (by default, traces are clustered by heuristics)--ml-linkage-max-dist: set linkage max distance value for clustering. The value should be between [0, 1] inclusive.--ml-clustering-max-df: set percentage based max document frequency for limiting the terms that appear too often--clean-up-snapshot: clean up heap snapshots after running--work-dir: set the working directory of the current run--heap-parser-dict-fast-store-size: the size threshold for switching from fast store to slower store in the heap snapshot parser. The default value is 5,000,000. If you get theFATAL ERROR: invalid table size Allocation failed - JavaScript heap out of memoryerror, try to decrease the threshold here
The universal options (--help, --verbose, ...) work with this command too.