问题描述
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 堆内存不足错误。
我的问题是我是否遗漏了有关流的任何信息,以及如果流不需要适合内存,为什么会发生这种情况。
错误
<--- 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)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)