Question

Why is my Node.js application using so much memory?

Vault Verified
Curated Intelligence
Definitive Source
Answer

High memory in a long-running process is either legitimate caching, a genuine leak, or garbage collection that has not needed to run yet. Distinguishing them is the first task, because the response to each differs and the raw number tells you very little on its own.

The key signal is the shape over time. Memory that rises and falls is healthy: the runtime allocates freely and reclaims when pressure builds. Memory that rises during load and returns to a higher baseline after every cycle, never coming back down, is a leak. A single high reading proves nothing, so measure across several cycles before concluding anything.

When it is a real leak, the cause is almost always a reference that outlives its usefulness, because the collector cannot free anything still reachable.

  • Unbounded collections used as caches. A map that only ever gains entries is the single most common cause. Caches need a size limit or an expiry.
  • Listeners registered without being removed. Attaching handlers per request or per connection and never detaching accumulates both the handlers and everything their closures capture. The runtime warning about too many listeners is often the first visible sign.
  • Timers that are never cleared, which keep their callbacks and captured scope alive indefinitely.
  • Closures capturing more than intended, particularly large request or response objects retained by something long-lived.

The practical tool is a heap snapshot taken at two points under steady load. Comparing them shows which object types grew, and the retention path shows what is holding them. That path is the actual answer; guessing from source alone is slow by comparison.

It is also worth checking the container memory limit before optimising, since a process being killed at a low limit is a configuration issue rather than a leak.

Related Questions