【问题标题】:Has the default memory limit of Node.js changed?Node.js 的默认内存限制是否改变了?
【发布时间】:2022-08-17 07:50:57
【问题描述】:

我正在使用 Node.js 进程(和node-fetch)批量下载视频文件,该进程将变量中的所有数据缓冲为简单的Buffers;不.而且我意识到,当它占用超过 13 GB 的内存时,它不再抱怨(“抛出错误”)超过内存限制,这与几年前我编写这样的内存消耗代码时不同。

Node.js(版本 18.2.0)实例是在我的 Windows 10(64 位)命令提示符下使用简单命令“node main.mjs”执行的,没有任何标志。我有大约 32 GB 的内存。

默认内存限制有什么变化吗?

我读了

  • 您是否将所有数据保存在单个缓冲区实例上?
  • @AbdurrahimAhmadov,不,我正在使用 Promise.all 同时下载多个(大约 20-40 个)视频,并使用 Buffer 下载大约 300 兆字节的视频。

标签: node.js memory memory-management v8


【解决方案1】:

我在我的笔记本电脑(节点 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
....

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-05
    • 2012-09-28
    • 2016-02-28
    • 2014-08-04
    • 2020-01-14
    • 1970-01-01
    相关资源
    最近更新 更多