我有一个定期与Disable cache
运行的脚本。该脚本从带有Mongoose的Mongo数据库中获取1000个文档,并更新其中一些文档。下面在主要方面对脚本进行了描述,但是在运行了几天脚本后,我发现它因使用多达3GB的堆内存而被操作系统杀死:
setInterval
我正在使用的节点版本为const mongoose = require('mongoose');
clientModel = require('../prod-api/models/clientModel');
clients = mongoose.model('clients');
userModel = require('../prod-api/models/userModel');
users = mongoose.model('users');
// connect to Mongo
mongoose.Promise = global.Promise;
const config = require('./config');
mongoose.connect(config.mongo);
async function run() {
let docCount = 0;
clients.find({processed: false}).sort({timestamp: 1}).limit(1000).cursor()
.on('data', async function(doc) {
docCount++;
const user = await users.findOne({_id: doc.user_id}).exec()
.catch(() => console.log('[Error]: Getting User'));
if (user) {
// do something with user here
}
doc.processed = true;
try {
await doc.save()
console.log(`[ * ] Saved ${doc._id}`);
}
catch (error) {
console.log('[Error]: ' + error);
}
})
.on('error', function(err){
console.log('[Error] ' + err);
})
.on('end', function(){
console.log(`[ - ] Done, documents processed: ${docCount}`);
});
}
setInterval(run, 15000);
,并且该脚本仅通过v9.11.2
命令在屏幕会话上运行。我看到的错误是:
forever
理想的候选者是FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
1: node::Abort() [/usr/bin/node]
2: 0x8d05bc [/usr/bin/node]
3: v8::Utils::ReportOOMFailure(char const*, bool) [/usr/bin/node]
4: v8::internal::V8::FatalProcessOutOfMemory(char const*, bool) [/usr/bin/node]
5: v8::internal::factory::NewFixedArray(int, v8::internal::PretenureFlag) [/usr/bin/node]
6: v8::internal::HashTable<v8::internal::StringTable, v8::internal::StringTableShape>::NewInternal(v8::internal::Isolate*, int, v8::internal::PretenureFlag) [/usr/bin/node]
7: v8::internal::HashTable<v8::internal::StringTable, v8::internal::StringTableShape>::New(v8::internal::Isolate*, int, v8::internal::PretenureFlag, v8::internal::MinimumCapacity) [/usr/bin/node]
8: v8::internal::HashTable<v8::internal::StringTable, v8::internal::StringTableShape>::EnsureCapacity(v8::internal::Handle<v8::internal::StringTable>, int, v8::internal::PretenureFlag) [/usr/bin/node]
9: v8::internal::StringTable::LookupString(v8::internal::Isolate*, v8::internal::Handle<v8::internal::String>) [/usr/bin/node]
10: 0x104018e [/usr/bin/node]
11: v8::internal::Runtime_KeyedGetProperty(int, v8::internal::Object**, v8::internal::Isolate*) [/usr/bin/node]
12: 0x3e78293842fd
error: Forever detected script was killed by signal: SIGABRT
,但还有哪些其他原因可能导致此内存泄漏以及如何防止这种情况。显然,我对GC的期望过高?