Node. js provides the facility to get process information such as process id, architecture, platform, version, release, uptime, upu usage etc. It can also be used to kill process, set uid, set groups, unmask etc. The process is a global object, an instance of EventEmitter, can be accessed from anywhere.
memoryUsage() method is an inbuilt method of the process module that provides information about the current processes or runtime of a Node. js program. The memory usage method returns an object describing the memory usage in bytes of the Node.
The heap is a memory segment used for storing objects, strings and closures. This is where all the magic happens. The heap is part of something bigger though: a running Node. js process store all its memory inside a Resident Set. You can think of it as of a big box which contains some more boxes.
The native module os can give you some memory and cpu usage statistics. The cpus() function gives you an average, but you can calculate the current usage by using a formula and an interval, as mentioned in this answer. There is also a package that does this for you, called os-utils. Show activity on this post.
In order to answer this question, one has to understand V8’s Memory Scheme first.
A running program is always represented through some space allocated in memory. This space is called Resident Set. V8 uses a scheme similar to the Java Virtual Machine and divides the memory into segments:
Now it is easy to answer the question:
Ref: http://apmblog.dynatrace.com/2015/11/04/understanding-garbage-collection-and-hunting-memory-leaks-in-node-js/
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).
You can read more about the heap at Wikipedia.
The Node.js documentation describes it as follows:
heapTotal and heapUsed refer to V8's memory usage. external refers to the memory usage of C++ objects bound to JavaScript objects managed by V8. rss, Resident Set Size, is the amount of space occupied in the main memory device (that is a subset of the total allocated memory) for the process, which includes the heap, code segment and stack.
All mentioned values are expressed in bytes. So, if you just want to print them, you probably want to rescale them to MB:
const used = process.memoryUsage();
for (let key in used) {
console.log(`Memory: ${key} ${Math.round(used[key] / 1024 / 1024 * 100) / 100} MB`);
}
That will give you an output like:
Memory: rss 522.06 MB
Memory: heapTotal 447.3 MB
Memory: heapUsed 291.71 MB
Memory: external 0.13 MB
Let's do this with an Example
The following example will show you how the increase in memory usage will actually increase the rss
and heapTotal
const numeral = require('numeral');
let m = new Map();
for (let i = 0; i < 100000; i++) {
m.set(i, i);
if (i % 10000 === 0) {
const { rss, heapTotal } = process.memoryUsage();
console.log( 'rss', numeral(rss).format('0.0 ib'), heapTotal, numeral(heapTotal).format('0.0 ib') )
}
}
Running The above will give you something like this:
rss 22.3 MiB 4734976 4.5 MiB
rss 24.2 MiB 6483968 6.2 MiB
rss 27.6 MiB 9580544 9.1 MiB
rss 27.6 MiB 9580544 9.1 MiB
rss 29.3 MiB 11419648 10.9 MiB
rss 29.3 MiB 11419648 10.9 MiB
rss 29.3 MiB 11419648 10.9 MiB
rss 32.8 MiB 15093760 14.4 MiB
rss 32.9 MiB 15093760 14.4 MiB
rss 32.9 MiB 15093760 14.4 MiB
This clearly shows you how using variable and continuously incrementing the space required by it increases the heapTotal and correspondingly the Resident Set Size(rss
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With