Skip to content

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

Key points

  • Builds often just need a higher limit.
  • Servers that run for hours and then crash are leaking.
  • Set the limit below the container's memory.
  • Heap snapshots compare what grew.

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.

Common causes

  • Large builds: webpack, TypeScript or Next.js compiling large projects.
  • Leaks: caches with no limit, listeners never removed, closures holding big data.
  • Loading whole files or query results into memory.
  • Unbounded in-memory queues.

Diagnose it

Decide first whether it is a growth pattern or a single large allocation.

bash
# 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.

Frequently asked questions

What should max-old-space-size be?

For a container, about 75 percent of its memory limit.

Why does the error happen only in CI?

CI machines often have less memory than laptops, and builds are memory-hungry.

Will more memory fix a leak?

It only delays the crash.

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.