Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory leaks using socket.io

I've found that sockets are not fully destroyed in socket io server side when manually disconnecting them. I've found this topic on github useful. While I'm looking for some variable-links that prevent GC from cleaning sockets, I'm asking a question here.

If anyone here encountered the same problem, this would be much help.

the code that does not work:

socket.on('disconnect', function(){
    socket.removeAllListeners();
});

///...................

socket.disconnect();

Workaround that, however, uses restricted library fields:

delete io.sockets[url];
io.j = [];
like image 451
Dan Avatar asked Mar 07 '12 07:03

Dan


People also ask

Does Socket.IO use a lot of memory?

Socket.IO requires a lot of memory which is a potential problem. SockJS does not scale well at all and does not manage the same level of concurrency as the other two.

Is Socket.IO better than WebSocket?

Key Differences between WebSocket and socket.ioIt provides the Connection over TCP, while Socket.io is a library to abstract the WebSocket connections. WebSocket doesn't have fallback options, while Socket.io supports fallback. WebSocket is the technology, while Socket.io is a library for WebSockets.

How many users Socket.IO can handle?

As we saw in the performance section of this article, a Socket.IO server can sustain 10,000 concurrent connections.

What causes node memory leaks?

Closures, timers, and event handlers can often create memory leaks in Node. js applications. Let's look at a piece of code from the Meteor team explaining a closure that leads to a memory leak. It leads to a memory leak as the longStr variable is never collected and keeps growing in memory.


1 Answers

actually, this is working as intended, when you disconnect a socket you simply state you're not expecting to receive any more data from that socket right now, to actually destroy the socket you basically do the delete socket action. Use this on the disconnect event, ie:

socket.on('disconnect', function(){
    delete socket; 
})

you can also do this on the io.sockets.sockets Object on an external function:

function deleteSocket(socketID){
    delete io.sockets.sockets[socketID];
}
like image 198
Gonçalo Vieira Avatar answered Nov 03 '22 05:11

Gonçalo Vieira