微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 node.js 中的 createWriteStream 创建大文件时 JavaScript 堆内存不足 致命错误:达到堆限制分配失败

如何解决使用 node.js 中的 createWriteStream 创建大文件时 JavaScript 堆内存不足 致命错误:达到堆限制分配失败

const fs = require('fs');
const file = fs.createWriteStream('./big.file');


for(let i=0; i<1e8; i++){
    file.write(`llorem ipsum ${i}`);
}

file.end();

上面的代码尝试使用 fs.createWriteStream 在 node.js 中创建一个巨大的文件,据我所知,流是可能无法立即使用的数据集合,不必适合在记忆中。 但是当我运行我的脚本时,我的内存占用不断增加,最终导致 JavaScript 堆内存不足错误。 我的问题是我是否遗漏了有关流的任何信息,以及如果流不需要适合内存,为什么会发生这种情况。

运行脚本之前

Before Script was run

脚本运行后

enter image description here

错误

<--- Last few GCs --->

[386723:0x6297e70]    42815 ms: Mark-sweep (reduce) 2046.9 (2081.1) -> 2046.2 (2081.6) MB,1806.1 / 0.0 ms  (+ 94.3 ms in 13 steps since start of marking,biggest step 7.6 ms,walltime since start of marking 1911 ms) (average mu = 0.341,current mu = 0.10[386723:0x6297e70]    45153 ms: Mark-sweep (reduce) 2047.7 (2081.8) -> 2046.9 (2082.1) MB,1919.1 / 0.0 ms  (+ 123.1 ms in 16 steps since start of marking,biggest step 11.3 ms,walltime since start of marking 2057 ms) (average mu = 0.244,current mu = 0.

<--- JS stacktrace --->

Fatal error: Reached heap limit Allocation Failed - JavaScript heap out of memory
 1: 0xb17ec0 node::Abort() [node]
 2: 0xa341f4 node::FatalError(char const*,char const*) [node]
 3: 0xcfe71e v8::Utils::ReportOOMFailure(v8::internal::Isolate*,char const*,bool) [node]
 4: 0xcfea97 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*,bool) [node]
 5: 0xee8d35  [node]
 6: 0xef7ab1 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace,v8::internal::GarbageCollectionReason,v8::GCCallbackFlags) [node]
 7: 0xefad0c v8::internal::Heap::AllocaterawWithRetryOrFailSlowPath(int,v8::internal::AllocationType,v8::internal::Allocationorigin,v8::internal::AllocationAlignment) [node]
 8: 0xec72bb v8::internal::Factory::NewFillerObject(int,bool,v8::internal::Allocationorigin) [node]
 9: 0x123052b v8::internal::Runtime_AllocateInYoungGeneration(int,unsigned long*,v8::internal::Isolate*) [node]
10: 0x16147d9  [node]
Aborted (core dumped)

解决方法

我相信您需要检查 write() 的返回值,在 false 时停止写入,并利用 drain 事件指示何时再次开始写入.

目前您的循环只是无限期地写入内部缓冲区而不刷新到磁盘,这就是您的脚本导致内存错误的原因。

这样的事情应该可以工作:

const fs = require('fs');
const file = fs.createWriteStream('./big.file');

const max = 1e8;
let i = 0;

file.on('drain',function writeAsMuchAsPossible() {
  while (i < max && file.write(`llorem ipsum ${i++}`));
  if (i === max) {
    file.end();
  }
}).emit('drain')

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。