11 MB 数据库的 NodeJS 堆内存不足

NodeJS out of heap memory for an 11 MB database

我有一个 MongoDB 集合,其中包含 14,000 个文档、100,000 个子文档和 BSON 备份中的 11 MB。我无法 运行 上面的 NodeJS 代码:

VisitorSchema.statics.getAllVisitors = async function() {
  try {
    // Return all users.
    return Visitor.find()
      .exec();
  } catch (err) {
    console.log(err);
  }
};

VisitorSchema.statics.checkRecentVisits = async function() {
  console.log("Starting...");
  let uvs = await this.getAllVisitors();
  console.log("Loaded!");
}

脚本需要几秒钟才能 运行 并抛出:

Starting...

<--- Last few GCs --->
fa[1355:0x00]    12006 ms: Mark-sweep 255.4 (257.0) -> 254.7 (257.2) MB, 507.2 / 0.0 ms  (+ 0.0 ms in 12 steps since start of marking, biggest step 0.0 ms, walltime since start of marking 529 ms) (average mu = 0.124, current mu = 0.042) allocation fail[1355:0x39dd420]    12456 ms: Mark-sweep 255.6 (257.2) -> 255.0 (257.5) MB, 371.5 / 0.0 ms  (+ 58.6 ms in 13 steps since start of marking, biggest step 17.2 ms, walltime since start of marking 451 ms) (average mu = 0.087, current mu = 0.046) allocation fa

<--- JS stacktrace --->

==== JS stack trace =========================================

    0: ExitFrame [pc: 0x00]

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory

...

Aborted (core dumped)

This thread 提到了 1400 万条记录的堆故障,而我离那还很远。

如何调试或解决问题?

您可以通过以下方式将任务的堆大小加倍:

NODE_OPTIONS=--max_old_space_size=8192 node task.js

以前如果用Windows,我用:

https://www.npmjs.com/package/increase-memory-limit

此外,如果您使用的是最新版本的节点,您可能想阅读以下内容:

https://github.com/endel/increase-memory-limit#readme

也检查一下: FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - process out of memory