Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Identify memory leaks with nodejs-dashboard


    In this lesson, I introduce a memory leak into our node.js application and show you how to identify it using the Formidable nodejs-dashboard. Once identified, we will add garbage collection stats to the error console allowing us to correlate garbage collection with the increased memory usage.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    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.