📜  Node.js process.memoryUsage() 方法

📅  最后修改于: 2022-05-13 01:56:32.780000             🧑  作者: Mango

Node.js process.memoryUsage() 方法

process.memoryUsage()方法是 process 模块的内置方法,它提供有关 Node.js 程序的当前进程或运行时的信息。内存使用方法返回一个对象,以字节为单位描述 Node.js 进程的内存使用情况。

句法:

process.memoryUsage()   

参数:此方法不接受任何参数:

返回值:此方法返回一个带有内存使用说明的对象。

下面的例子说明了在 Node.js 中process.memoryUsage()方法的使用。

示例 1:

index.js
// Requiring module
var process = require('process')
  
// Prints the output as an object
console.log(process.memoryUsage())


index.js
// Requiring module
var process = require('process')
  
// An example displaying the respective memory
// usages in megabytes(MB)
for (const [key,value] of Object.entries(process.memoryUsage())){
    console.log(`Memory usage by ${key}, ${value/1000000}MB `)
}


使用以下命令运行index.js文件:

node index.js

输出:

{
  rss: 23851008,     
  heapTotal: 4907008,
  heapUsed: 2905912, 
  external: 951886,  
  arrayBuffers: 17574
}

示例 2:

index.js

// Requiring module
var process = require('process')
  
// An example displaying the respective memory
// usages in megabytes(MB)
for (const [key,value] of Object.entries(process.memoryUsage())){
    console.log(`Memory usage by ${key}, ${value/1000000}MB `)
}

使用以下命令运行index.js文件:

node index.js

输出:

Memory usage by rss, 23.87968MB 
Memory usage by heapTotal, 4.907008MB
Memory usage by heapUsed, 2.907088MB
Memory usage by external, 0.951886MB
Memory usage by arrayBuffers, 0.017574MB

参考: https://nodejs.org/api/process.html#process_process_memoryusage