Menu

1 / 2

Finding a memory leak in a NestJS app in production

DEV Community: nestjs·Kamil Mysliwiec·17 days ago
#UeuwkC8i
#devtonestjs#devto#dev#heap#request
Long article (2760 words) — no AI summary yet.
Reading 0:00
15s threshold

The pattern is familiar. Memory climbs slowly for a day or three, then a pod restarts with OOMKilled , or the process dies with FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory . The restart brings memory back down, so the graph looks like a saw with very long teeth. And it never happens on your machine, because nothing on your machine runs for three days under real traffic.

Restarts hide a leak, they don't fix it. Here's how I'd go from "memory keeps growing" to the line of code that holds on to it.

First: is it actually a leak?

Node's memory always moves. V8 allocates, collects, allocates again, so a healthy service draws a sawtooth. A leak is when the bottom of the sawtooth keeps rising: what survives each major garbage collection grows, while traffic stays flat.

Three numbers from process.memoryUsage() tell most of the story:

heapUsed : memory held by live JavaScript objects. A leak in your code shows up here.

heapTotal : what V8 has reserved for the heap.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.