High memory usage (or memleak?)
Hi all!
I created a small example for the memory leak my ws (or WebSocket.IO) server has.
This small server only accepts WebSocket connections and sends one message. Client count and memory usage is displayed every 2 seconds.
The client open 10,000 connections, then every minute 2,000 new connections are openend and 2,000 are closed, so the count stays at about 10,000.
https://gist.github.com/2152933

The graph is the output of server.js (run on a EC2 "large" instance with node 0.6.13 and ws 0.4.9).
- Server startup: RSS is about 13 MB (0 clients)
- When the first 10,000 clients are connected, RSS = 420 MB
- During the next 10 minutes clients come and go (see client.js), RSS grows to 620 MB
- heap usage stay stable
- Client is stopped =(> RSS falls to 220 MB waited 1 minute for GC)
- After 1 minute, client ist started again => RSS jumps to 440 MB (10,000 clients)
- During the next minutes, RSS grows again up to 630 MB (after 10 minutes)
- Client is stopped => RSS falls to 495 MB
- When I start the client again, RSS usage seems stable (at least compared to the two runs before).
Questions:
- 400 MB for 10.000 clients (with no data attached) is much. But I see JS as an interpreted language is not that memory optimized than C. BUT, why does opening and closing 20.000 connections (during the 10 minute period) consume another 200 MB?
- The process is at about 30% CPU, so the GC has the chance to kick in (and, ws uses nextTick, so the GC really has a chance)
- Why is he GC unable to free the memory after the second run? Can't be the Buffer/SlowBuffer problem (fragmented small Buffers in different 8k SlowBuffers), as there are no Buffers used anymore...
- Why does the RSS usage remain pretty stable after the first two runs?
The effect is the same (but slower) with only 1,000 client connections per minute, but things get even worse when I don't stop the client after 10 minutes. Our production server runs with about 30-40% CPU constantly (30k connections), 1-5% at night (1-2k connections), but is never completely idle. The growing of the RSS usage never seems to stop.
On the production server, RSS grows until node crashes (0.4.x crashes at 1 GB) or the process gets killed by the system (0.6 supports more than 1 GB).
I'll try two things tomorrow:
- The same setup with Node 0.4 (as 0.4 seems much better in regards of memory consumption than 0.6), and
- with different WebSocket libraries, e.g. "websock", which is not 100% stable but only consumes one third (still leaking though), and a variant of WebSocket.IO with the original Socket.IO (not ws!) hybi parsers, see my "nows" branch.
The setup is a default Amazon EC2 large instance, so this must be an issue for anyone who runs a WebSocket server using ws (or must likely also WebSocket.IO with ws receivers) with some traffic. I refuse to believe Node is not capable of serving this.
Or am I missing something?
Nico
Update: the 60-minute version is still running, I'll post the results tomorrow. RSS seems to stabilize around 900 MB if I don't stop the clients. Come on, 900 MB (500 of which is garbage!)?!
This is 60 minutes with 1 minute pause after each 60 minute run:

Maybe the "leak" is no leak but very very high memory consumption...
Great report, Nico. You are onto something when you say that each connection consumes a lot of memory. I pool rather aggressively, in order to ensure high speed. The 'ws' library is written to be as fast as possible, and will at present outperform all other node.js websocket libraries, for small data amounts as well as large.
That said, I will deal with this, to push the memory use down. An alternative is to introduce a configuration option which allows the user to favor either speed or memory consumption.
In your test above, are you sending any data for the 10k clients? How large would you say the largest packet is?
This is WebSocket.IO 10-minute (like the very first chart) with Node 0.6. It's even worse than ws, as it never returns the memory during idle phases.

@einaros The client ist the one in the Gist – the client sends 10k at the beginning of each connection.
A configuration option would be amazing! I'll observe the production server, which is running Node 0.6 right now, if memory usage rise ends somewhere ;)
I understand that much memory is needed to ensure fast connection handling, however the memory should be freed after some (idle?) time...
For 10k users sending an initial 10k (in a single payload), the consumption would be at least 10.000 * 10k.
I agree that the pools should be released. Adding a set of timers to do that without negatively affecting the performance for high connection counts will require thorough consideration before being implemented.
Thank you for your continued work on this. We'll get to the bottom of it :)
Well, I open 10,000 clients at the startup, and then open and close 2,000 random clients every minute. So there should be 10,000 clients all the time, but about 20,000 "connection" events (and thus, 20,000 initial 10k messages received) during the 10 minutes.
Isn't the pool released after a client disappers?
Each pool is released as the client closes, yes.
@einaros then it must be something different (or poor GC), because after each 10 minute period all clients are disconnected, so there should be no buffer left (so even the shared SlowBuffers should be freed by V8)...
For comparison, this is WebSocket.IO without ws (backported the original Socket.IO hybi modules) and without client tracking (only clientsCount):

GC seems to run from time to time, but does not catch everything. Maybe interesting for @guille
@nicokaiser, I wrote the "old" websocket.io hybi parsers as well, so this is all my thing to fix in either case :)
I've found a couple of issues now, which takes care of leaks visible through the v8 profiler's heap dump. With my recent changes, I can spawn up 5k clients to a server, send some data, then disconnect the users, and the resulting heap dump will look more or less as it did before any clients connected.
I can't yet get the rss to fall back to a reasonable size, though, but I'm looking into it.
And you are not leaking any buffers? As they are shows in RSS and not in the V8 heap
On Friday, March 30, 2012 at 10:29 AM, Einar Otto Stangvik wrote:
I've found a couple of issues now, which takes care of leaks visible through the v8 profiler's heap dump. With my recent changes, I can spawn up 5k clients to a server, send some data, then disconnect the users, and the resulting heap dump will look more or less as it did before any clients connected.
I can't yet get the
rssto fall back to a reasonable size, though, but I'm looking into it.
Reply to this email directly or view it on GitHub: https://github.com/einaros/ws/issues/43#issuecomment-4838425
@3rd-Eden, for js Buffer/SlowBuffer instances, they should show up in the heap dump. I figured it may be due to allocations in my native extensions, but the results were exactly the same with those disabled.
@einaros that sounds great! That's exactly the kind of issues I get – growing rss that don't fall back (the difference between heapTotal and rss grow until the process crashes.
I can't help but think that the remaining issues are caused by something within node leaking native resources, probably triggered by something I am doing. And I'm guessing that something is the buffer pool. Now the question is how the buffers are handled / used wrong, and how they wind up leaking native allocation blocks.
On the production system, I disabled BufferPools (by using new Buffer - like BufferPool would do with initial size 0 and no growing/shrinking strategy) and validation (by, well, not validating utf8), but this does not solve the problem.
So it must be either the buffertool (mask) (I cannot test the JS version from the Windows port, as I'm using Node 0.4) or something else...
@nicokaiser, I'll have another look at the bufferutil and see what I can find.
That could certainly be the issue here, as there also countless reports that socket.io on 0.4 leaks less memory than socket.io on node 0.6. So it could be that node is leaking buffers somewhere as well.
On Friday, March 30, 2012 at 10:48 AM, Einar Otto Stangvik wrote:
I can't help but think that the remaining issues are caused by something within node leaking native resources, probably triggered by something I am doing. And I'm guessing that something is the buffer pool. Now the question is how the buffers are handled / used wrong, and how they wind up leaking native allocation blocks.
Reply to this email directly or view it on GitHub: https://github.com/einaros/ws/issues/43#issuecomment-4838641
@nicokaiser, I disabled my native extensions again, to no avail.
Will go pick a fight with the buffers now.