我有一个MongoDB集合,其中包含14000个文档,10万个子文档和11mb的BSON备份。我无法在上面运行这个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)
这个线程提到了1400万条记录的堆故障,我还远远没有达到。
我如何调试问题或解决它?
可以使用以下命令将任务堆大小增加一倍:
NODE_OPTIONS=--max_old_space_size=8192 node task.js
在过去,如果使用Windows,我使用:
https://www.npmjs.com/package/increase-memory-limit
同样,如果你使用的是最新版本的node,你可能会喜欢读这个:
https://github.com/endel/increase-memory-limit自述
也检查一下:CALL_AND_RETRY_LAST分配失败-进程内存不足