I just finished developing my first node.js application, and now I'm testing it on my VPS. As I watched the resource usage of the "node" process, I noticed an increase in memory usage when a page is requested (especially some). In particular, if the requested page is a static page, the increase is minimal. If the requested page is / admin, the increase may be 1 Mb! Of course, when / admin is requested, my server does more than serve the static page. It connects to mongodb, it performs 4 "find", it binds the results to the html template using bind . Now what is the problem? This used memory will never be released! So I thought there was a logical error in my code, but then I made another test much more interesting.
Consider this very simple nodejs server:
var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(3000, 'my_public_ip');
if I try to make several requests using the browser (just holding f5 for one minute), the memory usage grows slowly, and the memory used by the process will never be released even after a long time and after closing the browser. Now, it is likely that there is some error in my / admin code (1 mb of used memory and never released for every request, it is very high!), But I think itβs very strange that the memory used by the simple script above will be never released! What do you think about this? Is there any way to avoid this?
Also (on my real server) I used memwatch as follows:
var memwatch = require('memwatch'); memwatch.on('leak', function(info) { console.log(info); process.exit(1); });
If I execute several requests with a browser, after about 10 seconds I will do it, the process will end, and this will be an error:
{ start: Wed Nov 26 2014 08:21:07 GMT-0500 (EST), end: Wed Nov 26 2014 08:22:04 GMT-0500 (EST), growth: 4775624, reason: 'heap growth over 5 consecutive GCs (57s) - 287.65 mb/hr' }
What does it mean? This seems to be related to the garbage collector! I know that it would be better to insert my / admin code here, but the fragment is very long and associated with global variables, so it is impossible to understand without a copy of 200 lines: D. If you need more information, I will give you!