Showcasing Node.js Memory Leaks
In order to expose the inspector, let's run Node.js with the --inspect
flag.
node --inspect index.js
Debugger listening on ws://127.0.0.1:9229/7fc22153-836d-4ed2-8090-a84a842a199e
For help, see: https://nodejs.org/en/docs/inspector
Sample app listening on port 3000.
Open up Chrome and go to chrome://inspect
. Here you can take snapshots of the memory usage.
open -a "Google Chrome" chrome://inspect/#devices
Watching Memory Allocation In Real-Time
You use the Allocation instrumentation on timeline
option in this case. Select that radio button and check the Record stack traces of allocations
checkbox. This will start a live recording of the memory usage.
For this use case, I used loadtest
to run 1000 requests against the sample Express app with a concurrency of 10.
For the first few requests, you can see a spike in memory allocation. But it’s obvious that most memory is allocated to the arrays, closure, and objects.