Memory leaks using socket.io

I found that the sockets were not completely destroyed on the io socket side of the server when they were manually disconnected. I found this github theme useful. Although I'm looking for some variable references that prevent the GC from cleaning sockets, I ask a question here.

If someone here faced the same problem, that would be very helpful.

code that doesn't work:

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

A workaround that, however, uses the fields of a limited library:

 delete io.sockets[url]; io.j = []; 
+6
source share
1 answer

in fact, it works as intended, when you disconnect the socket, you simply declare that you do not expect to receive more data from this socket right now, in order to actually destroy the socket, which you basically execute in the delete socket action. Use this in case of shutdown, i.e.:

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

You can also do this on the io.sockets.sockets object by an external function:

 function deleteSocket(socketID){ delete io.sockets.sockets[socketID]; } 
+1
source

Source: https://habr.com/ru/post/910142/


All Articles