Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Sample node.js leak detection using memwatch and heapdump
var http = require('http');
var util = require('util');
var heapdump = require('heapdump');
var memwatch = require('memwatch');
var server = http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
for (var i=0; i<1000; i++) {
server.on('request', function leakyfunc() {
var l = http;
res.end('Hello World\n');
}).listen(1337, '');
console.log('Server running at Process PID: ',;
memwatch.on('leak', function(info) {
var file = '/tmp/myapp-' + + '-' + + '.heapsnapshot';
heapdump.writeSnapshot(file, function(err){
if (err) console.error(err);
else console.error('Wrote snapshot: ' + file);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment