当内存不足时,我的代码会自行停止并给出一系列有关“内存不足”的错误。
当我的代码停止工作或我自己停止(ctrl + c)时,有什么方法可以console.log()
进行任何操作?
一切都在终端上。
答案 0 :(得分:2)
您可以尝试以下方法:
const os = require('os');
const THRESHOLD = 1000000 * 100; // 100 mb
// Check how much space left with a minute interval
setInterval(function () {
if (os.freemem() - process.memoryUsage().rss < THRESHOLD) {
console.log('We lack of memory!');
}
}, 1000 * 60);
答案 1 :(得分:1)
好吧,您可以尝试通过以下方式增加Node.js的内存限制:
// Increase max memory to 4GB.
$ node --max-old-space-size=4096 index.js
所以它不会崩溃。