Australian cloud for business software — built and run in Sydney since 2002

NetLeverage
NetLeverage

Node.js "FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory": find what fills the heap before you raise the limit

Technical article
Troubleshooting

About this article: This article is reviewed against the cited sources. This is source research, not lab validation. Check the stated versions and your own requirements before making changes.

When a Node.js process ends with FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory, V8 has used up the heap size it is allowed and garbage collection could not free enough. The process aborts; it does not recover. Read the limit that applies, watch whether heapUsed climbs without falling, and capture snapshots near the limit before you change any number. Raising --max-old-space-size is the quick remedy, and it only helps when the data is needed rather than leaked.

What the error means

In the Node.js 22.23.5 source, the fatal-error handler prints FATAL ERROR: %s %s, where the first value is a location string from V8 and the second is "Allocation failed - JavaScript heap out of memory" when the failure is in the heap. In the V8 code bundled with that release, the location string "Reached heap limit" is used when the old generation cannot expand and the near-heap-limit callback has not made room. Joined, they give the message in the title. No Node.js documentation page prints the Reached heap limit form; it is joined from the two source files. The Node.js command-line page shows a variant of the same family: FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory.

Node.js's page for --max-old-space-size says it "Sets the max memory size of V8's old memory section" and that "As memory consumption approaches the limit, V8 will spend more time on garbage collection in an effort to free unused memory." So a process often slows down before it dies.

The v8 module page says heap_size_limit is the maximum size of the V8 heap, in bytes, "either the default limit, determined by system resources, or the value passed to the --max_old_space_size option". The default therefore differs from one machine to the next.

Likely causes

Cause What you see How to confirm
Objects are kept and never released (a leak) heapUsed climbs over hours and does not fall after garbage collection Compare two heap snapshots and look for large positive deltas
One job loads more than the limit allows The abort happens at the same step every time, for example a very large file or query result held at once The same input fails on a fresh start; the crash time does not depend on uptime
Default limit is lower than the machine can give The host has plenty of free memory when the process dies heap_size_limit is far below host memory
Limit was set higher than the host can supply The process vanishes with no FATAL ERROR line Our own reading: the kernel or the container runtime ended it; check as in step 4 below
Capturing a snapshot used the memory itself The process ends just after a snapshot was requested The V8 page says a snapshot needs memory about twice the heap and names a risk of being killed for memory, not this FATAL ERROR line

Prove the cause before changing anything

All of these only read, except that step 2 creates one new file in a scratch folder.

1. Keep the whole message. The lines above it matter. Node.js's page shows a <--- Last few GCs ---> section with lines such as Mark-sweep 130.6 (147.8) -> 130.5 (147.8) MB. Our own reading: the figure before the arrow is the heap in use before the collection and the figure after is what remained, so a drop of 0.1 MB means the heap was full of live objects.

2. Read the limit your process really has. Put the four lines from the Node.js guide into a file in a scratch folder, heap-limit.js:

const v8 = require('node:v8');
const { heap_size_limit } = v8.getHeapStatistics();
const heapSizeInGB = heap_size_limit / (1024 * 1024 * 1024);
console.log(`${heapSizeInGB} GB`);

Run it with the same NODE_OPTIONS value and flags as the service, so that it reports the limit your service has:

node heap-limit.js

Node.js's page says NODE_OPTIONS values "are interpreted before command-line options". Our own reading: a flag in the service's command therefore wins over the same flag in the environment.

3. Watch memory over time. The Node.js guide describes process.memoryUsage(), which returns rss, heapTotal, heapUsed, external and arrayBuffers, and says that if heapUsed "steadily grows without being released, it could indicate a memory leak". Where your application already logs these, compare readings over hours. A leak climbs; a single oversized job jumps.

4. Tell this error from an operating system kill. This error is printed by Node.js itself. A kill by the kernel leaves its record in the kernel log instead; see Linux OOM Killer on a VM: Find the Cause, Set Memory Limits. In a container, see Docker container Exited (137): OOMKilled, a stop timeout, or a kill.

Fix

Choose by what step 3 showed.

Capture snapshots near the limit

Use this when memory climbs and you need to know what grows. Node.js's page says --heapsnapshot-near-heap-limit=max_count "Writes a V8 heap snapshot to disk when the V8 heap usage is approaching the heap limit" and that the snapshots "can be compared to determine what objects are being allocated". The guide adds: "If you're going to take a heap snapshot in production, make sure the process you're taking it from can crash without impacting your application's availability."

First save the current state, with start-app.sh standing for whatever file holds your start command:

cp -p start-app.sh start-app.sh.2026-12-03

Disruptive step: the next change restarts the Node.js process with a flag that writes up to three heap snapshots. The V8 page says "Creating a heap snapshot requires memory about twice the size of the heap at the time the snapshot is created" and that it "blocks the event loop", so the application stops answering while each snapshot is written and may be killed for memory. Our own reading: a snapshot describes the objects in the heap, so it can include data your application holds; our own advice is to keep the files off shared storage and to check free disk space first.

Add the flag to the start command. Then stop the process, wait until it has ended, and start it the way you normally do:

node --heapsnapshot-near-heap-limit=3 index.js

After the crash, load the older snapshot first and the newer second in the Chrome DevTools Memory tab, switch the mode from Summary to Comparison, and, in the guide's words, "Look for large positive deltas and explore the references that caused them in the bottom panel."

Only if you need to back out, the saved copy goes back over the changed start command.

Disruptive step: the next command overwrites start-app.sh, so any edit made to that file after the save is lost, and the restart that follows it drops connections again.

cp -p start-app.sh.2026-12-03 start-app.sh

Then stop the process, wait until it has ended, start it the way you normally do, and confirm that the service answers.

Raise the limit

Use this when the data is genuinely needed, or when step 2 showed a default far below what the host can give. Node.js's page gives the sizing 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." Its example command is node --max-old-space-size=1536 index.js.

Run only one of the two changes at a time, and undo the first before starting the second, because each keeps its own saved copy.

First save the current state:

cp -p start-app.sh start-app.sh.before-limit

Disruptive step: the next change sets a larger heap limit and restarts the Node.js process. Our own reading: a limit above what the host can supply leads to swapping or to a kill by the operating system, which gives no FATAL ERROR line. Raising the limit also delays the abort of a leak without removing it.

Set the flag in the start command. Then stop the process, wait until it has ended, and start it the way you normally do:

node --max-old-space-size=1536 index.js

Only if you need to back out, the saved copy goes back over the changed start command.

Disruptive step: the next command overwrites start-app.sh, so any edit made to that file after the save is lost, and the restart that follows it drops connections.

cp -p start-app.sh.before-limit start-app.sh

Then stop the process, wait until it has ended, and start it the way you normally do.

Reduce what is held at once

If a single job loads a very large file or result into memory, the remedy is in the code: process it in pieces. This is our own advice.

Example (illustrative): app01 has 2 GiB (2048 MiB) of memory and runs index.js. The service dies with FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory after about two days. heap-limit.js prints 1.4296875 GB, which is 1,535,115,264 bytes, or 1,464 MiB. heapUsed climbs every hour. Setting --max-old-space-size=1536 raises the limit by 1536 - 1464 = 72 MiB and leaves 2048 - 1536 = 512 MiB for everything else; the crash moves from about two days to a little later. Snapshots taken near the limit show one growing set of objects, so the fix is in the code that keeps them, not in the number.

Confirm the fix

  1. node heap-limit.js, run with the service's flags, prints the limit you intended.
  2. heapUsed rises and falls with load across several days and does not trend upward.
  3. The service ends no more with the FATAL ERROR line, and the host keeps free memory at peak.

What to watch afterwards

  • Record process.memoryUsage() values at a fixed interval and alert on a rising heapUsed trend rather than on one reading.
  • Re-check the limit after a Node.js upgrade, because the default is determined by system resources and may differ between releases.
  • Treat a heap snapshot file as sensitive data.
  • The --max-old-space-size-percentage flag appears on the Node.js 22.23.5 page and says it "takes precedence over --max-old-space-size when both are specified"; confirm that your own release has it before you rely on it.

Risks and limits

  • These commands have not been run or lab-tested.
  • Snapshots stop the event loop and need about twice the heap in memory, per Node.js's page; do not take them on the only instance serving customers.
  • The message text is a reading of the Node.js 22.23.5 source; other releases may word it differently, and the "Reached heap limit" and "Ineffective mark-compacts near heap limit" forms are two spellings of the same family.
  • A larger limit hides a leak for longer. It does not fix one.
  • The default limit depends on the machine, as the v8 module page says.

Sources

  • Command-line API — Node.js documentation, v22.23.5, no date shown, accessed 3 December 2026
  • V8 — Node.js documentation, v22.23.5, no date shown, accessed 3 December 2026
  • Understanding and Tuning Memory — Node.js Learn, no date shown, accessed 3 December 2026
  • Using Heap Snapshot — Node.js Learn, no date shown, accessed 3 December 2026
  • node_errors.cc — Node.js source repository, tag v22.23.5, no date shown, accessed 3 December 2026
  • heap.cc — V8 source bundled in Node.js, tag v22.23.5, no date shown, accessed 3 December 2026