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
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.
// 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();# 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
- Take a snapshot after warm-up.
- Send realistic traffic for 10 to 15 minutes.
- Take a second snapshot.
- In Chrome devtools, open the second one in Comparison view against the first, sorted by Size Delta.
- Pick the constructor that grew most and look at its Retainers: the chain of references keeping it alive leads to the code at fault.
| Cause | Example | Fix |
|---|---|---|
| Unbounded cache | A Map keyed by user or URL that is never pruned | LRU with a size limit, or TTLs |
| Event listeners | emitter.on() per request, never removed | Use once(), or remove in cleanup |
| Global arrays | Pushing request data for "debugging" | Remove, or cap the length |
| Timers | setInterval created per connection, never cleared | clearInterval on close |
| Closures | Callbacks capturing large objects stored long-term | Store 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.
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.
Related reading
- JavaScript heap out of memory: how to fix it in Node.js: Fix FATAL ERROR: JavaScript heap out of memory. Set --max-old-space-size correctly, handle build-time failures and find runtime leaks with heap snapshots.
- Python memory usage: find what uses memory: Measure and reduce Python memory usage: RSS vs allocated objects, tracemalloc snapshots, finding leaks in long-running workers.
- Goroutine leaks in Go: how to find and fix them: Find and fix goroutine leaks in Go: read pprof goroutine profiles, spot blocked channels and missing context cancellation, and use goleak in your tests.
- Java memory usage: heap, metaspace and leaks: Understand Java memory usage: heap, metaspace, threads and direct memory, why RSS exceeds -Xmx, how to size for containers.
- .NET OutOfMemoryException: causes and how to fix it: Fix .NET System.OutOfMemoryException: use dotnet-counters and dotnet-gcdump, spot Large Object Heap fragmentation and set the right GC limits in containers.
- Node.js performance monitoring: Monitor Node.js apps: event loop delay, heap and garbage collection, request latency per route, outgoing calls and errors.
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.