Search code examples
node.jsgarbage-collectionwebsocketv8

Understanding object creation and garbage collection of a NodeJS WebSocket server


I'm using the ws module in nodejs for a web socket server, and the simplified version of the code is this:

var WebSocketServer = require('ws').Server
var wss = new WebSocketServer({port: 9001});

wss.on('connection', function (ws) {

    // is the ws object created here ?

  ws.on('message', function (message) {
    if (message[0] === '+') {
        ws.name = message.substring(1);
        console.log(ws.name+' connected');
    }
  });

  ws.on('close', function () {
    console.log(ws.name+' disconnected');
    // Will this ws object be deleted ?
  });
});

I would like to know:

  • On each event of a connection, is a ws object created ?
  • Does one of this object per connected client persist in RAM until it's garbage collected ?

And most importantly: If tons of clients disconnect and the ws objects keep lying around until they are garbage collected, when the garbage collection happens, my server could be locked for quite some time right ? Should I go through the trouble of storing these ws objects in another object, so I could use the delete keyword and remove them as soon as I get the close event ?

If I were to do so, my could would be somewhat like this:

var WebSocketServer = require('ws').Server
var wss = new WebSocketServer({port: 9001});

var websockets = {};

wss.on('connection', function (ws) {
  ws.on('message', function (message) {
    if (message[0] === '+') {
        ws.name = message.substring(1);
        websockets[ws.name] = ws; // Add to the object that stores ws objects
        console.log(ws.name+' connected');
    }
  });

  ws.on('close', function () {
    delete websockets[ws.name]; // Delete from the object
    console.log(ws.name+' disconnected');
  });
});

So, is it worth it ? Am I writing more garbage collection friendly code in the second snippet ? Will it help avoid locking up for a long time when a garbage collection happens ?

UPDATE: I'm sorry I just realised how stupid the second snippet of code is. I'm actually duplicating the amount of objects when I do websockets[ws.name] = ws... But the first part of the question still remains valid.


Solution

  • On each event of a connection, is a ws object created ?

    Yes.

    Does one of this object per connected client persist in RAM until it's garbage collected ?

    Yes, that's true for every JavaScript object.

    Should I go through the trouble of storing these ws objects in another object, so I could use the delete keyword and remove them as soon as I get the close event ?

    No. Using delete keyword won't free your memory. In fact it does nothing else then just removing the reference. Consider this example:

    var x = { };
    var y = { };
    var z = { };
    x.test = z;
    y.test = z;
    delete x.test;
    

    As you can see x.test is removed ( the object x no longer has .test attribute ) but z is not deleted at all, because y holds reference to z.

    And how do you think WS can work with multiple ws objects? It stores them somewhere internally. If you want to remove ws object, then simply do

    ws.on('close', function () {
        delete ws;
    });
    

    This will ensure that at some point the garbage collector will collect ws. On the other hand I think that WS already takes care of this internally, so you don't have to worry about it at all.

    Side note: There is no way to trigger garbage collector in JavaScript. But do not worry about it. If you reach your memory limit it will fire on its own.