← All posts

Memory Leak in Node.js Apps

image by icon-library.com

This is my experience of detecting memory leaks in Node.js applications. I’m working on a project where the load on the server is about 15k requests per second and sometimes memory problems can occur. So it happened with me. I noticed a systematic increase in RAM, thanks to the use of Grafana metrics (a powerful tool for analytics).

I had never encountered such a problem before and there was no one to suggest how to be in this situation. To find the reason for this, I had to spend 2 days, testing several hypotheses and researching the Internet.
But let’s start over.

What is RAM in Node.js?

Node.js apps memory structure

The memory structure of V8 is divided into the following areas:
1. Code Segment.
2. The stack is a memory area that stores static data, including method/function frames, primitive values, and pointers to objects.
3. The heap is the largest area of memory where V8 stores objects or dynamic data. This is where garbage collection takes place.
V8 manages heap memory using garbage collection, it frees memory used by objects that are not directly or indirectly referenced from the stack.

What are memory leaks?
A memory leak is a piece of memory on the heap that is no longer needed by the application, but that was not returned to the operating system by the garbage collector.

What could be the reasons?
1. Global variables.
2. Multiple references.
3. Timers and events.
4. Closures.

Now let’s move on to practice.
The first sign of an application memory leak is memory, CPU usage, and host machine load average increasing over time for no apparent reason. But when you restart the application, all problems immediately disappear! But over time, the problem recurs periodically.

Grafana metrics

Here’s what I saw. You can see how every 2–3 hours the RAM rapidly increased until the application was restarted. That is, due to a memory leak, it constantly increased.

Problem detected. Where to begin?
The first step to delay the problem is to restart the application before it reaches a critical size. I used the pm2 command: 
pm2 start server.js — max-memory-restart 200M
Now we have some time and I can start debugging.

Where to start debugging?
1. Creating an effective test environment:
 — The code must be built, optimized, and tuned exactly as it is when run in a production environment in order to reproduce the leak identically.
 — A properly configured test environment should perform the same load as the production environment.
2. Accessing Node.js memory with V8 Inspector and Chrome Dev Tools.
 — You can use developer tools to inspect Node.js applications. Node.js and Chrome use the same engine, Chrome V8, which contains the inspector used by the developer tools.
3. wtf.dump (combines functions for requesting active handles and requests:
process._getActiveHandles() process._getActiveRequests()).
4. pm2 monit (tool for monitor memory and CPU easily and straight from your terminal).


I have done all this, step by step, and finally I found and fixed the issue that could lead to big problems. You may never encounter such a problem, but if you do, you’ll know where to start.
I hope my story helps you. If you have any questions or thoughts, please feel free to write your comment.