Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodeJS when and how is the heap memory freed?

Tags:

node.js

In order to understand the memory usage pattern of NodeJS's V8 engine, I wrote a simple web program as shown below:

contents of server.js:
var http = require("http");

var server = http.createServer(function(req, res) {
   res.write("Hello World");
   res.end();
});
server.listen(3000);

When the program is launched using node server.js, the initial memory snapshot is as below: enter image description here

After I kept making multiple URL hits to this server, I could see a pattern of increased heap usage. To be more precise, for every 6 or 7 hits, there is an increase of 4K. I kept repeating the hits continuously for about 2 minutes, then this is the snapshot. enter image description here

I didn't see any eventual decrease in heap usage, even if I kept it idle without load.

My question is:

Is this a normal behavior, or there is a memory leak in nodeJS? Or, am I understanding or interpreting it incorrectly?

like image 388
Mopparthy Ravindranath Avatar asked Dec 04 '25 18:12

Mopparthy Ravindranath


1 Answers

Node uses V8 under the hood, so the answer to this question most likely applies: How does V8 manage its heap?

The code appears to be valid, so to test you could write a small application to repeatedly call your api and then examine Node's memory while running. Use this to help detect possible leakage (if there is any over 5 consecutive runs of the garbage collector): http://www.nearform.com/nodecrunch/self-detect-memory-leak-node/

like image 158
Jon Fast Avatar answered Dec 06 '25 07:12

Jon Fast



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!