Memory management in Node Js

Posted By :Arun Singh |29th January 2019

In the running node js process store, all its memory inside a Resident set it contains all part of the application like the actual JavaScript code and stack where all the variables live.

What is Nodejs Heap

In the running node.js process global object which contains information about the current Node.js process and it provides what memory usage for a particular object by using memoryUsage().

memoryUsage() return various information of particular Object like:

  • rss: Resident Set Size(rss), it is the total memory allocated for the process execution.
  • heapTotal: It is the total size of the allocated heap.
  • heapUsed: Its the actual memory used during the execution of our process.

Demo Memory usage for reverse() small array

In this demo, we will see how much memory is used by a very basic reverse() javascript array method.

create new file reverseMemoryUsage.js

const arr = [1, 2, 3, 4, 5, 6, 9, 7, 8, 9, 10];
arr.reverse();
const used = process.memoryUsage().heapUsed / 1024 / 1024;
console.log(`Approximately size ${Math.round(used * 100) / 100} MB`);

After running this script you should see this following output

Approximately size 3.6 MB of memory

In the above example, We can get all memoryUsage listed by this method: process.memoryUsage() this method will give all memory usage information in the array like rss, heapTotal, heapUsed and external.

const arr = [1, 2, 3, 4, 5, 6, 9, 7, 8, 9, 10];
arr.reverse();
const used = process.memoryUsage();
for (let key in used) {
  console.log(`${key} ${Math.round(used[key] / 1024 / 1024 * 100) / 100} MB`);
}

you can see this output for above example:

rss 25.63 MB
heapTotal 5.49 MB
heapUsed 3.6 MB
external 0.01 MB

It's not the most accurate way for measuring the memory consumption in Node Js but at least we will get the job done. By using memoryUsage method you have at least an indication about how much memory a given script/process takes.

 

Demo Memory usage for reverse() big array

Create new file bigArrayMemoryUsage.js

let arr = Array(1e6).fill("some string");
arr.reverse();
const used = process.memoryUsage().heapUsed / 1024 / 1024;
console.log(`Approximately size ${used} MB`);

Array(1e6) means It creates an empty array with a length property of 1 million. Then fill method will fill every position with a string. Now we have one array with 1 million elements. 

After running above script output is

Approximately size 11.23 MB

If you want to increase the size of our array change this line.

let arr = Array(1e6).fill("some string") to let arr = Array(1e8).fill("some string")

Array(1e8) will create an empty array with a length property of 100 million. After running this script again you will see how the memory usage increases:

Approximately 1278.83 MB

 

In this blog, I described only how to get the size of node js object for the monitor memory size of your application. if you were to investigate a memory leak in a Node.js application you would need more than that.  here's best writeup for more information http://jayconrod.com/posts/55/a-tour-of-v8-garbage-collection

 

Thanks for reading!


About Author

Arun Singh

Arun is a MEAN stack developer. He has a fastest and efficient way of problem solving techniques. He is very good in JavaScript and also have a little bit knowledge of Java and Python.

Request For Proposal

Sending message..

Ready to innovate ? Let's get in touch


Notice: Undefined index: HTTP_REFERER in /var/html/www/AI/wp-content/themes/oxides-child/functions.php on line 272

Notice: Undefined index: HTTP_REFERER in /var/html/www/AI/wp-content/themes/oxides-child/functions.php on line 272

Notice: Undefined index: HTTP_REFERER in /var/html/www/AI/wp-content/themes/oxides-child/functions.php on line 272

Notice: Undefined index: HTTP_REFERER in /var/html/www/AI/wp-content/themes/oxides-child/functions.php on line 272

Chat With Us