Skip to content

Instantly share code, notes, and snippets.

@vicapow
Created July 11, 2012 15:45
Show Gist options
  • Save vicapow/3091278 to your computer and use it in GitHub Desktop.
Save vicapow/3091278 to your computer and use it in GitHub Desktop.
leaking memory?
var http = require('http')
, util = require('util')
, pwd = '/Users/victor/Downloads/images'
, fs = require('fs')
, mfds = 1024 // max open file descritors
, ofds = 0
, memwatch = require('memwatch')
, _ = require('underscore')
function copyFiles(){
fs.readdir(pwd,function(err,files){
if(err) throw err;
var filename = ''
while(ofds < mfds) addStream();
function addStream(){
if(files.length==0) return done();
filename = files.pop();
var wstream = fs.createWriteStream('/dev/null')
, rstream = fs.createReadStream(pwd + '/' + filename)
ofds+=2;
var after = _.after(2,function(){
wstream = null;
rstream = null;
ofds-=2;
addStream();
});
rstream.on('close',after);
wstream.on('close',after);
rstream.pipe(wstream);
}
function done(){
if(ofds===0){
files = null;
filename = null;
process.stdout.write('DONE!');
memwatch.gc();
process.nextTick(copyFiles);
}
}
});
}
setTimeout(copyFiles,1000);
var hd = new memwatch.HeapDiff();
// report to console postgc heap size
memwatch.on('stats', function(d) {
console.log("postgc: current_base: %s",d.current_base);
// Take the second snapshot and compute the diff
var diff = hd.end();
console.log(util.inspect(diff.after.size,true,10));
hd = new memwatch.HeapDiff();
});
memwatch.on('leak', function(d) {
console.log("LEAK:", d);
throw new Error('leak detected');
});
// also report periodic heap size (every 10s)
setInterval(function() {
console.log("heap: %s",process.memoryUsage().heapUsed);
},5000);
@vicapow
Copy link
Author

vicapow commented Jul 17, 2012

it turns out this is a bug in memwatch not node :P

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment