Skip to content
Shahariar Niloy, home

Don’t raise the memory limit — find what’s holding on

How to trace “JavaScript heap out of memory” crashes in a Node or Next.js server, ranked by impact.

Performance2 min readBy Shahariar Niloy

When a Node server starts crashing with “JavaScript heap out of memory”, the first fix everyone reaches for is a bigger heap. It works — for a while. The crash just moves later, and every crash after that is more expensive.

The better question isn’t how much memory do we need. It’s what is holding on to memory it should have let go?

Rank before you fix

List everything that can keep memory alive past the end of a request. Then rank each suspect by two things: how likely it is to cause these crashes, and how much it can hold at its worst. Work strictly from the top of that list. It stops you from polishing the wrong thing.

The usual suspects

See it coming

Add a health check that compares the real heap with the container’s memory limit. When it gets close, the instance reports itself unhealthy and stops taking traffic — before it crashes, not after.

Memory problems are rarely one big leak. They’re a few reasonable decisions that become dangerous together, under load, on a bad day.

Case study: Taming memory
Next postWhy your retries are a memory leak