npm执行报"JavaScript heap out of memory"错误

今天用VUE编译百度离线世界地图是报出"JavaScript heap out of memory"错误,网上查了下资料说是内存超限了。

错误内容

执行npm run dev时报如下错误:

<--- Last few GCs --->

[20044:0000024F4EA487F0]   747461 ms: Mark-sweep 2038.1 (2077.2) -> 2038.0 (2064.9) MB, 19.5 / 0.1 ms  (+ 62.2 ms in 31 steps since start of marking, biggest step 10.0 ms, walltime since start of marking 184 ms) (average mu = 0.395, current mu = 0.559) fi[20044:000
0024F4EA487F0]   747581 ms: Mark-sweep 2040.3 (2064.9) -> 2040.1 (2080.4) MB, 95.0 / 0.1 ms  (average mu = 0.319, current mu = 0.210) allocation failure scavenge might not succeed


<--- JS stacktrace --->

==== JS stack trace =========================================

    0: ExitFrame [pc: 00007FF6AD83463D]
Security context: 0x03e61a2808a1 
    1: exec [000003E61A289881](this=0x0149f9402809 >,0x0149f94027e9 )
    2: InternalFrame [pc: 00007FF6AD7B1781]
    3: EntryFrame [pc: 00007FF6AD7B136C]
    4: ExitFrame [pc: 00007FF6AD83463D]
    5: 0x019f65304c31 (aka [Symbol.replace]) [000003E61A2897D9](this=0x0149f9402809 <...

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
 1: 00007FF6ACC6232F napi_wrap+124543
 2: 00007FF6ACC036A6 v8::base::CPU::has_sse+34502
 3: 00007FF6ACC04366 v8::base::CPU::has_sse+37766
 4: 00007FF6AD408C5E v8::Isolate::ReportExternalAllocationLimitReached+94
 5: 00007FF6AD3F0CA1 v8::SharedArrayBuffer::Externalize+833
 6: 00007FF6AD2BE56C v8::internal::Heap::EphemeronKeyWriteBarrierFromCode+1436
 7: 00007FF6AD2C9910 v8::internal::Heap::ProtectUnprotectedMemoryChunks+1312
 8: 00007FF6AD2C6444 v8::internal::Heap::PageFlagsAreConsistent+3204
 9: 00007FF6AD2BBCD3 v8::internal::Heap::CollectGarbage+1283
10: 00007FF6AD2C2404 v8::internal::Heap::GlobalSizeOfObjects+212
11: 00007FF6AD2F8AEB v8::internal::StackGuard::HandleInterrupts+907
12: 00007FF6AD0491F9 v8::internal::interpreter::JumpTableTargetOffsets::iterator::operator=+10121
13: 00007FF6AD83463D v8::internal::SetupIsolateDelegate::SetupHeap+567949
14: 00007FF6AD80A7F5 v8::internal::SetupIsolateDelegate::SetupHeap+396357
15: 00007FF6AD7B1781 v8::internal::SetupIsolateDelegate::SetupHeap+31697
16: 00007FF6AD7B136C v8::internal::SetupIsolateDelegate::SetupHeap+30652
17: 00007FF6AD316C53 v8::internal::Execution::CallWasm+1395
18: 00007FF6AD3165D6 v8::internal::Execution::Call+182
19: 00007FF6AD076592 v8::internal::RegExpParser::unicode+2962
20: 00007FF6AD021E6E v8::internal::DeclarationScope::was_lazily_parsed+23678
21: 00007FF6AD83463D v8::internal::SetupIsolateDelegate::SetupHeap+567949
22: 00007FF6AD870B7A v8::internal::SetupIsolateDelegate::SetupHeap+815050
23: 00007FF6AD821249 v8::internal::SetupIsolateDelegate::SetupHeap+489113
24: 00007FF6AD7B41CC v8::internal::SetupIsolateDelegate::SetupHeap+42524
25: 00007FF6AD851128 v8::internal::SetupIsolateDelegate::SetupHeap+685432
26: 00007FF6AD7B41CC v8::internal::SetupIsolateDelegate::SetupHeap+42524
27: 00007FF6AD7B41CC v8::internal::SetupIsolateDelegate::SetupHeap+42524
28: 00007FF6AD7B41CC v8::internal::SetupIsolateDelegate::SetupHeap+42524
29: 00007FF6AD851128 v8::internal::SetupIsolateDelegate::SetupHeap+685432
30: 00007FF6AD7B41CC v8::internal::SetupIsolateDelegate::SetupHeap+42524
31: 00007FF6AD7AD57C v8::internal::SetupIsolateDelegate::SetupHeap+14796
32: 00007FF6AD85498E v8::internal::SetupIsolateDelegate::SetupHeap+699870
33: 00007FF6AD7B41CC v8::internal::SetupIsolateDelegate::SetupHeap+42524
34: 00007FF6AD7B41CC v8::internal::SetupIsolateDelegate::SetupHeap+42524
35: 00007FF6AD7B41CC v8::internal::SetupIsolateDelegate::SetupHeap+42524
36: 00007FF6AD7B41CC v8::internal::SetupIsolateDelegate::SetupHeap+42524
37: 00007FF6AD7B41CC v8::internal::SetupIsolateDelegate::SetupHeap+42524
38: 00007FF6AD7B41CC v8::internal::SetupIsolateDelegate::SetupHeap+42524
39: 00007FF6AD7B41CC v8::internal::SetupIsolateDelegate::SetupHeap+42524
40: 00007FF6AD7B41CC v8::internal::SetupIsolateDelegate::SetupHeap+42524
41: 00000342DAB4CAA9

参考

1.https://github.com/vuejs/vue-cli/issues/1453
2.https://stackoverflow.com/questions/55842735/javascript-heap-out-of-memory-when-building-vue-js-app

最终方案

尝试了各种修改--max_old_space_size的方案还是不行

set NODE_OPTIONS=--max_old_space_size=4096 # windows
export NODE_OPTIONS=--max_old_space_size=4096 # mac/linux

最终使用yarn dev编译通过了,很是神奇啊,yarn默认的内存大小是多少呢?

你可能感兴趣的:(npm执行报"JavaScript heap out of memory"错误)