node.js - Node.js 的默认内存限制是否已更改?

标签 node.js memory memory-management v8

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

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

默认内存限制有变化吗?

A Node.js process taking up over 12 gigs

我已阅读

最佳答案

我在我的笔记本电脑( Node 18.2.0,Windows 64 位)上测试了堆大小,它增加到我的完整 RAM 容量。我的笔记本电脑有 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
....

关于node.js - Node.js 的默认内存限制是否已更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72415924/

相关文章:

javascript - 如何在 Node.js 网站中组织模型?

javascript - Node : async loop that waits between iterations

node.js - 如何在单个请求中读取 Firestore 中的多个文档?

swift - UITableView 内存问题

c - 释放结构数组的内存

node.js - 带有 .npmrc 和身份验证的 Yarn

c++ - 当函数中未指定返回值时,C++ 程序如何获取返回值?

php - 在 PHP (PDO) 中了解 MySQL 的内存使用情况

php - 在 PHP 中管理大型数组

c - 为什么在 Linux 中内存使用量大于物理 RAM?