Skip to content

Troubleshooting guide

How to find a Node.js memory leak

A leak shows as memory that climbs with traffic and never comes back down, until the process is killed. Heap snapshots tell you exactly what is holding on.

3 min read9 sectionsUpdated

On this page

Key points

  • A leak is growth that survives garbage collection, not just high memory.
  • Compare two heap snapshots taken minutes apart under load.
  • Common causes: unbounded caches, listeners never removed, closures in long-lived arrays, timers.
  • Alert on memory near the limit, before the OOM kill.

Confirm it is a leak

Memory that rises after deploy and then flattens is normal warm-up. A leak looks like a staircase or a steady slope over hours, with each garbage collection reclaiming less, until the process restarts. Plot heap used and resident memory (RSS) per instance over a day, with restarts marked.

javascript
// Log memory every minute (or export as metrics)
setInterval(() => {
  const m = process.memoryUsage();
  console.log(JSON.stringify({ msg: 'memory', rss: m.rss, heapUsed: m.heapUsed, heapTotal: m.heapTotal, external: m.external }));
}, 60_000).unref();

Capture heap snapshots

bash
# Start Node with the inspector, then use Chrome at chrome://inspect → Memory → Take snapshot
node --inspect server.js

# Or write a snapshot on a signal, without the inspector (Node 12+)
node --heapsnapshot-signal=SIGUSR2 server.js
kill -USR2 <pid>      # writes Heap.<date>.heapsnapshot in the working directory

# Or from code (Node 11.13+)
node -e "require('v8').writeHeapSnapshot()"

Compare snapshots

  1. Take a snapshot after warm-up.
  2. Send realistic traffic for 10 to 15 minutes.
  3. Take a second snapshot.
  4. In Chrome devtools, open the second one in Comparison view against the first, sorted by Size Delta.
  5. Pick the constructor that grew most and look at its Retainers: the chain of references keeping it alive leads to the code at fault.

Usual suspects

CauseExampleFix
Unbounded cacheA Map keyed by user or URL that is never prunedLRU with a size limit, or TTLs
Event listenersemitter.on() per request, never removedUse once(), or remove in cleanup
Global arraysPushing request data for "debugging"Remove, or cap the length
TimerssetInterval created per connection, never clearedclearInterval on close
ClosuresCallbacks capturing large objects stored long-termStore only what is needed

Alert before it crashes

Set the heap limit deliberately (--max-old-space-size) below the container memory limit, and alert when memory stays above 80 to 90% of the limit. In Kubernetes, watch restarts and OOMKilled events. VitalRix infrastructure monitoring shows memory against limits per pod and can alert when one instance's memory is too high, and deploy markers in APM show which release started the climb.

Frequently asked questions

What does "JavaScript heap out of memory" mean?

V8 reached its heap limit and could not free enough memory. Either the process legitimately needs more (raise --max-old-space-size within the container limit) or it is leaking.

Do heap snapshots pause the process?

Yes, briefly, and they need memory roughly the size of the heap. Take them on one instance taken out of the load balancer, or in staging under replayed traffic.

Watch your own site and services with VitalRix

Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.