Troubleshooting guide
JavaScript heap out of memory: how to fix it in Node.js
V8 reached its heap limit and could not free enough memory, so Node.js aborted. The fix differs between a build that needs more room and a server that is leaking.
3 min read10 sectionsUpdated
On this page
What it means
Node's JavaScript objects live on the V8 heap, which has a limit that depends on version and available memory. When garbage collection cannot reclaim enough, V8 prints Reached heap limit Allocation failed and the process aborts. Native buffers live outside this heap, so a process may grow without this error, too.
# Print the effective heap limit (MB)
node -e "console.log(v8.getHeapStatistics().heap_size_limit / 1048576)"
# Log each GC
node --trace-gc server.js 2>&1 | tail -20
# Write a heap snapshot automatically near the limit
node --heapsnapshot-near-heap-limit=2 server.js
# Send SIGUSR2 for a snapshot on demand
node --heapsnapshot-signal=SIGUSR2 server.js &
kill -USR2 $!Fixes
For builds, set NODE_OPTIONS=--max-old-space-size=4096 in CI. For servers, set the limit to about 75 percent of the container's memory so that native memory has space. Use streams instead of reading big files whole. Add a maximum size to every cache, using an LRU. Paginate or stream large query results.
Reading heap snapshots
Take two snapshots some minutes apart, under load, and open them in Chrome DevTools' Memory panel. Use the Comparison view and sort by size delta: constructors with a large positive delta are what is accumulating. Then follow the Retainers pane to the path that keeps those objects alive, which often ends in a module-level Map, an event emitter or a timer. Be aware that writing a snapshot pauses the process and may roughly double its memory use for a moment, so take it from an instance that is out of rotation.
Catch it automatically
Chart heap used against the limit, and alert when it keeps rising across GC cycles. APM adds release markers, so you can tell which deployment started the climb. Container restarts with code 134 are this crash.
Related reading
- PHP "Allowed memory size exhausted": how to fix it: Fix PHP Fatal error: Allowed memory size exhausted. Read the error correctly, raise memory_limit for the right SAPI and stop loading big data into arrays.
- GC overhead limit exceeded in Java: how to fix it: Fix java.lang.OutOfMemoryError: GC overhead limit exceeded. Enable GC logs, take a heap dump, find what fills the heap and size the JVM for containers.
- 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.
- php-fpm "server reached pm.max_children": how to fix it: Fix php-fpm server reached pm.max_children: calculate the right worker count from memory, find slow scripts with the slow log and choose between pm modes.
- PHP maximum execution time exceeded: how to fix it: Fix PHP Maximum execution time of 30 seconds exceeded: why external waits may not count, which timeouts line up with Nginx, and how to move long work away.
- 431 Request Header Fields Too Large: how to fix it: Fix 431 Request Header Fields Too Large: bloated cookies, Node.js header limits and localhost cookie build-up, with commands to measure real header size.
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.