Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 420
roundrobin Avatar asked Jul 18 '14 23:07

roundrobin


People also ask

How do I resolve a memory leak issue in Node JS?

A quick way to fix Node. js memory leaks in the short term is to restart the app. Make sure to do this first and then dedicate the time to seek out the root cause of the memory leak.

Can you have memory leaks in JavaScript?

The JavaScript engine allocates memory when you create objects and variables in your application, and it is smart enough to clear out the memory when you no longer need the objects. Memory leaks are caused due to flaws in your logic, and they make way for poor performance in your application.

Can memory leak occur in stack?

Stack memory leaks occur when a method keeps getting called but never exits. This can happen if there is an infinite loop or if the method is being called with different data each time but the data is never used. Eventually, the stack will fill up and the program will run out of memory.


1 Answers

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

like image 70
Magus Avatar answered Oct 13 '22 19:10

Magus