Socket.io: Socket.io memory doesn't drop of after clients disconnect

Created on 2 Feb 2016  Â·  11Comments  Â·  Source: socketio/socket.io

I use the most simple example

var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var fs = require('fs');

app.listen(1994);

function handler(req, res) {
    res.end('socket.io start')
}

io.on('connection', function(socket) {});

I use socket.io-client, create a 4k connections
Then I closed the client, find memory doesn't drop.

socket.on('disconnect', funtion() {
    delete io.sockets[socket.id];
    delete io.sockets.sockets[socket.id];
});

heapUsed decreased slightly
rss and heapTotal without any change

SOS

The memory is cleaned up by the garbage collectors periodically.
But I waited for half an hour, still no change.
Can I only use global.gc() to forcibly reclaim memory it?

@nuclearace

Closed due to inactivity

Most helpful comment

lol was this fixed? i'm scared to use sockets.io because i keep hearing about this issue

All 11 comments

It seems that once a client connects, it never gets deleted from socket.io , or socket.io itself a memory leak problem .

@TOP-Chao I was stress testing socket.io and realized that issue as well.

After investigating, it seems that when global.gc() is called, it reduces the memory.
Although, it seems the memory seems to get reduced slowly.

The first image is not using global.gc()

screen shot 2016-04-16 at 00 30 05

The second image is using global.gc() every minute

screen shot 2016-04-16 at 00 30 28

The code:

server.js

var http = require('http');
var io = require('socket.io');
const PORT = 3000;
var process = require('process');
var server = http.createServer(function(req, res){
    res.writeHead(404, {'Content-Type': 'text/html'});
    res.end('<h1>Aw, snap! 404</h1>');
});

server.listen(PORT);
io = io.listen(server);

var connectedCount = 0;
console.log('start');

logMemory();

// Add a connect listener
io.on('connection', function(socket) {

    //console.log('Client connected.', connectedCount);
    connectedCount++;

    // Disconnect listener
    socket.on('disconnect', function() {
        connectedCount--;
//        console.log('Client disconnected. Total Connections: ', connectedCount);
        if(!connectedCount) {
            logMemory();
        }
    });

});

setInterval(function(){
    logMemory();
}, 60000);

function logMemory() {
    var memory = process.memoryUsage();
    console.log((new Date()), 'RSS ',memory.rss, ', Heap Used ', memory.heapUsed, ' Heap Total ', memory.heapTotal);
}

client.js

var io = require('socket.io-client');
const CLIENT_COUNT = 100;
const PORT = 3000;

for(var i = 0; i < CLIENT_COUNT; i++) {
    newSocketClient(i);
}

function newSocketClient(idx) {
    var socket = require('socket.io-client')('http://127.0.0.1:'+PORT, {forceNew: true});
    socket.on('connect', function(){
        console.log('connected', idx);
        socket.disconnect();
        console.log('disconnected', idx);
    });
}

In a production environment is not recommended use global.gc (),initially I was using global.gc () release the memory, but this method makes socket connection is unstable, resulting in dropped calls. Socket.io will take the initiative to release memory because the inert recycling and reuse connections, leading to see the memory has not been released.

The memory is cleaned up by the garbage collectors periodically.

To me, memory is only** reclaimed if/when the OS needs it (if not triggered manually with global.gc()). So that doesn't seem like a leak. Or am I wrong?

** that's obviously more complex than that, but you get the picture :angel:

I am running 2 nodejs servers with socket io. both of them are proxied equally by nginx for load . so individual socket server is handling around 10K active connections . And process memory starts increasing from 300 MB initially to 3000MB finally within 2 days . I am using socket io 1.3.7 in production with node 4.4

Similar issue here. I clean up unused sockets with:

sckt.removeAllListeners();
sckt.disconnect(true);

But memory consumption keeps growing. Development server with socket.io 1.4.8 and node 0.10.33.

2016-09-13 10:37 GMT+02:00 niranjanMeteor [email protected]:

I am running 2 nodejs servers with socket io. both of them are proxied
equally by nginx for load . so individual socket server is handling around
10K active connections . And process memory starts increasing from 300 MB
initially to 3000MB finally within 2 days . I am using socket io 1.3.7 in
production with node 4.4

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/socketio/socket.io/issues/2427#issuecomment-246613805,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AF_1Xuz7Q-JCwjj4OvtWtTT1YLSsIlHsks5qpmDigaJpZM4HRQLl
.

That issue was closed automatically. Please check if your issue is fixed with the latest release, and reopen if needed (with a fiddle reproducing the issue if possible).

lol was this fixed? i'm scared to use sockets.io because i keep hearing about this issue

tbh it seems like the problem is still there... I'm trying to find a memory leak in my backend app but everything points to something inside of socket.io

This is not good. Issue still seems to exist. Definetly a very critical bug right here.

ping

Was this page helpful?
0 / 5 - 0 ratings