-
-
Save Filirom1/2587413 to your computer and use it in GitHub Desktop.
mem leak ? timeout and onError with a working HTTP Server
This file contains hidden or 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
node --expose-gc leak.js | |
webkit-devtools-agent started on 0.0.0.0:1337 | |
We should do 18 requests | |
3MB used | |
Done: 0/18, not yet GC: 0 | |
Got error: undefined | |
Got error: undefined | |
Got error: undefined | |
Got error: undefined | |
Got error: undefined | |
Got error: undefined | |
Got error: undefined | |
Got error: undefined | |
Got error: undefined | |
Got error: undefined | |
Got error: undefined | |
Got error: undefined | |
Got error: undefined | |
Got error: undefined | |
Got error: undefined | |
Got error: undefined | |
Got error: undefined | |
Got error: undefined | |
96MB used | |
Done: 18/18, not yet GC: 18 | |
all requests done, memory should be collected by now ? | |
96MB used | |
Done: 18/18, not yet GC: 18 | |
all requests done, memory should be collected by now ? |
This file contains hidden or 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'), | |
weak = require('weak'), | |
done = 0, count = 0, countGC = 0, todo = 18; | |
var agent = require('webkit-devtools-agent'); | |
console.log('We should do '+ todo +' requests'); | |
// show memused and progress while running | |
setInterval(status, 5000);status(); | |
var http = require('http'); | |
http.createServer(function (req, res) { | |
res.writeHead(200, {'Content-Type': 'text/plain'}); | |
res.end('Hello World\n'); | |
}).listen(3000, '127.0.0.1', getall); | |
function getall() { | |
for(var i = 0; i < todo; i++) { | |
(function(){ | |
// creates a big buffer | |
var megabig = new Big(); | |
weak(megabig, afterGC); | |
// this should not trigger a memory leak but it does when using http.get | |
function cb(e) { | |
if(e) console.log("Got error: " + e.message); | |
done+=1; | |
return megabig; | |
} | |
// using setTimeout instead of http.get shows no mem leak | |
// setTimeout(cb, 10); | |
http.get({ | |
hostname: 'localhost', | |
pathname: '/index.html?' + Math.random(), | |
port: 3000, | |
//agent: false, | |
protocol: 'http:' | |
}, cb).on('error', function(e) { | |
cb(e); | |
}).setTimeout(10, function(){ | |
console.log('nothing here') | |
}) | |
})() | |
} | |
} | |
function Big() { | |
count ++; | |
this.yo = (new Buffer(1 * 1024 * 1024 * 3)).toString(); | |
} | |
function afterGC(){ | |
countGC ++ | |
} | |
function status() { | |
gc() | |
console.log((process.memoryUsage().heapUsed/1024/1024).toFixed() + 'MB used'); | |
console.log('Done: '+ done +'/'+ todo + ', not yet GC: ' + (count - countGC)); | |
if (done === todo) { | |
console.log('all requests done, memory should be collected by now ?') | |
} | |
} | |
setTimeout(function(){}, 1000000000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment