Skip to content

Instantly share code, notes, and snippets.

@Chunlin-Li
Created March 5, 2016 08:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Chunlin-Li/20ca5aa62cafed15d31b to your computer and use it in GitHub Desktop.
Save Chunlin-Li/20ca5aa62cafed15d31b to your computer and use it in GitHub Desktop.
reduplicate node-inspetor network agent bug: injection of network agent cause EventEmitter memory leak.
'use strict';
// use node-debug to run client.js and observe the memory useage.
var http = require('http');
var keepAliveAgent = new http.Agent({keepAlive: true, maxSockets: 500});
var list = [];
for (var i = 0; i < 500; i++) {
list.push("hello world");
}
var data = list.join('');
list = null;
setInterval(function(){
var result = {};
var clientReq = http.request({
host: '127.0.0.1',
port: 4444,
path: '/',
agent: keepAliveAgent,
headers: {
'Content-Length': data.length
}
}, function(resp) {
result['body'] = [];
resp.on('data', function(chunk){
result['body'].push(chunk);
});
resp.on('end', function(){
result['body'] = Buffer.concat(result['body']);
})
});
clientReq.end(data);
clientReq.on('error', function(err){
console.error('error ', err, err.stack);
})
}, 100);
setInterval(function(){
console.log('## MEM ', process.memoryUsage());
}, 1000);
'use strict';
// use node to run server.js as normal.
var http = require('http');
var list = [];
for (var i = 0; i < 500; i++) {
list.push("node-inspector/node-inspector")
}
var data = list.join('');
list = null;
var server = http.createServer(function(req, res){
var body = [];
req.on('data', function(chunk){
body.push(chunk);
});
req.on('end', function(){
body = Buffer.concat(body);
setTimeout(function(){
res.writeHead(200, {'Content-Length': data.length}, 'OK');
res.write(data);
res.end();
}, 100);
});
});
server.listen(4444);
setInterval(function(){
console.log('## MEM ', process.memoryUsage());
}, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment