我在我的笔记本电脑(节点 18.2.0,Windows 64 位)上测试了堆大小,它增加到了我的全部内存容量。我的笔记本电脑有 8 GB 的 RAM。超过 5 GB 的数据被其他进程使用。
我从这个 (link) 中找到了一个脚本,并用它来测试堆容量,如下所示:
const array = [];
while (true) {
// This makes the array bigger on each iteration
array.push(new Array(10000000));
const memory = process.memoryUsage();
console.log((memory.heapUsed / 1024 / 1024 / 1024).toFixed(4), 'GB');
}
输出:
1.4201 GB
1.4945 GB
1.5690 GB
1.6432 GB
1.7177 GB
1.7922 GB
1.8667 GB
1.9412 GB
2.0157 GB
FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory
但是缓冲区存储在堆之外。首先,我了解了一个实例的缓冲区的最大大小,然后我对其进行了测试,我确信缓冲区不能容纳超过为实例指定的大小。但是,在各种情况下检查缓冲区时,内存很少使用并且几乎保持不变。:
//get maxiumum size of the single buffer instance
const { constants } = require('buffer');
console.log(`Buffer maxiumum size ${constants.MAX_LENGTH}`);
输出:
Buffer maximum size 4294967296
我测试了它创建一个 8 GB 的缓冲区
while (true) {
Buffer.alloc(8000*1024*1024);
const memory = process.memoryUsage();
console.log((memory.heapUsed / 1024 / 1024 / 1024).toFixed(4), 'GB');
}
它会引发以下错误:
RangeError [ERR_INVALID_ARG_VALUE]: The argument 'size' is invalid. Received 8388608000
但是当我用 3 GB 测试缓冲区时,它工作并给出了以下结果:
while (true) {
Buffer.alloc(3000*1024*1024);
const memory = process.memoryUsage();
console.log((memory.heapUsed / 1024 / 1024 / 1024).toFixed(4), 'GB');
}
//output
....
0.0042 GB
0.0042 GB
0.0042 GB
0.0042 GB
0.0042 GB
0.0042 GB
....