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

后端 未结 4 1135
你的背包
你的背包 2020-12-07 09:51

From the official documentation (source):

process.memoryUsage()

Returns an object describing the memory usage of the Node

相关标签:
4条回答
  • 2020-12-07 10:06

    The Node.js doumentation 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
    
    0 讨论(0)
  • 2020-12-07 10:06

    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)

    0 讨论(0)
  • 2020-12-07 10:07

    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.

    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/

    0 讨论(0)
  • 2020-12-07 10:10

    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.

    0 讨论(0)
提交回复
热议问题