JavaScript Heap Out of Memory: What It Means and How to Fix It
Error reference
JavaScript Heap Out of Memory
Language
Severity
CriticalWhen it happens
This fatal error crashes the Node.js process when the V8 JavaScript heap is exhausted.
Potential fixes
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.
Deep dive
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.
Potential fixes
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.
Examples
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)
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));Practice in English
How would you explain a JavaScript Heap Out of Memory to a fellow dev? Choose the right phrase:
"I hit an error...
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