您可以看到内置的内存正在使用多少process
module.
const process = require("process");
The process
模块有一个方法称为memoryUsage https://nodejs.org/api/process.html#process_process_memoryusage,它显示 Node.js 中内存使用情况的信息。
console.log(process.memoryUsage());
当您运行代码时,您应该看到一个对象,其中包含内存使用所需的所有信息!
$ node index.js
{
rss: 4935680,
heapTotal: 1826816,
heapUsed: 650472,
external: 49879,
arrayBuffers: 9386
}
以下是对每个属性的一些见解。
-
rss
-(驻留集大小)主存储设备中占用的空间量。
-
heapTotal
- V8 引擎中的内存总量。
-
heapUsed
- V8 引擎使用的内存量。
-
external
- 绑定到 JavaScript 对象的 C++ 对象的内存使用情况(由 V8 管理)。
-
arrayBuffers
- 分配的内存ArrayBuffer
s and Buffer
s.
对于您的问题,您可能需要使用heapTotal
and heapUsed
。根据该值,您可以关闭该服务。例如:
const process = require("process");
const mem = process.memoryUsage();
const MAX_SIZE = 50; // Change the value to what you want
if ((mem.heapUsed / 1000000) >= MAX_SIZE) {
videoTranscoder.kill(); // Just an example...
}
除以一百万部分只是将字节转换为兆字节(B 转换为 MB)。
Change the code to what you like - this is just an example.