Skip to content

Instantly share code, notes, and snippets.

@danypype
Last active August 29, 2015 14:07
Show Gist options
  • Save danypype/a2c46da944a0eeb96b7f to your computer and use it in GitHub Desktop.
Save danypype/a2c46da944a0eeb96b7f to your computer and use it in GitHub Desktop.
Node.js' net socket memory leak?
{
"dependencies": {
"heapdump": "^0.2.10"
}
}
var net = require("net")
, heapdump = require("heapdump");
function onConnection (client) {
client.on("data", function (data) {
client.end();
});
};
function onListen () {
var socket = net.connect({host: "127.0.0.1", port: 3000});
socket.bigBuffer = new Buffer(50000000); //50MB Buffer
heapdump.writeSnapshot(__dirname + "/initial.heapsnapshot", function writeInitialSnapshot () {
console.log("Wrote initial heap snapshot");
socket.write("data");
});
socket.on("close", function () {
writeEndSnapshot();
});
};
function writeEndSnapshot () {
setTimeout(function () {
console.log("Running GC");
gc();
heapdump.writeSnapshot(__dirname + "/final.heapsnapshot", function writeFinalSnapshot () {
console.log("Wrote final heap snapshot");
});
}, 1000);
};
net.createServer(onConnection).listen(3000, onListen);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment