node.js process.memoryUsage() 的返回值代表什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12023359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:11:17  来源:igfitidea点击:

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

node.jsv8

提问by Mahn

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.

process.memoryUsage()

返回一个对象,描述 Node 进程的内存使用量(以字节为单位)。

var util = require('util');

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

这将生成:

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

heapTotal 和 heapUsed 指的是 V8 的内存使用情况。

Exactly what do rss, heapTotal, and heapUsedstand for?

究竟是什么做RSSheapTotalheapUsed立场?

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

这似乎是一个微不足道的问题,但我一直在寻找,到目前为止我找不到明确的答案。

回答by timqian

In order to answer this question, one has to understand V8's Memory Scheme first.

要回答这个问题,首先要了解V8的Memory Scheme。

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:

一个正在运行的程序总是通过在内存中分配的一些空间来表示。这个空间称为常驻集。V8 使用了类似于 Java 虚拟机的方案,将内存划分为段:

  • 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
  • rss: 常驻集大小
  • heapTotal:堆的总大小
  • heapUsed:实际使用的堆

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

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

回答by Ray Toal

RSSis 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).

RSS常驻集大小,即进程内存中保存在 RAM 中的部分(与交换空间或文件系统中保存的部分相反)。

The heapis the portion of memory from which newly allocated objects will come from (think of mallocin C, or newin JavaScript).

所述是从新近分配的对象将来自(想到的存储器的部分malloc中C,或new在JavaScript)。

You can read more about the heap at Wikipedia.

您可以在Wikipedia 上阅读有关堆的更多信息。

回答by bvdb

The Node.js doumentationdescribes it as follows:

Node.js的doumentation其描述如下:

heapTotal and heapUsedrefer to V8's memory usage. externalrefers 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.

heapTotal 和 heapUsed指的是 V8 的内存使用情况。external指的是绑定到 V8 管理的 JavaScript 对象的 C++ 对象的内存使用。rss,Resident Set Size是进程在主内存设备(即总分配内存的子集)中占用的空间量,包括堆、代码段和堆栈。

All mentioned values are expressed in bytes.So, if you just want to print them, you probably want to rescale them to MB:

所有提到的值都以字节表示。因此,如果您只想打印它们,您可能希望将它们重新缩放为 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

回答by Cherag Verma

Let's do this with an Example

让我们用一个例子来做这件事

The following example will show you how the increase in memory usage will actually increase the rssand heapTotal

以下示例将向您展示内存使用量的增加将如何实际增加rssheapTotal

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)

这清楚地向您展示了如何使用变量并不断增加它所需的空间会增加 heapTotal 和相应的 Resident Set Size( rss)