Memory Leak in Express.js with EventSource

I think I am running into a memory leak with an Express app when connecting x number of EventSource clients to it. After connecting the clients and sending them x messages and disconnecting them, my Express app only releases a small amount of the allocated Heap/RSS.

To confirm this I saved a Heapdump when starting the server and one after connecting 7,000 clients to it and sending x messages to each client. I waited for a while to give the GC a chance to clean up before taking the heap snapshot.

To compare these heap snapshots I loaded them in the Chrome Developer Tools Profile view and chose the "Comparison" mode.

My questions are:

1) How to interpret these numbers? (For reference see the attached heap snapshot screenshot.)

2) For instance it looks like that the Socket objects doesn't almost free any objects at all, is that correct?

3) Can you give me more tips to investigate the problem?

Heap Snapshot Express.js app

As to my Comment...

Javascript can't clear up a section of memory should anything be pointing at it about 2 years ago some one found an exploit and it was quickly closed like that and it works like this

var someData = ["THIS IS SOME DATA SAY IT WAS THE SIZE OF A SMALL APPLICATION"];
var somePointer = someData[0];
delete someData;

they then injected an application into somePointer as it was a reference to a memory location when there was no data now. hey presto you injected memory.

So if there is a reference like above somePointer = someData[0]; you cant free the memory until you delete someData so you have to remove all references to anything you want cleaning in your case ALL_CLIENTS.push(this); on line 64 is making your system memory accessible through ALL_CLIENTS, so what you can do is

Line 157

_.each(ALL_CLIENTS, function(client, i) {
                    var u; // holds a undefined value (null, empty, nothing)
                    client.close();
                    //delete ALL_CLIENTS[i];
                    ALL_CLIENTS[i] = u;
                    ALL_CLIENTS.unused++;
                }); 

On another note this is not a memory leak a memory leak is say you had this server you close it if the memory did not free up after you exited it then you have a memory leak if it does clean the memory behind it's self it's not a leak it's just poor memory management

Thanks to @Magus for pointing out that delete is not the best thing you could use however i would never recommend that you implement a limiting structure but you could try

Line 27: ALL_CLIENTS.unused = 0;

Line 64:

var u;
if(ALL_CLIENTS.unused > 0){
    for(var i = 0; i < ALL_CLIENTS.length; i++){
        if(ALL_CLIENTS[i] == u){
            ALL_CLIENTS[i] = this;
            ALL_CLIENTS.unused--;
            i = ALL_CLIENTS.length;
        }
    }
}else{
    ALL_CLIENTS.push(this);
}

You could be free from memory leak and as a bonus avoid the garbage collector. All you got to do is object polling.

You could do something like

var clientsPool = new Array(1000);
var clientsConnected = [];

When a new client connects, you do

var newClient = clientsPool.pop();
//set your props here
clientsConnected.push(newClient);

That's an awesome way to avoid the Garbage Collector and prevent memory leak. Sure, there's a little more work to it and you will have to manage that carefully but it's totally worth for performance.

There's an awesome talk about it, here you go https://www.youtube.com/watch?v=RWmzxyMf2cE