Capturing and Comparing Heap Snapshots
Find retained objects and leak sources by diffing heap snapshots in the inspector.
Why Heap Snapshots Matter
A heap snapshot is a complete dump of every JavaScript object alive in V8 at the moment you capture it. For a Node.js backend, it is the single most precise tool for answering one question: what is still being retained, and why?
- A leaking process keeps allocating objects that never get garbage-collected because something is still holding a reference.
- One snapshot tells you what exists now; comparing two snapshots over time tells you what is growing — which is the actual leak signal.
In this lesson you will capture snapshots from a running Node service, load them into Chrome DevTools, diff them, and read the retainer chain back to the offending code.
Exposing the Inspector
To capture snapshots from a real service you first attach the V8 inspector. Start the process with --inspect so DevTools (or the inspector protocol) can connect.
node --inspect server.jsopens the inspector on127.0.0.1:9229.- Open
chrome://inspectin Chrome, click inspect on your target, then go to the Memory tab. - Never bind the inspector to a public interface in production — it grants full code execution.
The snippet below is the exact CLI invocation you would script in your start command.
// package.json scripts
{
"scripts": {
"debug": "node --inspect=127.0.0.1:9229 server.js",
"debug:brk": "node --inspect-brk server.js"
}
}All lessons in this course
- The V8 Heap, Generational GC, and Object Lifetimes
- Capturing and Comparing Heap Snapshots
- CPU Profiling and Flame Graphs for Hot Paths
- Detecting and Fixing Common Leak Patterns