In this article I will quickly describe how I analyzed memory usage, mostly for my own future reference.
Node.js has a functionality to start a debugger, which is available through websockets. To start the debugger on a process that is already running, without being prepared, you can just send the respective process a signal USR1
and Node.js will start the debugger.
kill -USR1 <pid>
After this you have to check the stderr output of the respective process, usually that should be forwarded to some kind of log. The message you are looking for should look something like this:
Debugger listening on ws://127.0.0.1:9229/900ebfb3-dd7b-448f-8aac-7b50d4fd7e7e
Of course, the UUID will probably be different. What is most important in this case is the port number the debugger is listening on. In this case, as is the default, the debugger is listening on port 9229
. If you can't find this message, you can try just listening on that port anyway.
Usually the process from above will be running on a server that you are not directly working on and/or do not have a graphical interface on. In this case it will be easier if you have some debugger client running on a local machine.
The Node.js debugger will be listening only on the localhost IP address for safety reasons. If you want to access it from another machine, assuming you have SSH access, you can use SSH port forwarding with the following command.
ssh -CqTf -L 9229:localhost:<debugger port> <server name> sleep 10
-n
when command execution starts. This means ssh will exit even if it worked and is still running in the background.Now you are ready to connect to the debugger with some kind of client. Note that Chromium (and thus Chrome & Edge) comes with built in tools for Node.js. Go to chrome://inspect or edge://inspect and you should see a link about "dev tools for node".
After that you can access the console, check memory contents with a heap snapshot and much more. Probably everything you want, really.
The process on the server should keep running unless you stop it or something bad happens. So normally you should be able to just stop the debugging session and the process will also keep running.
To stop the Node.js debugger, you need to go to the console and type the following:
inspector = require('inspector');
inspector.close();
After you have typed this, the dev tools will be disconnected. You can then continue by closing the dev tools and stopping the SSH command so the tunnel will be closed too.