Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS / ExpressJS Memory Leak

I've a static ExpressJS Server like that:

var express = require("express"),
    app = express();

app.use(express.static(__dirname));

app.listen(1050);

When i start the server it uses 20MB of v8 heap. If i do a page reload every second the heap used grow continuously. After 4 hours it goes to 40MB of v8 heap used. The total v8 heap goes to 80MB and the RSS (total memory used by the process) goes to 130MB.

Why this simple and static server uses so much ram? It seems a memory leak. If i don't stop the page reload, the used memory keeps growing.

It's impossible to do larger projects if a simple static server like this uses too much ram.

NodeJS version: v0.10.21 ExpressJS version: 3.3.5

EDIT: I noticed that it's a problem with NodeJS, because i tried node-static instead of express, and while the used/total V8 heap stayed constant, the RSS memory used by nodejs continued to grow up.

Screen:
https://www.dropbox.com/s/4j5qs3rv2549dix/Screenshot%202014-03-20%2014.06.57.png https://www.dropbox.com/s/0c30ou8l3rv2081/Screenshot%202014-03-20%2014.07.54.png https://www.dropbox.com/s/5be1isk4v70qj8g/Screenshot%202014-03-20%2014.08.10.png
(Starts at 13:48)

like image 863
angelocala94 Avatar asked Mar 19 '14 13:03

angelocala94


2 Answers

Not sure if you are still in need of an answer but ill post for anyone else who might be having the same issues.

I was having this same exact problem and fixed it by using:

--max-old-space-size 5

This limits how much memory is held until it gets deleted by GC.

like image 137
phantoms Avatar answered Nov 17 '22 00:11

phantoms


To help the creators of NodeJS and Express potentially, try taking memory snapshots (done using chrome dev tools, use the url chrome://inspect ) this will allow you to see where your memory is allocated.

like image 25
Starystars67 Avatar answered Nov 16 '22 23:11

Starystars67