socketio / socket.io

Realtime application framework (Node.JS server)
https://socket.io
MIT License
61.09k stars 10.11k forks source link

Memory Leak with Broadcast #299

Closed giggsey closed 10 years ago

giggsey commented 13 years ago

This code seems to be leaking memory in String.Array (found via v8-profiler + node-inspector).

$ node -v v0.4.8 Socket.io-node version: Latest master (a2f8fb4970371d709aba)

Forgot to mention:

Only seems to happen:

var port = 8082;

var io = require('./Socket.IO-node').listen(port);
var profiler = require('v8-profiler');

io.set('log level', 2);

process.on('uncaughtException', function(err) {
  console.log(err);
});

var connections = 0;

io.sockets.on('connection', function(client){ 
  connections++;
  client.on('disconnect', function(){ connections--; client = null;}) 
});

process.on('exit', function () {
    console.log('Exiting');
});

function update() {
    if (connections > 0)
    {
        io.sockets.json.send( { test: 0, testing: 1 });
    }
}

setInterval(update,250);

setInterval(function() {
    console.log("Number of connections: " + connections);
},1800000);
gametbt commented 13 years ago

Hi, How does your client look?

giggsey commented 13 years ago

I can't remember exactly, but it's similar to:

        socket = io.connect("http://" + monitorip + ":8081" ); 
        socket.on('connect', function(){ $("#status").html('Connected'); $("#status").css('color','green');  }) 
        socket.on('message', function(ev){ 
$.each(ev,function(i,e) {
    console.log(e);
});
        socket.on('disconnect', function(){ $("#status").html('Disconnected'); $("#status").css('color','red'); }) 
dvv commented 13 years ago

@giggsey: please, try io.sockets.json.send( { test: 0, testing: 1, payload: 'lenghtystiiiiiing-4096-random-octets-to-bite-caching' });

What is the amount of the leak then? TIA

giggsey commented 13 years ago

@dvv: I've added a random() to a random variable in the json, and set it to take a heap snapshot every second. Not sure if my JavaScript function is good enough, but it is generating random crap.

When it was giving 4096 characters, memory was increasing by around 32kb/s. When I reduce it to 1 character, it increases by just under 500/s.

Both times, the count column, is increasing by +4, which sort of makes sense, as it's sending 4 messages per second.

setInterval(function() {
    profiler.takeSnapshot('snappy');
},1000);

function random()
{
    var text = "";
    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    for( var i=0; i < 4096; i++ )
        text += possible.charAt(Math.floor(Math.random() * possible.length));

    return text;
}
dvv commented 13 years ago

Thanks! So it do depend on the size of message being sent, i suppose. This makes me think we don't cleanup sent messages.

dvv commented 13 years ago

What transport do you use (websocket, flashsocket, xhr-polling, ...)? Couldn't you make the same tests for another transport?

giggsey commented 13 years ago

@dvv: I'm using websocket (Chrome stable), but also tried it using xhr-polling + jsonp-polling, both of which leak by the same amount. I couldn't get the htmlfile working, so I don't know about that.

giggsey commented 13 years ago

I think this issue is resolved with 7257e1ec3688287e752f (possibly before).

Edit: It still leaks memory in release 0.7.6.

giggsey commented 13 years ago

7257e1e seems to still be leaking memory. I had my full version running for roughly 23 hours, and it had used 300-400MB RES.

darkyen commented 12 years ago

Does it still leak memory ?

rase- commented 10 years ago

@giggsey still relevant in the latest version?

giggsey commented 10 years ago

No longer using socket.io.

Using engine.io with another project in front.