Detecting memory leaks in nodejs

Are there some tools for detecting memory leaks in nodejs? And tell me about your experience in testing nodejs applications.

The following tool should be useful for spotting memory leaks:

node-inspector

And there's also a tutorial to help you find memory leaks here:

https://github.com/felixge/node-memory-leak-tutorial

In tracking down a memory leak I tried the above node-inspector.
As of April 2012 it was not updated to work with the contemporary node release v0.6.12 As such I found: https://github.com/c4milo/node-webkit-agent.
It was able to show heap snapshots for the newer V8 engine which wasn't supported by node-inspector. In short order I was able to detect the leaking module (in may case loggly), I hope you have similar success!

I could also recommend following sources:

  1. following episodes of NodeUp, where profiling and memory leaks detection tools are discussed:

  2. This article - Tracking Down Memory Leaks in Node.js – A Node.JS Holiday Season, which basically aggregates all widely known modules and techniques to track down memory leaks

  3. For Mac OS - Instruments tool could be helpful to debug native (C++) modules. It is not so sophisticated as SmartOS tools, but very easy to setup.

I use Chrome dev tools and heapsnapshot file directly, not node-inspector or node-webkit-agent.

require() the heapdump module.

Send usr2 signal to the running nodejs process to get heapsnapshot file.

Load the heapsnapshot file on the profiles tab of Chrome dev tools.

One good thing is https://github.com/bnoordhuis/node-heapdump very simple and you can view results in Chrome (because of same V8 engine in browser javascript and nodejs) and compare object sizes in memory at every moment you want.

And one more tip how to detect memory leak "manualy" or help to prevent it, is force garbage collector in some places of your code and see what happens.

Start your application with "node --expose-gc file.js" and anywhere in code you can use function gc(); to call garbage collection.

I have used the npm package Memwatch:

Take a look to the Github repository and the NPM source

Basically this package checks the memory heap usage right after a garbage collection is performed by the V8 engine, and give you a baseline of the actual memory usage.

Here's how I used it:

var memwatch = require('memwatch');

memwatch.on('leak', function(info) {
    console.log('Memwatch leak: ');
    console.log(info);
});

memwatch.on('stats', function(stats) {
    console.log.message('Memwatch stats: ');
    console.log(stats);
});

From the original documentation:

The 'stats' event, emitted occasionally, give you the data describing your heap usage and trends over time.

The 'leak' event, is emitted when it appears your code is leaking memory. It is usually executed when the heap size continously grows in a short period of time.

Memwatch also provides a "HeapDiff" class to compute the heap state between two snapshots you can take in your functions.

It might be a good idea to have memwatch running in your stage environment, in order to track events causing the issue.