[解决]angular(ng2) webpack打包时崩溃JavaScript heap out of memory

我的情况是:项目使用ng5,代码量比较大,最近经常会遇到代码修改后,自动编译的时候经常会崩溃掉,提示内存足,分配不了更多内存

CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory

完整错误提示如下:

Starting the development server...

<--- Last few GCs --->

[2683:0x102802a00]    69789 ms: Mark-sweep 1351.8 (1420.1) -> 1351.5 (1420.6) MB, 513.4 / 0.0 ms  (average mu =0.119, current mu = 0.007) allocation failure scavenge might not succeed
[2683:0x102802a00]    70505 ms: Mark-sweep 1352.2 (1420.6) -> 1351.8 (1421.1) MB, 712.0 / 0.0 ms  (average mu =0.055, current mu = 0.005) allocation failure scavenge might not succeed

<--- JS stacktrace --->

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

    0: ExitFrame [pc: 0x39417c9841bd]
Security context: 0x2290b1c9e6c9 
    1: /* anonymous */(aka /* anonymous */) [0x229008134879] [/Users/liluohan/xiaodianpu-saas/node_modules/webpack/lib/Stats.js:~140] [pc=0x39417e6c057a](this=0x2290945822e1 ,module=0x2290197e05b1 )
    2: arguments adaptor frame: 3->1
    3: filter [0x2290b1c86a91](this=0x2290081347b1 ,0x2290...

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
 1: 0x100033c31 node::Abort() [/usr/local/bin/node]
 2: 0x1000353ea node::FatalTryCatch::~FatalTryCatch() [/usr/local/bin/node]
 3: 0x10019bc9a v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [/usr/local/bin/node]
 4: 0x10056a072 v8::internal::Heap::FatalProcessOutOfMemory(char const*) [/usr/local/bin/node]
 5: 0x100569029 v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::GCCallbackFlags) [/usr/local/bin/node]
 6: 0x100566cb8 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [/usr/local/bin/node]
 7: 0x1005731fc v8::internal::Heap::AllocateRawWithRetry(int, v8::internal::AllocationSpace, v8::internal::AllocationAlignment) [/usr/local/bin/node]
 8: 0x100541b94 v8::internal::Factory::NewFillerObject(int, bool, v8::internal::AllocationSpace) [/usr/local/bin/node]
 9: 0x1007cb394 v8::internal::Runtime_AllocateInNewSpace(int, v8::internal::Object**, v8::internal::Isolate*) [/usr/local/bin/node]
10: 0x39417c9841bd
11: 0x39417e6c057a
12: 0x39417c98c5a3
13: 0x39417ca2535d
[1]    2681 abort      npm start
➜  xiaodianpu-saas git:(master) ✗ cnpm unstall ngx-cookie

上网找资料说修复这个目录:node_modules\.bin 的两个文件,但对我来说没啥用。这个目录只要一编译就会全部清空,然后再写入,所以改的内容一下就没有了。

最终我是通过修改启动项目的脚本参数, 就是package.json的scripts标签下指定最大内存限制,就是加参数:max_old_space_size

具体如下:

"start": "node --max_old_space_size=4096 scripts/start.js",

4096 就是4G的意思,可以根据自己项目需求修改。修改后最终是没有崩溃了。。。原来是多么崩溃,一改代码就崩,然后 npm start 重新启动,人都快要崩溃了。

[解决]angular(ng2) webpack打包时崩溃JavaScript heap out of memory_第1张图片

这样执行 npm start的时候,参数就会生效,于是终于可以安心写代码了。 cheers````

 

补一下错误截图如下

[解决]angular(ng2) webpack打包时崩溃JavaScript heap out of memory_第2张图片

你可能感兴趣的:(js,nodejs)