# FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory (Node.js)

Printed by V8 just before the process aborts, so no JavaScript handler can catch it. Typical during next build, webpack or tsc on large codebases, and in long-running servers whose memory climbs until it reaches the heap ceiling.

```
<--- Last few GCs --->

[18324:0x150008000]   182340 ms: Mark-Compact 4043.1 (4130.4) -> 4029.7 (4132.9) MB, 1823.40 / 0.00 ms  (average mu = 0.112, current mu = 0.041) allocation failure; scavenge might not succeed

<--- JS stacktrace --->

FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory
----- Native stack trace -----

 1: 0x100c1b5c8 node::OOMErrorHandler(char const*, v8::OOMDetails const&) [/usr/local/bin/node]
 2: 0x100e0a1f4 v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, v8::OOMDetails const&) [/usr/local/bin/node]
 3: 0x100fd9a20 v8::internal::Heap::FatalProcessOutOfMemory(char const*) [/usr/local/bin/node]
```

## Quick fix

Give V8 more old-generation heap with --max-old-space-size (in megabytes), for example NODE_OPTIONS=--max-old-space-size=4096, keeping it below the machine or container memory limit. That unblocks builds. For a server that slowly climbs to the limit, more heap only delays the crash: take heap snapshots and fix the leak.

1. Decide whether the crash is in a build step or a running server.
2. For builds, set NODE_OPTIONS=--max-old-space-size=4096 (or higher) in that environment.
3. Keep the value under the container memory limit, leaving room for native memory.
4. For servers, log process.memoryUsage().heapUsed over time to confirm steady growth.
5. Start with --heapsnapshot-near-heap-limit=2 and compare snapshots in Chrome DevTools.
6. Fix whatever retains the growing objects: caches, listeners, closures, module-level arrays.

```bash
# Build step that needs more heap
NODE_OPTIONS=--max-old-space-size=4096 npm run build

# Server: write up to 2 heap snapshots as memory nears the limit
node --max-old-space-size=2048 --heapsnapshot-near-heap-limit=2 server.js
```

## Why it happens

### Memory leak in a long-running process

Module-level Maps or arrays used as caches with no eviction, event listeners added per request and never removed, or closures that keep whole request objects alive. Heap use rises with traffic until garbage collection can no longer free enough.

### Loading a whole dataset into memory

JSON.parse on a very large file, fs.readFileSync of a big export, or an ORM query that returns every row at once. Parsed JavaScript objects take far more memory than the raw bytes they came from.

### Large builds on small machines

TypeScript type checking, webpack and Next.js builds on big monorepos can need more heap than the default allows. The default depends on the Node version and system memory, so a build that passes on a 32 GB laptop can fail on a 4 GB CI runner.

### Unbounded concurrency

Promise.all over tens of thousands of items starts every request at once and holds every pending response in memory until all of them finish. Batching or a concurrency limit keeps the working set small.

## Other fixes

- Stream large files with fs.createReadStream and a streaming JSON or CSV parser instead of reading them whole.
- Paginate or use a cursor for big database queries rather than loading every row.
- Give in-process caches a size cap and TTL (an LRU) so they stop growing.
- If the process dies with exit code 137 and no FATAL ERROR line, the container limit killed it first: raise the container memory or lower the heap flag.

## Same error, other wording

- `FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory`
- `FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory`
- `FATAL ERROR: invalid array length Allocation failed - JavaScript heap out of memory`

## Sources

- [Node.js CLI options (--max-old-space-size, --heapsnapshot-near-heap-limit)](https://nodejs.org/api/cli.html)
- [Node.js v8 module (heap snapshots and statistics)](https://nodejs.org/api/v8.html)

---

This crash aborts the process before any JavaScript can run, so Runheed doesn't record this one. Runheed records the exceptions a running app throws and serves them to coding agents over MCP: https://runheed.com/?ref=agent-md

Human-readable version: https://runheed.com/errors/heap-out-of-memory
