The ability to reply to discussions is limited to PRO members. Want to join in the discussion? Click here to subscribe now.

Manage Memory and Garbage Collection in Node.js

Manage Memory and Garbage Collection in Node.js

8:40
In this lesson, you will learn how to use view and interpret the garbage collection activity of your node.js app via the console. You will also learn how to take heapdump snapshots and analyze them with Chrome Developer Tools to identify possible memory leaks. A sample app with a known memory leak is provided as part of the walk-through to illustrate how to use the tools in your own environments.
Watch this lesson now
Avatar
egghead.io

In this lesson, you will learn how to use view and interpret the garbage collection activity of your node.js app via the console. You will also learn how to take heapdump snapshots and analyze them with Chrome Developer Tools to identify possible memory leaks. A sample app with a known memory leak is provided as part of the walk-through to illustrate how to use the tools in your own environments.

Avatar
Mickey

Windows Issue

Good example, but only found out that server.js had changed at the end of the lesson, and initially had been using the server.js created earlier which didn't have the memory leak. So for someone following thru with the lesson meant it didn't work and not sure why.

As I'm following on a Windows machine, found that I was unable to install the heapdump package, so that put the final kybosh on it. Would have been nice to end the course on a high, but wasn't to be.

In reply to egghead.io
Avatar
Will

Thanks for your comments Mickey!
I'll take a look at the discrepancy, thank you for pointing it out. Regarding using heapdump and other packages from Windows- you may find it useful to run a small Vagrant server or Docker image locally. Doing so allows you to simulate the behavior you will see on your production servers, as many of them may be one of the Linux distros.

In reply to Mickey
Avatar
John

I ran this lesson on Ubuntu 14.04, Node version 6.2.2. There was no memory leak activity reported.

output

john@john-aspire-7741:~/Documents/fenestra/development/gids-web/node-egghead/http$ node --trace_gc server-heapdump.js
[30883:0x28a71b0]       11 ms: Scavenge 2.1 (37.0) -> 2.0 (37.0) MB, 0.9 / 0 ms [allocation failure].
[30883:0x28a71b0]       16 ms: Scavenge 2.1 (37.0) -> 2.1 (38.0) MB, 1.0 / 0 ms [allocation failure].
[30883:0x28a71b0]      109 ms: Scavenge 4.0 (41.0) -> 3.7 (41.0) MB, 1.0 / 0 ms [allocation failure].
received request
received request
received request
received request

received request
received request
received request
received request
received request
received request
^C
john@john-aspire-7741:~/Documents/fenestra/development/gids-web/node-egghead/http$ node --version
v6.2.2

server-heapdump.js

const http = require('http')
const heapdump = require('heapdump')

function BigData() {
    var mem = Array(1000000).join("a")
}

const leak = []
const server = http.createServer((request, response) => {
    if (request.url === '/') {
        leak.push(new BigData())
        console.log('received request')
        response.writeHead(200, {"Content-Type" : "text/plain"})
        response.end('Hello world')
    }
})

server.listen(3000)
Avatar
Will

Yup. I was able to duplicate the results you show.
The lesson is still accurate for versions of node 4.x and lower but I'll need to update it for v6.
Thanks for pointing this out!

In reply to John
Avatar
Vishwas

I see you are requiring a module heap but it's not been referenced anywhere in the code. Where is the heap dump file created?

Avatar
jpbamberg1993

Your log say "allocation on failure" mine says "allocation failure". Are they different?

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?