@memlab/cli
v2.0.7
Published
command line interface for memlab
Readme
@memlab/cli
The memlab command-line interface: every memlab command, its options, and
the dispatcher that runs them. It provides the memlab binary.
Most people install the CLI through the
memlab package, which bundles it with
the rest of memlab:
npm install -g memlabInstalling @memlab/cli directly gives you the same memlab command.
Find memory leaks in a web page
Write a test scenario:
the page to load, the interaction you suspect leaks memory, and how to undo
it. page is a Puppeteer Page:
// test-google-maps.js
function url() {
return 'https://www.google.com/maps/@37.386427,-122.0428214,11z';
}
async function action(page) {
await page.click('text/Hotels');
}
async function back(page) {
await page.click('[aria-label="Close"]');
}
module.exports = {action, back, url};Then run it:
memlab run --scenario test-google-maps.jsmemlab opens the page in Chromium, takes a heap snapshot after each step, and prints one representative retainer trace per cluster of leaked objects. A retainer trace is the chain of references from a GC root that keeps the leaked object alive.
Commands
Find leaks
| Command | What it does |
| --- | --- |
| memlab run --scenario <file> | Run a scenario in the browser and find memory leaks |
| memlab find-leaks | Find leaks in heap snapshots you already have (--baseline, --target, --final, or --snapshot-dir) |
| memlab diff-leaks | Find leaks that are new in a test run compared to a control run |
| memlab trace --node-id <id> | Print the retainer trace of one heap object |
Analyze a heap
| Command | What it does |
| --- | --- |
| memlab analyze <analysis> | Run a heap analysis, for example string, detached-DOM, or unbound-object |
| memlab heap --snapshot <file> | Explore one heap snapshot interactively |
| memlab view-heap --snapshot <file> | View one heap snapshot in an interactive terminal UI |
| memlab anonymize --snapshot <file> --output <file> | Remove user data from a heap snapshot so it can be shared |
| memlab snapshot-header --snapshot <file> | Print node and edge counts and the memory needed to analyze a heap snapshot, without parsing it |
| memlab class-histogram --snapshot <file> | Count objects and their self size per class (--top, --name-pattern) |
| memlab class-diff --baseline <file> --target <file> | Compare objects and self size per class between two heap snapshots, and count objects allocated after the baseline |
| memlab class-trend --snapshot-dir <dir> | Track objects per class across a series of snapshots and list the classes that grew at every step |
| memlab heap-stats --snapshot <file> | Split a heap snapshot into code, strings, JS arrays, system objects and native memory, and into loaded code vs memory the program allocated |
| memlab sliced-strings --snapshot <file> | Find large strings kept alive only by short substrings of them |
| memlab weakref-census --snapshot <file> | Count WeakRefs, live vs empty, by what holds them; flags collections that keep entries after their target is gone |
| memlab script-census --snapshot <file> | Report the source text of loaded scripts kept in the heap, and scripts kept in memory more than once |
| memlab find-objects --snapshot <file> | List objects by class name, node type, retained size or detached state, largest first, with their ids for memlab trace |
| memlab dom-audit --snapshot <file> | Count the DOM elements attached to the document, by tag and by repeated element (list virtualization candidates) |
| memlab native-contexts --snapshot <file> | Split a heap snapshot by JavaScript realm (page, iframes, detached windows, vm contexts) and show what each detached one holds |
Browser runs
| Command | What it does |
| --- | --- |
| memlab snapshot --scenario <file> | Run a scenario and take heap snapshots, without finding leaks |
| memlab warmup / memlab warmup-and-snapshot | Warm up the app's server before taking snapshots |
| memlab measure --scenario <file> | Run a scenario many times to tell real heap growth from run-to-run noise |
Other
| Command | What it does |
| --- | --- |
| memlab help [command] | List all commands, or print help for one |
| memlab version | Show the versions of all installed memlab packages |
| memlab get-default-work-dir | Print the directory where results are saved by default |
| memlab reset | Reset and re-create memlab's working directories |
Common options:
--work-dir <dir>: save the run's snapshots, screenshots, and results here.--headful: show the browser window (headless by default).--device <name>: emulate a device, for example a phone.--chromium-binary <path>: use a specific Chromium build.--silent,--verbose: change how much is printed.
Every command and option is listed in the CLI reference.
Use the CLI from code
run() parses process.argv and dispatches to the matching command, the same
way the memlab binary does:
const {run} = require('@memlab/cli');
run();To drive memlab from a program, the
@memlab/api package is usually
a better fit: it takes options as objects and returns results instead of
printing them.
Add your own heap analysis
memlab analyze loads analyses written as plugins. See
@memlab/heap-analysis
for how to write one, then pass it with --analysis-plugin.
