Created
July 11, 2012 15:45
-
-
Save vicapow/3091278 to your computer and use it in GitHub Desktop.
leaking memory?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
it turns out this is a bug in memwatch not node :P