Skip to content
tezvyn:
Menu

Heap Snapshots: Finding Node.js Memory Leaks

Source: nodejs.orgMediumHow cards are made

A heap snapshot is a photograph of your app's memory. Use it to diagnose leaks by comparing snapshots over time to see which objects grow. The big footgun: taking one freezes your app and can double memory usage, risking a crash in production.

Why it exists

Node.js is garbage-collected, but memory leaks still happen when old, unneeded objects are unintentionally kept alive by references. When memory usage grows indefinitely without returning to a baseline, you need a way to inspect the heap to find out what is being retained and why.

The mental model

Think of a heap snapshot as a detailed census of your application's memory at a single moment. It lists every object (the 'residents') and shows the chain of references (the 'relationships') keeping each one alive. By comparing two censuses taken at different times, you can spot which populations are growing out of control, pointing you to a memory leak.

How it works

The V8 engine generates a file representing the entire heap graph. You load this snapshot file into a tool like Chrome Developer Tools. The DevTools UI lets you inspect objects, see their retained size (memory freed if the object were deleted), and view the 'retainer path'—the chain of references preventing an object from being garbage collected. To find a leak, you compare two snapshots to see which object counts have increased.

When to use it

Use heap snapshots when you observe steadily increasing memory usage that doesn't go down after garbage collection. The typical workflow is to take one snapshot, perform actions you suspect are causing the leak, and then take a second snapshot to compare against the first. This is the definitive tool for post-mortem analysis of a memory leak.

When not to use it

Do not use heap snapshots for real-time monitoring. The process is extremely heavyweight. Taking a snapshot blocks the main event loop thread, sometimes for over a minute, making your application unresponsive. It also temporarily consumes significant memory to build the snapshot itself, which can cause the process to exceed its memory limit and crash. In production, only take snapshots on instances that can be safely terminated.

One canonical example

A common leak source is an event emitter where listeners are added but never removed. To debug this, you'd start your app, take a heap snapshot, then simulate activity that registers listeners. After some time, take a second snapshot. In Chrome DevTools, comparing the two would reveal a large number of listener objects retained by the event emitter, showing you exactly where the leak is.

Interview question

What is the primary reason to exercise caution when taking a heap snapshot in a production Node.js environment?

  • a.The snapshot process can temporarily consume significant memory and block the application's event loop.Correct
  • b.They provide too much granular data, making it challenging to identify the root cause of a leak quickly.
  • c.They only capture a partial view of memory, potentially missing critical leak sources.
  • d.Analyzing them requires specialized, non-standard tools that are difficult to integrate into CI/CD pipelines.
Why?

The card explicitly states that taking a snapshot 'can double memory usage, risking a crash in production' and 'blocks the main event loop thread, sometimes for over a minute.' Option C is incorrect because the card describes a snapshot as a 'detailed census' and 'entire heap graph,' implying a comprehensive view.

Just read this? Test yourself on what you have been reading.

Read the original → nodejs.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on node.js — each one lists the topics its interview covers.

See open roles