The profiling section allows you to take memory and CPU snapshots straight from your production servers. You will then get a file that can be inspected with the chrome developer tool.
You can now install the V8 profiler via pm2, and have it available everyhwere on your server. Just use:
$ pm2 install profiler # or $ pm2 install v8-profiler
You will need to install pmx and to add the v8-profiler dependency to your project:
$ npm install v8-profiler --save
If you don’t see the profiling buttons appearing after restarting your app, consider upgrading pm2 to 2.3+.
Once the module is installed, restart your application and different buttons will appea on the Profiling pages:
Now click on the button to take a heapdump, it may take some time depending on the weight of the heap file. Once the heapdump file is ready the “Download” button will appear:
Click on the “Download” button. Once the download is complete, open up the Google Chrome developer tool (Ctrl+Shift+i), go to the Profiles tab and select the option Load:
Select the heapdump file you just downloaded and you will have an overview of your memory.
To track memory leak you will need multiple heapdump files and compare them to see which element is increasing over time.
To know more about memory analysis please follow this link.