What do the return values of node.js process.memoryUsage() stand for?


Question

From the official documentation (source):

process.memoryUsage()

Returns an object describing the memory usage of the Node process measured in bytes.

var util = require('util');

console.log(util.inspect(process.memoryUsage()));

This will generate:

{ rss: 4935680, heapTotal: 1826816, heapUsed: 650472 }

heapTotal and heapUsed refer to V8's memory usage.

Exactly what do rss, heapTotal, and heapUsed stand for?

It might seem like a trivial question, but I've been looking and I could not find a clear answer so far.

1
116
6/27/2016 8:57:13 AM

Accepted Answer

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:

  • Code: the actual code being executed
  • Stack: contains all value types (primitives like integer or Boolean) with pointers referencing objects on the heap and pointers defining the control flow of the program
  • Heap: a memory segment dedicated to storing reference types like objects, strings and closures. enter image description here

Now it is easy to answer the question:

  • rss: Resident Set Size
  • heapTotal: Total Size of the Heap
  • heapUsed: Heap actually Used

Ref: http://apmblog.dynatrace.com/2015/11/04/understanding-garbage-collection-and-hunting-memory-leaks-in-node-js/

120
6/27/2016 8:54:30 AM

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.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon