Memory leak in Nodejs
This is my code
function Client() {
var _self = this;
this.connection = io.connect('http://127.0.0.1:2044/data/01', {
'reconnection': true,
'reconnectionDelay': 10000,
'reconnectionDelayMax': 15000,
'reconnectionAttempts': 0
});
this.connected = false;
this.connection.on('connect', _onConnect);
function _onConnect() {
_self.connected = true;
}
setInterval(go, 100);
function go(){
_self.connection.emit('NORMAL_DATA', {data: {}, _id: 0}, function(){});
}
}
? How are you detecting the leak
@terrywong2 please give us more information.
I'm investigating this on a similar issue, updated the above code to provide a little more info:
var memwatch = require('memwatch'),
util = require('util'),
heapdump = require('heapdump'),
io = require('socket.io-client');
var hd = new memwatch.HeapDiff();
memwatch.on('leak', function(info) {
console.error('LEAK! info: ' + JSON.stringify(info));
console.error(JSON.stringify(hd));
// memwatch
var diff = hd.end();
console.error(util.inspect(diff, true, null));
hd = new memwatch.HeapDiff();
// heapdump - have to cross compile
var file = '/tmp/myapp-' + process.pid + '-' + Date.now() + '.heapsnapshot';
heapdump.writeSnapshot(file, function(err){
if (err) console.error(err);
else console.error('Wrote snapshot: ' + file);
});
});
(function Client() {
var _self = this;
this.connection = io.connect('http://127.0.0.1:2044/data/01', {
'reconnection': true,
'reconnectionDelay': 10000,
'reconnectionDelayMax': 15000,
'reconnectionAttempts': 0
});
this.connected = false;
this.connection.on('connect', _onConnect);
function _onConnect() {
_self.connected = true;
}
console.log('rss, heapTotal, heapUsed');
setInterval(go, 100);
function go(){
var data = process.memoryUsage();
console.log(data.rss + ', ' + data.heapTotal + ', ' + data.heapUsed);
_self.connection.emit('NORMAL_DATA', {data: new Array(10000000).join('*'), _id: 0}, function(){});
}
})();
Notice that I amplified the leak by sending more data.
>>$ node leak.js
rss, heapTotal, heapUsed
32825344, 15539200, 4246800
43085824, 26587800, 14536224
53350400, 36604464, 24354432
...
1494024192, 1468987416, 1454078240
1504026624, 1479004080, 1464082392
And the leaks:
LEAK! info: {"start":"2016-05-03T17:11:37.000Z","end":"2016-05-03T17:11:39.000Z","growth":149559096,"reason":"heap growth over 5 consecutive GCs (2s) - -2147483648 bytes/hr"}...
LEAK! info: {"start":"2016-05-03T17:11:41.000Z","end":"2016-05-03T17:11:56.000Z","growth":950326848,"reason":"heap growth over 5 consecutive GCs (15s) - -2147483648 bytes/hr"}...
FATAL ERROR: JS Allocation failed - process out of memory
Aborted (core dumped)
The OP probably noticed his heap usage was increasing. Not an indicator by itself, but the large data here shows a leak, since some variable(s) still have references and can't be garbage collected. My guess is Buffers.
I also get a few heap dumps, here's the stats:

Here's we see the string leak. Here's where it traces to:

... so what appears to be happening is what I see in other socket.io implementations; if emit() is called without a something connected on the other side, data is queued indefinitely in Buffers until eventually you run out of memory. Which seems like dangerous default behavior. It would be nice for the library to have some sort of default limit which can be changed on construction, or optionally set to unlimited with lots of warnings.
For future readers: the client is indeed buffering packets until reconnection.
You can however control this behavior, please see the documentation here: https://socket.io/docs/v3/client-offline-behavior/#Buffered-events