GrepBoard/Manuals
605 manuals ·

Node.js --max-old-space-size: Fix JavaScript heap out of memory

·Tested on Node.js 22.23.4 (node:22-slim Docker · 512 MB, 1 GB, 4 GB containers) · 2026-10·Test scope ↓·5 steps

JavaScript heap out of memory means Node hit the V8 heap limit. Read heap_size_limit, then set --max-old-space-size below the container memory limit.

output
FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory

Node.js used memory up to the V8 heap limit and could not get more, so it aborted (exit code 134). Before raising the limit, read the current one with v8.getHeapStatistics() and work out whether the app genuinely needs more or is leaking. When you raise it, keep --max-old-space-size below the memory the process can actually use (the container limit).

[1] Check the current limit. heap_size_limit is the whole heap V8 may use.

console
$ node -e 'const v8=require("v8");console.log("heap_size_limit MiB =", Math.round(v8.getHeapStatistics().heap_size_limit/1048576))'

The same command in containers with different memory limits (Node.js 22.23.4):

Container --memoryDefault heap_size_limit--max-old-space-size=256
512 MB259 MiB259 MiB
1 GB524 MiB268 MiB
4 GB2096 MiB304 MiB

On this release the default followed the container limit (roughly half). The official docs do not give the formula, so re-check with this command after upgrading Node or moving hosts. --max-old-space-size sets only the old space, as the name says, so heap_size_limit comes out a little higher.

[2] Find out why it filled up. The Last few GCs block printed just before the crash is the clue:

output
<--- Last few GCs --->[46:0x41ffb000]     1297 ms: Mark-Compact (reduce) 63.7 (65.3) -> 63.6 (66.3) MB, pooled: 0 MB, 313.80 / 0.00 ms  …<--- JS stacktrace --->FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory

When a full GC barely frees anything (63.7 -> 63.6), live objects fill the heap. If the workload legitimately grew, raise the limit. If memory climbs steadily over time until the crash, treat it as a sign of a possible leak, not proof — a growing workload, cache or batch looks the same. Under the same load, check whether the objects surviving GC keep growing and whether their retainer paths in the heap snapshot from [3] keep getting bigger. If it is a leak, a higher limit only delays the crash.

[3] Write a heap snapshot before it dies. --heapsnapshot-near-heap-limit=<count> writes a snapshot when the heap approaches its limit (official docs), up to count files; use 1 or 2.

console
$ node --max-old-space-size=64 --heapsnapshot-near-heap-limit=1 leak.js; echo "exit=$?"
output
Wrote snapshot to /tmp/Heap.20261027.111832.55.0.001.heapsnapshot…FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memoryexit=134
console
$ ls -l *.heapsnapshot
output
-rw------- 1 root root 142138782 Oct 27 11:18 Heap.20261027.111832.55.0.001.heapsnapshot

Open the snapshot in the Memory tab of Chrome DevTools to see which objects piled up.

[4] Change the limit. The value is in MiB. The official docs' example: "On a machine with 2 GiB of memory, consider setting this to 1536 (1.5 GiB) to leave some memory for other uses and avoid swapping." Buffers and native modules use memory outside the heap, so never set it equal to the container limit.

console
$ node --max-old-space-size=1536 index.js$ NODE_OPTIONS=--max-old-space-size=1536 npm run build

Put in NODE_OPTIONS, it applies to every node process started from that shell, including build tools npm launches. Confirm it took by running the command from [1] the same way — the --max-old-space-size=256 column above is that check, and NODE_OPTIONS gave identical values.

The Node.js 22 CLI docs also list --max-old-space-size-percentage, "a percentage of available system memory" (greater than 0, up to 100), and say it takes precedence over --max-old-space-size when both are given. On 22.23.4, with 25, the 512 MB, 1 GB and 4 GB containers got 131, 268 and 1072 MiB — a share of the container limit.

console
$ node --max-old-space-size-percentage=25 -e 'const v8=require("v8");console.log("heap_size_limit MiB =", Math.round(v8.getHeapStatistics().heap_size_limit/1048576))'heap_size_limit MiB = 131

[5] Roll back. Drop the flag or clear NODE_OPTIONS (unset NODE_OPTIONS, or delete the environment line in systemd, the Dockerfile or compose) and the next start uses the default from [1]. If the Node process starts dying with 137 (SIGKILL) after you raised the limit, it most likely hit the container memory limit before the heap limit — revert right away. SIGKILL can come from elsewhere too, so confirm with OOMKilled in docker inspect or the kernel log (Docker memory limits).

Where UV_THREADPOOL_SIZE matters

Separate from memory: the official docs list the APIs that use libuv's threadpool — fs (except watchers and explicitly synchronous calls), async crypto.pbkdf2(), scrypt(), randomBytes() and friends, dns.lookup(), and zlib. The default is 4 threads, so one slow job among these makes unrelated ones wait. Raise it when those APIs pile up; it has nothing to do with the heap limit.

console
$ UV_THREADPOOL_SIZE=16 node -e 'const fs=require("fs");const t=()=>fs.readFileSync("/proc/self/status","utf8").match(/Threads:\s+(\d+)/)[1];console.log("before fs:",t());fs.readFile("/etc/hostname",()=>console.log("after fs:",t()))'before fs: 7after fs: 23

That counts Threads in /proc/self/status before and after the first fs.readFile. With the default it went 7 → 11 (four added); with 16 it went 7 → 23. The pool is created on first use, and the docs warn that setting process.env.UV_THREADPOOL_SIZE from inside the code is not guaranteed to work — pass it as an environment variable at startup.

#nodejs#max-old-space-size#heap-out-of-memory#v8#uv-threadpool-size#docker