Skip to content

Instantly share code, notes, and snippets.

@vvo
Created April 25, 2012 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save vvo/2488897 to your computer and use it in GitHub Desktop.
Save vvo/2488897 to your computer and use it in GitHub Desktop.
mem leak ?
require('webkit-devtools-agent')
var
http = require('http'),
done = 0, todo = 30, doing = todo;
console.log('We should do '+ todo +' requests');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(3000, '127.0.0.1', getall);
// show memused and progress while running
setInterval(status, 5000);status();
function getall() {
// creates a big buffer
var megabig = new Big();
// this should not trigger a memory leak but it does when using http.get
function cb() {
done+=1;
return megabig;
}
while(doing--) {
http.get({
hostname: 'localhost',
pathname: '/index.html?' + Math.random(),
port: 3000,
protocol: 'http:'
}, cb);
}
}
function Big() {
this.yo = (new Buffer(1 * 1024 * 1024 * 100)).toString();
}
function status() {
var
mem = process.memoryUsage(),
rss = (mem.rss/1024/1024).toFixed(),
heapUsed = (mem.heapUsed/1024/1024).toFixed(),
heapTotal = (mem.heapTotal/1024/1024).toFixed();
console.log('rss: %dMB, heapUsed: %dMB, heapTotal: %dMB',rss, heapUsed, heapTotal);
console.log('Done: '+ done +'/'+ todo);
if (done === todo) {
console.log('All requests done, memory should be collected')
}
}
@stefounet
Copy link

url & request modules seem to be unused, you should remove them

@vvo
Copy link
Author

vvo commented Apr 26, 2012

thx :)

@stefounet
Copy link

http.request + req.end() needs less memory ! see : https://gist.github.com/2503608
(but memory leak is still there ...)

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