liveBook · Manning

V8.writeHeapSnapshot

As explained in JavaScript in Depth

A Node.js API function that writes a snapshot of the V8 managed heap to a file.

v8.writeHeapSnapshot() writes the V8-managed heap to a .heapsnapshot file. Load that file into Chrome DevTools to investigate retained objects after the process has produced the evidence. section 11.1.3 section 11.1.4

Use the API when live debugger attachment is impractical or when programmatic capture fits the diagnostic workflow:

  • Analyze the JSON-formatted file in DevTools rather than treating it as a human-readable report. section 11.1.4
  • Expect a full garbage collection before writing; the snapshot represents objects that remain retained. section 11.1.4
  • Prefer signal-triggered capture with --heapsnapshot-signal=SIGUSR2 when another terminal must initiate collection. section 11.1.4

Capturing a suspected leak

A growing array captured by an uncleared interval can retain SessionRecord objects indefinitely. Capture a snapshot, open it in DevTools, and follow the retainer chain backward from the records to the closure or container keeping them alive. section 11.1.4

How JavaScript in Depth covers it

How deep it goes
Explains file creation, viewing, triggering, and garbage-collection effects, but defers detailed snapshot schema internals.
Examples
Demonstrates direct Node.js capture, POSIX signal triggering, and diagnosing an interval closure that retains growing records.
What it assumes
Assumes Node.js runtime familiarity and basic DevTools use; supplies the capture and offline-analysis workflow.
What you can do after
Diagnose memory retention by capturing a heap file, opening it in DevTools, and tracing objects through their retainer chains.
How it connects
Connects heap snapshots with Chrome DevTools, garbage collection, retainer chains, and the Inspector Protocol.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of JavaScript in Depth. The section links point to the parts of the book it draws on.