Find memory leaks in nodejs in production
Asked Answered
C

2

5

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.

Claimant answered 17/9, 2015 at 9:37 Comment(3)
Why can't you stop the program and restart it?Lated
I don't want stop it because I'm in production, the leak appears 3 days after starting so I don't want change my production code and wait 3 days to may be have the same leak (which may not appear again). So, does exist it a tool to dump the memory (like in java for exemple).Claimant
Probably the leak occurs within the first hour, you just don't notice it right away. I recommend starting an instance in your test/dev environment, playing an hour worth of requests through it, and see how that goes. No need to mess with prod.Lated
C
8

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.
Ceasar answered 18/4, 2017 at 14:8 Comment(0)
R
3

Use the Chromium inspector

This solution works with most recent Node.js versions (the other answer depends on node-inspector, which is no longer maintained and does not work with Node.js 7 or higher.

It also doesn't require running the process with --inspect or doing any other preparation. You can use it right now with you process currently running in production.

Start by sending a signal to the process to enable debug mode:

kill -USR1 <pid>

Don't worry, your process won't be actually killed. After that, you should see a message similar to this in stderr:

Debugger listening on ws://127.0.0.1:9229/94b1fa2c-e478-4a76-bfb1-fc96c38d79610
For help, see: https://nodejs.org/en/docs/inspector

Now open chrome://inspect in a Chromium-based browser and instruct it to inspect your process by choosing it in the list that appears.

If your process is running in a remote machine, you can easily inspect it via a SSH tunnel by running this command:

ssh -C2qTnN -L 9229:localhost:9229 <remote-machine>

Replacing both occurrences of 9229 with the actual port (check the log message printed to stderr) and <remote-machine> with the address to your remote machine.

Reference: Node.js docs

Respectful answered 7/12, 2020 at 15:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.