There is a client that connects to a socket, communicates with the nodejs server, and disconnects. With each reconnect, the server starts to have more and more memory, eats up 1.5 Gb of RAM per month (under the conditions of the task it’s impossible for the daemon to fall, so restarting is not an option)

var _io = io.of('/signal') _io.on('connection', function (client) { var geoData, clientIp, onDisconnect = function () { client.removeListener('get_last', onGetLast); client.removeListener('login', onLogin); client.removeListener('logout', onLogout); client.removeListener('is_logged', onIsLogged); client.removeListener('disconnect', onDisconnect); delete onGetLast; delete onLogin; delete onLogout; delete onIsLogged; delete onDisconnect; delete geoData; delete clientIp; }, onGetLast = function () { config.DEBUG && console.log('on `get_last`'); if ('guests' === client.room) { client.emit('receive_signals', DATA.lastSignalsTime.slice(-10)); } else if ('logged' === client.room) { client.emit('receive_signals', DATA.lastSignals.slice(-10)); } }, onLogin = function (code) { config.DEBUG && console.log('on `login`', code); if (code === config.loginCode) { client.leave('guests'); client.join('logged'); client.room = 'logged'; //output = signals; } }, onLogout = function () { config.DEBUG && console.log('on `logout`'); client.leave('logged'); client.join('guests'); client.room = 'guests'; }, onIsLogged = function () { config.DEBUG && console.log('on `is_logged`'); var result; if ('logged' === client.room) { result = 1; } else { result = 0; // room 'guest' } client.emit('is_logged_res', result); }; clientIp = 'production' === config.NODE_ENV ? _.getClientIp(client) : '128.101.101.101'; geoData = DATA.mmdb.countries.getGeoDataSync(clientIp); client.room = 'guests'; client.join('guests'); client.on('disconnect', onDisconnect); client.on('get_last', onGetLast); //check if clients country is valid if (!geoData || !('object' === typeof geoData.country && null !== geoData.country && 'string' === typeof geoData.country.iso_code && -1 === DATA.invalidCountries.indexOf(geoData.country.iso_code)) ) { return; } client.on('login', onLogin); client.on('logout', onLogout); client.on('is_logged', onIsLogged); }); 
  • Eats 1.5 GB of RAM ... and then what? Eating more? Is falling? - D-side
  • It continues to work normally, does not fall, but continues to eat, with each reconnect. I did not find a normal profiler, which will indicate where exactly the data remains, only through the node-inspector I see what I eat during a reconnect and that's it. - Andrey
  • This is odd, as far as I know, the V8 has a default consumption limit of 1.4 GB heap. - D-side
  • even 1.5 GB is a lot like for a server that, after a load, serves the 1st user, and takes up memory as if a couple of thousand users. Maybe I somehow provoke this leakage in my code, do not clear something? - Andrey
  • Not the fact that it is generally a leak, and not a feature of the garbage collector. Check out how to reduce the limit. - D-side

1 answer 1

Thank you, Button

Solved the problem by adding a parameter

 --max_old_space_size 

and at the event disconnect , manually delete the events hung on the client by the method

 socket.removeListener('event_name', onEvenCallback);