Search code examples
node.jsmemory-leakssocket.iopm2

Socket.io emit causing steady memory increase


In my program to emit some values to all the sockets, using Socket.io with Redis Adapter, there is a memory leak issue.

Code Snippet :

....node modules require statements here....

const server = http.createServer(app);
io = socketIo(server);
io.adapter(socketIoRedis({ host: redis_host, port: redis_port }));

for(....) {
    data_out = {A:10, B:20, id:'1234'} //Just sample data, it changes by loop
    data_ref = data_out[id]
    io.emit(data_ref, JSON.stringify(data_out));
}    

In the server, when running the program using pm2 and monitoring the memory(pm2 monit), the memory keeps increasing constantly. And the server just hangs reaching the maximum memory size available.

How can the memory increase be avoided? This is happening with no connected clients too.


Solution

  • This is a similar issue I'm facing, and for me the issue is with redisAdapter. Please check the adapter implementation, and make sure that's not leaking memory, take a heap snapshot, load it in chrome and see if you can find out the leaker. For me it was redisCluster creation using ioredis.