Skip to main content

Node.js · 5 common causes

JavaScript heap out of memory

Updated

V8 reached its heap ceiling and could not free enough memory to continue, so the process aborted.

The exact message

<--- Last few GCs --->

[42:0x5a1f0a0]    41283 ms: Mark-sweep 2043.1 (2081.3) -> 2042.5 (2081.3) MB, 1250.4 / 0.0 ms

<--- JS stacktrace --->

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
 1: 0xb7a3c0 node::Abort() [node]
 2: 0xa8b6f5 node::OOMErrorHandler(char const*, v8::OOMDetails const&) [node]
Aborted (core dumped)

Paths, line numbers and stack frames differ on every machine, and the wording is tightened between versions. The part that identifies the problem is JavaScript heap out of memory.

How to fix it

Cause 1 of 5

The default heap limit is smaller than the job needs.

Raise it for that one command: NODE_OPTIONS=--max-old-space-size=4096 npm run build (the value is in megabytes). Only ask for memory the machine or container actually has — requesting 8 GB inside a 2 GB container just moves the failure to the kernel's OOM killer, which gives you no stack trace at all.

Cause 2 of 5

A container memory limit is lower than what Node assumed.

Size the heap against the container, not the host: set --max-old-space-size to roughly 75% of the container's limit, leaving room for the rest of the process. Raise the container limit too if the workload genuinely needs it.

Cause 3 of 5

The code holds an entire dataset in memory at once.

Stream it. Read files and HTTP bodies through streams or async iterators and process in batches; a JSON.parse of a multi-gigabyte file cannot be made to fit by any flag. For line-based data, readline over a read stream is a small change.

Cause 4 of 5

There is a leak — something grows and is never released.

Take two heap snapshots minutes apart and compare: run with node --inspect, open chrome://inspect, and use the Memory panel. Listeners added per request, an unbounded cache Map, and closures captured by a module-level array are the usual finds. A MaxListenersExceededWarning in the logs is an early tell.

Cause 5 of 5

Parallel workers each claim a full heap.

Jest, webpack and similar spawn one worker per core, and the limit is per process. Cap them — jest --maxWorkers=2 — so the total stays inside the machine's RAM.

Stopping it coming back

Set the memory flag explicitly in the scripts that need it rather than relying on the default, and run the memory-heavy job in CI on the same limits as production so it fails there first.

JavaScript heap out of memory — common questions

What is the default heap limit?
It depends on the Node version and the available memory — modern versions size it from the system rather than using one fixed number, and 64-bit builds commonly land around 2 GB or more. Do not rely on a specific figure; set the flag when a job needs a particular ceiling.
Is raising max-old-space-size a real fix?
For a build that legitimately needs more room than the default, yes. For a leak, no — it only postpones the crash, and a larger heap makes each garbage collection pause longer. If the number keeps needing to grow, that is the signal to look for the leak.
Why does the process die with no stack trace at all?
Then it was the operating system, not V8. A container killed at its cgroup limit is terminated instantly (exit code 137) with nothing printed. Check docker inspect for OOMKilled, or dmesg on a bare host.

Test the fix before you paste it back

Reproducing the failure in a scratch file is usually faster than editing the real one. AltF Code is a free in-browser editor with real syntax highlighting for HTML, CSS, JavaScript, TypeScript, Python, SQL and JSON, and it runs HTML, CSS and JavaScript live in a preview panel. No install and no sign-up.

Open the editor