Skip to content

nodeでJavaScript heap out of memoryエラーが発生したときの対処法

diamond-writerwebpack でバンドルしているとき以下のエラーが発生した。

Terminal window
$ webpack
<--- Last few GCs --->
[2779:0x1b101000] 28550 ms: Scavenge (interleaved) 2026.3 (2073.6) -> 2024.6 (2074.8) MB, pooled: 0 MB, 20.40 / 0.00 ms (average mu = 0.410, current mu = 0.208) allocation failure;
[2779:0x1b101000] 28709 ms: Scavenge (interleaved) 2027.7 (2075.1) -> 2026.9 (2096.1) MB, pooled: 0 MB, 150.67 / 0.00 ms (average mu = 0.410, current mu = 0.208) allocation failure;
<--- JS stacktrace --->
FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory
----- Native stack trace -----
1: 0xe3811e node::OOMErrorHandler(char const*, v8::OOMDetails const&) [webpack]
2: 0x125fb70 v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, v8::OOMDetails const&) [webpack]
3: 0x125fe47 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, v8::OOMDetails const&) [webpack]
4: 0x148d885 [webpack]
5: 0x14a70f9 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [webpack]
6: 0x147b7c8 v8::internal::HeapAllocator::AllocateRawWithLightRetrySlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [webpack]
7: 0x147c6f5 v8::internal::HeapAllocator::AllocateRawWithRetryOrFailSlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [webpack]
8: 0x14553ce v8::internal::Factory::NewFillerObject(int, v8::internal::AllocationAlignment, v8::internal::AllocationType, v8::internal::AllocationOrigin) [webpack]
9: 0x18b69cc v8::internal::Runtime_AllocateInYoungGeneration(int, unsigned long*, v8::internal::Isolate*) [webpack]
10: 0x7f18e6aac476
Aborted (core dumped)

このメッセージが意味しているものは読めないけど、肝は FATAL ERROR: Reached heap limit Allocation failed のところで、ヒープメモリが足りていないことが原因らしい。V8エンジンは世代別GCなので、長期間生き残ったオブジェクトはOld Spaceに移る。ここが不足しているらしい。なのでNode.jsにコマンドラインオプションを渡す方法のどれかで —max_old_space_size おプションを渡すといい。

Terminal window
export NODE_OPTIONS='--max_old_space_size=4096'
npx webpack