Skip to content

Instantly share code, notes, and snippets.

@irom77
Forked from dberesford/gist:28876b39d26b02b7683a
Created December 11, 2015 15:41
Show Gist options
  • Save irom77/7fe38236b661af8159aa to your computer and use it in GitHub Desktop.
Save irom77/7fe38236b661af8159aa to your computer and use it in GitHub Desktop.
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, '127.0.0.1');
server.setMaxListeners(0);
console.log('Server running at http://127.0.0.1:1337/. Process PID: ', process.pid);
memwatch.on('leak', function(info) {
console.error(info);
var file = '/tmp/myapp-' + process.pid + '-' + Date.now() + '.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