Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson

Already subscribed? Sign In

Autoplay

    Identify memory leaks with nodejs-dashboard

    Will ButtonWill Button

    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.

    javascriptJavaScript
    nodeNode.js
    Code

    Code

    Become a Member to view code

    You must be a 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
    Transcript

    Transcript

    00:00 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.

    00:19 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.

    00:44 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.

    00:58 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.

    01:26 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.

    01:39 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.

    01:53 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.

    Discuss

    Discuss