Lingua-e
JavaScript

What is a JavaScript Heap Out of Memory?

← Errors

This fatal error crashes the Node.js process when the V8 JavaScript heap is exhausted. Unlike most errors, it cannot be caught with try/catch because it is a hard process abort. The default heap size is roughly 1.5 GB on 64-bit systems.

Why it happens

  • Accumulating large amounts of data in memory instead of streaming.
  • A memory leak from closures, event listeners, or global caches that grow unboundedly.
  • Loading an entire large file or database result set into memory at once.
  • The heap size is insufficient for the workload and needs to be increased.

Examples

JavaScript

Code that triggers the error

const data = [];
while (true) {
  data.push(new Array(10000).fill("x"));
}

Error output

FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory
 1: 0xb7c2e0 node::Abort() [node]
...
Aborted (core dumped)

How to fix it

Use streams and generators to process data incrementally. Profile memory with --inspect and Chrome DevTools heap snapshots to find leaks. Increase heap size with --max-old-space-size as a temporary measure while fixing the root cause.

Fixed code

// Increase heap size as a short-term fix
// node --max-old-space-size=4096 app.js

// Long-term: process data in streams instead
const { Readable } = require("stream");
const readable = Readable.from(generateData());
readable.on("data", (chunk) => process(chunk));

Related errors

Ready to practice your English at work?

Lingua-e has interactive exercises built around real developer conversations: standups, code reviews, retrospectives, and more. Practice until it comes naturally.

Try Lingua-e for free