Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find memory leaks in nodejs in production

I have a nodejs application in production and I have a memory leaks (memory increases from 600Mo to 3Go in 3 days).

I searched some tools to take a memory dump but I found only plugins who need to be in the application to write dump.

Of course, I can't stop my application and restart it (after modification) to take snapshot, I would like to take a memory dump outside node with a outside tools.

Do you know a tool like this ?

Thanks.

like image 515
Kiva Avatar asked Sep 17 '15 09:09

Kiva


People also ask

How do I find a memory leak in production?

The best approach to checking for the existence of a memory leak in your application is by looking at your RAM usage and investigating the total amount of memory been used versus the total amount available. Evidently, it is advisable to obtain snapshots of your memory's heap dump while in a production environment.

How do you check if there are memory leaks?

The primary tools for detecting memory leaks are the C/C++ debugger and the C Run-time Library (CRT) debug heap functions. The #define statement maps a base version of the CRT heap functions to the corresponding debug version. If you leave out the #define statement, the memory leak dump will be less detailed.


1 Answers

You can create one (or better several) heapdump(s) in two ways, modifying the code, or without modifying the code.

Fast way (edit code):

  1. Add require('heapdump'); at the beginning of your code.
  2. Send a USR2 signal to node.js process, by doing kill -USR2 {{pid}} from the terminal, or from the code with process.kill(process.pid, 'SIGUSR2');
  3. You will get several heapdump-XXX.YYY.heapsnapshot files, that you can compare with Chrome Dev Tools.

Slow way (without editing code neither restarting):

  1. Send a USR1 signal to node.js process, by doing kill -USR1 {{pid}}. Note that this will enable the debug mode, causing an apparent "freeze" in the process.
  2. Run node-inspector (posible after installing it globally by doing npm install -g node-inspector)
  3. Open http://0.0.0.0:8080/debug?port=5858 in Google Chrome (port 8080 is used by node-inspector, and port 5858 is used by debugger), this will cause the process to "unfreeze"
  4. Now you can take so many head dumps as you need, by clicking in "Take heap snapshot" button.
  5. You will have several Snapshot X files, that you can compare with Chrome Dev Tools.
like image 183
greuze Avatar answered Sep 24 '22 17:09

greuze