2

I wrote a simple node server:

var http = require('http');

var server = http.createServer();

var i = 0;
var onRequest = function(req, res){
    res.write('test ' + (++i));
    res.end();
    console.log(i);
};

server.on('request', onRequest);

server.listen(8080);

When I started the server, node was using 5.8 MB of memory, But after serving 100,000 requests, it had memory usage of 21.5 MB.

Will this memory be deallocated at some point (when)?

I'm sure there's no memory leak in the script, it's too simple.

I'm testing on windows 8, localhost.

4
  • what version of Node and V8 are you using? Commented Jun 11, 2013 at 16:16
  • I'm using node V0.10.0, so not quite the latest. I don't know how to check which version of V8 it uses, but it was unmodified. Commented Jun 11, 2013 at 16:24
  • I updated my answer with how to check the V8 version in node Commented Jun 11, 2013 at 16:44
  • I was previously using v8 3.14.5.8, and now since upgrading to node 0.10.10, I have v8 3.14.5.9 Commented Jun 11, 2013 at 17:20

1 Answer 1

3

In the latest version of Node, this memory should be deallocated at some point by V8 and this might be done incrementally.

In previous versions of Node/V8 deallocation was more aggressive and was not done incrementally. The latter wasn't so good for performance.

Have a listen to this http://nodeup.com/fortyfive

You can check the version of V8 node is using like this:

node -e "console.log(process.versions.v8)"

This post will give you plenty of pointers to check for memory leaks in Node.js https://hacks.mozilla.org/2012/11/tracking-down-memory-leaks-in-node-js-a-node-js-holiday-season/

You can also force GC as detailed here: http://simonmcmanus.wordpress.com/2013/01/03/forcing-garbage-collection-with-node-js-and-v8/

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, It seams upgrading to the latest node/v8 has fixed the problem. After running the same test again, it finished at 13 MB, I could also see the memory fluctuating while running

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.