8

Quted from What do the return values of node.js process.memoryUsage() stand for? RSS is the resident set size, the portion of the process's memory held in RAM (how much memory is held in the RAM by this process in Bytes) file size of 'text.txt' used in example is here is 370KB (378880 Bytes)

var http    = require('http');
var fs      = require('fs');
var express = require('express');
var app     = express();

console.log("On app bootstrap = ", process.memoryUsage());

app.get('/test', function(req, res) {

    fs.readFile(__dirname + '/text.txt', function(err, file) {
        console.log("When File is available = ", process.memoryUsage());
        res.end(file);

    });

    setTimeout(function() {
        console.log("After sending = ", process.memoryUsage());
    }, 5000);

});

app.listen(8081);

So on app bootstrap: { rss: 22069248, heapTotal: 15551232, heapUsed: 9169152 } After i made 10 request for '/test' situation is:

When File is available =  { rss: 33087488, heapTotal: 18635008, heapUsed: 6553552 }
After sending          =  { rss: 33447936, heapTotal: 18635008, heapUsed: 6566856 }

So from app boostrap to 10nth request rss is increased for 11378688 bytes which is roughly 30 times larger than size of text.txt file.

I know that this code will buffers up the entire data.txt file into memory for every request before writing the result back to clients, but i expected that after the requst is finished occupied memory for 'text.txt' will be released? But that is not the case?

Second how to set up maximum size of RAM memory which node process can consume?

Community
  • 1
  • 1
Srle
  • 10,366
  • 8
  • 34
  • 63
  • 1
    so i commented out `fs.readFile` part and these were logs , 1st req `After sending = { rss: 31162368, heapTotal: 18862880, heapUsed: 10961672 }` 10th req `After sending = { rss: 31596544, heapTotal: 18862880, heapUsed: 11393584 }` which is like 424kb , quite wired , am also looking for an answer . EDIT : found this http://stackoverflow.com/questions/16441601/node-express-memory-increase-on-every-request – Aishwat Singh Jan 28 '16 at 05:07

2 Answers2

5

In js garbage collector does not run immediately after execution of your code. Thus the memory is not freed immediately after execution. You can run GC independently, after working with large objects, if you care about memory consumption. More information you can find here.

setTimeout(function() {
    global.gc();
    console.log("After sending = ", process.memoryUsage());
}, 5000);

To look at your memory allocation you can run your server with v8-profiler and get a Heap snapshot. More info here.

Community
  • 1
  • 1
Alexey B.
  • 11,965
  • 2
  • 49
  • 73
1

Try running your example again and give the process some time to run garbage collection. Keep an eye on your process' memory usage with a system monitor and it should clear after some time. If it doesn't go down the process can't go higher in memory usage than mentioned below.

According to the node documentation the memory limit is 512 mb for 32 bit and 1 gb for 64 bit. They can be increased if necessary.

Alex Carlos
  • 882
  • 5
  • 7