The last thing I want to show you is what you can do with the memory usage screen in NodeJS-dashboard. I have my express application here, and I've got the root endpoint set up so it generates a random number of fake lorem words, somewhere between one and a thousand words.
Then it splits it into an array. For each word in the array, it pushes it up to a global variable or a global array and stores it there. Returning to the NodeJS-Dashboard, whenever I kick off a bunch of users to hit this endpoint, if you watch the resident memory down there, you can see that it's continually increasing.
One of the things I can do to troubleshoot this further is to install an NPM module called the Garbage Collection Profiler. Again, I'll save this to a devDependency, because it's not something you're going to want to do in production.
While that's installing, I can return to my application code. I'll create a profiler object that's going to require the Garbage Collection Profiler. That creates an event stream so I can do profiler.on and listen for the garbage collection event. Then I'm just going to write out to the error console whenever this gets called the info object that gets created.
Now, returning to the NodeJS-dashboard, whenever I start hitting this with users. As the memory starts to increase, we'll see the garbage collections operations happening in the error console.
By watching this run, what I'm seeing is the garbage collections operations are happening, but my heap usage is not going down, indicating that something's not being released from memory, giving me the opportunity to troubleshoot that further.
The next step I would take in troubleshooting this would be to start taking heap dumps at the beginning of the application life cycle. Later, whenever we're up around this point when we've got a bunch of memory allocated, take that heap dump and compare it before and after to see what it is that's consuming the memory.