Diagnostics

Memory and heap snapshots

Memory diagnostics distinguish JavaScript heap use, external allocations and resident process memory. A heap snapshot describes reachable objects and their relationships at a point in time; it is not a complete account of all process memory. process.memoryUsage returns byte counts including rss, heapTotal, heapUsed, external and arrayBuffers; arrayBuffers is included in external. RSS includes more than JavaScript heap and can vary because of native memory and allocator behavior. A retained reference, such as an unbounded Map of sessions, can prevent objects from becoming collectible. v8.writeHeapSnapshot can block the event loop and consume substantial memory; snapshots may contain secrets. Use only a small synthetic process and keep snapshots private. Do not infer a leak from a single sample or force garbage collection as a routine repair. Do not add all memoryUsage fields together: they overlap. A rising RSS alone does not identify which objects are retained. The fixtures intentionally avoid writing a real heap snapshot. Sources: https://nodejs.org/download/release/v22.16.0/docs/api/v8.html.

Formal Syntax & Structure

nodejs syntaxnodejs
process.memoryUsage(); v8.writeHeapSnapshot([filename])

Working Example

Example Snippetnodejs
import assert from 'node:assert/strict';const memory=process.memoryUsage();const fields=['rss','heapTotal','heapUsed','external','arrayBuffers'];for(const key of fields)assert.ok(Number.isFinite(memory[key])&&memory[key]>=0);console.log(fields.join(','));
Node.js Reference