Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node process memory usage, resident set size constantly increasing

Tags:

node.js

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?

like image 436
Srle Avatar asked Mar 21 '15 17:03

Srle


People also ask

What is the default memory limit on a Node process?

Tuning the Garbage Collector In Node < 12 , it sets a limit of 1.5 GB for long-lived objects by default. If this exceeds the memory available to your dyno, Node could allow your application to start paging memory to disk.

What is RSS memory Node?

RSS is the resident set size, the portion of the process's memory held in RAM (as opposed to the swap space or the part held in the filesystem). The heap is the portion of memory from which newly allocated objects will come from (think of malloc in C, or new in JavaScript).

How much RAM is required for Node JS?

At least 2GB of RAM. At least 4 vCPUs.

How do I allocate more RAM to Node?

Use the --max-old-space-size flag when running a Node. js application to increase the available memory heap. Note that the flag needs to come before calling the file with the node command.


1 Answers

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.

like image 186
Alexey B. Avatar answered Oct 19 '22 02:10

Alexey B.