process.memoryUsage()
- 返回: <Object>
process.memoryUsage()
方法会返回描述 Node.js 进程的内存使用情况(以字节为单位)的对象。
例如:
console.log(process.memoryUsage());
会返回:
{
rss: 4935680,
heapTotal: 1826816,
heapUsed: 650472,
external: 49879,
arrayBuffers: 9386
}
heapTotal
和heapUsed
代表 V8 的内存使用情况。external
代表 V8 管理的绑定到 Javascript 对象的 C++ 对象的内存使用情况。rss
,常驻集大小, 是为进程分配的物理内存(总分配内存的子集)的大小,包括所有的 C++ 和 JavaScript 对象与代码。arrayBuffers
代表分配给ArrayBuffer
和SharedArrayBuffer
的内存,包括所有的 Node.jsBuffer
。 这也包含在external
值中。 当 Node.js 被用作嵌入式库时,此值可能为0
,因为在这种情况下可能无法跟踪ArrayBuffer
的分配。
当使用 Worker
线程时, rss
会是对整个进程都有效的值,而其他字段只代表当前线程。
- Returns: <Object>
The process.memoryUsage()
method returns an object describing the memory usage
of the Node.js process measured in bytes.
For example, the code:
console.log(process.memoryUsage());
Will generate:
{
rss: 4935680,
heapTotal: 1826816,
heapUsed: 650472,
external: 49879,
arrayBuffers: 9386
}
heapTotal
andheapUsed
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, including all C++ and JavaScript objects and code.arrayBuffers
refers to memory allocated forArrayBuffer
s andSharedArrayBuffer
s, including all Node.jsBuffer
s. This is also included in theexternal
value. When Node.js is used as an embedded library, this value may be0
because allocations forArrayBuffer
s may not be tracked in that case.
When using Worker
threads, rss
will be a value that is valid for the
entire process, while the other fields will only refer to the current thread.