Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

A Node.js app on Heroku crashes with 'heap out of memory' while the dyno has memory left

Your error

allocation failure scavenge might not succeed3 more lines it can show

Fix it fast

Most common cause, 1 of 3

The heap is too small for the normal workload

On Fir, move to a larger dyno and the default limit grows with it.

Fix
# Procfile for a 1 GiB dyno (Standard-2X)
web: node --max-old-space-size=512 server.js
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

allocation failure scavenge might not succeed followed by JavaScript heap out of memory means V8 reached its heap limit while the Heroku dyno had memory left. Give the heap half the dyno: Fir does that by default, on Cedar set --max-old-space-size in the Procfile. If heap use after collection keeps rising on Node.js 22 to 26, it is a leak.

  • Applies to Node.js 22, Node.js 24, Node.js 26, Heroku Cedar and Fir dynos
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Log heap use and the heap limit from inside the app once a minute. Heroku's own metrics do not report the heap, so these lines are what tell you whether usage after collection levels off near the limit or keeps climbing.

Run this first
import { getHeapStatistics } from 'node:v8';
import { memoryUsage } from 'node:process';

const mb = (n) => Math.round(n / 1048576);
setInterval(() => {
  const { heapUsed, rss } = memoryUsage();
  console.log(JSON.stringify({
    heapUsedMB: mb(heapUsed),
    heapLimitMB: mb(getHeapStatistics().heap_size_limit),
    rssMB: mb(rss),
  }));
}, 60_000).unref();

Then find the line you see in this list and open its fix:

  1. 1The heap is too small for the normal workloadheapUsedMB rises to heapLimitMB under load and levels off there; crashes line up with traffic peaks or big requests
  2. 2A leak keeps growing the heapheapUsedMB after each collection creeps up over hours or with request count, and the crash comes back at the same pace after every restart
  3. 3A heap flag in the NODE_OPTIONS config var sets the ceilingThe config var NODE_OPTIONS contains --max-old-space-size, often with a low value, and builds also stop with heap errors

What does the error look like?

  • allocation failure scavenge might not succeed
  • FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
  • <--- Last few GCs --->
  • The process dies while dyno memory use is still under the quota

Cause 1 of 3

The heap is too small for the normal workload

You seeheapUsedMB rises to heapLimitMB under load and levels off there; crashes line up with traffic peaks or big requests

Check

Heap out of memory with dyno memory to spare, after heap use has levelled off near the limit, means the heap is too small for the workload. Heroku calls this insufficient memory: usage after collection settles near the limit instead of growing forever. On Fir, Node.js reads the dyno limit and gives old space about half of it, between 256 MiB and 2 GiB (4 GiB on dynos with 16 GiB or more). On Cedar the default follows the host machine, so the flag you set decides the ceiling.

Check
cat Procfile

Fix

On Fir, move to a larger dyno and the default limit grows with it. On Cedar, set --max-old-space-size in the Procfile to about half the dyno's memory, such as 256 on a 512 MiB dyno or 512 on a 1 GiB dyno, and raise it with the dyno size. Keep the other half free: new space can add up to 200 MiB, and Buffers live outside the heap.

Fix
# Procfile for a 1 GiB dyno (Standard-2X)
web: node --max-old-space-size=512 server.js

Verify

After the deploy, heapLimitMB in the log lines is higher than before, and the next traffic peak passes without a crash.

Verify
grep max-old-space-size Procfile

Confirmed by devcenter.heroku.com

Did this fix it?

Cause 2 of 3

A leak keeps growing the heap

You seeheapUsedMB after each collection creeps up over hours or with request count, and the crash comes back at the same pace after every restart

Check

Heap out of memory that returns at a steady pace after every restart points to a leak. A leak keeps objects reachable, so garbage collection can never free them. Raising the limit or the dyno size only moves the crash later.

Check
npx autocannon -d 60 http://localhost:3000

Fix

Find what grows with heap snapshots. Take a baseline in Chrome DevTools, send traffic, take a second one and sort the comparison by size delta. On Heroku use a staging app: a snapshot blocks the event loop and needs about twice the heap in memory.

Fix
# Procfile on a staging app (Cedar)
web: node --inspect=9090 server.js

# on your machine, then open chrome://inspect
heroku ps:forward 9090

Verify

With the leak fixed, heapUsedMB under steady load stops rising after the first minutes.

Verify
npx autocannon -d 60 http://localhost:3000

Confirmed by devcenter.heroku.com

Did this fix it?

Cause 3 of 3

A heap flag in the NODE_OPTIONS config var sets the ceiling

You seeThe config var NODE_OPTIONS contains --max-old-space-size, often with a low value, and builds also stop with heap errors

Check

Heap out of memory on the dyno and in the build at once points to a --max-old-space-size flag in the NODE_OPTIONS config var. Config vars reach the build as well as the dynos on Cedar, and on Fir when build-time config vars are on, so one low value limits both. Heroku advises against heap flags in NODE_OPTIONS for that reason.

Check
heroku config:get NODE_OPTIONS

Fix

Remove the heap flag from NODE_OPTIONS and put it in the Procfile, so it applies only to the running app. If NODE_OPTIONS holds other flags too, set it again with only those.

Fix
heroku config:unset NODE_OPTIONS
# Procfile
web: node --max-old-space-size=256 server.js

Verify

The config var is gone, the next build succeeds and the web process logs the heap limit you set.

Verify
heroku config:get NODE_OPTIONS

Confirmed by devcenter.heroku.com

Did this fix it?

How do I stop it coming back?

  • Keep the heap log line from the first step in production, since nothing else on Heroku shows heap usage.
  • Change --max-old-space-size in the Procfile every time you change the dyno size.
  • When one dyno runs several Node.js processes, give each a heap limit of half the dyno memory divided by the process count.
  • Run a load test locally after big dependency or caching changes and watch heap use after collection.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: TaxiDex, Workforce, stegnor.com.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.