angular4 编译时内存溢出

ng build --prod

[5019:0x103001c00]   975889 ms: Mark-sweep 1444.8 (1570.7) -> 1444.8 (1567.7) MB, 1096.2 / 0.0 ms  (+ 0.0 ms in 0 steps since start of marking, biggest step 0.0 ms, walltime since start of marking 1250 ms) last resort 
[5019:0x103001c00]   976995 ms: Mark-sweep 1444.8 (1567.7) -> 1444.7 (1567.7) MB, 1104.5 / 0.0 ms  last resort 


<--- JS stacktrace --->

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

Security context: 0x6a161e1bbd9 
    1: OutputStream(aka OutputStream) [0x6a161e02241 :~3952] [pc=0x846e41ba59a](this=0x6a161e02241 ,options=0x6a161e02241 )
    2: print_to_string [0x6a161e02241 :4375] [pc=0x846e3b1a329](this=0x34233ad63c09 ,options=0x6a161e02241 )
    3: arguments adaptor frame: 0->1
    4: best_of_expression(aka best_...

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
 1: node::Abort() [/usr/local/bin/node]
 2: node::FatalException(v8::Isolate*, v8::Local, v8::Local) [/usr/local/bin/node]
 3: v8::internal::V8::FatalProcessOutOfMemory(char const*, bool) [/usr/local/bin/node]
 4: v8::internal::Factory::NewTransitionArray(int) [/usr/local/bin/node]
 5: v8::internal::TransitionArray::Insert(v8::internal::Handle, v8::internal::Handle, v8::internal::Handle, v8::internal::SimpleTransitionFlag) [/usr/local/bin/node]
 6: v8::internal::Map::CopyReplaceDescriptors(v8::internal::Handle, v8::internal::Handle, v8::internal::Handle, v8::internal::TransitionFlag, v8::internal::MaybeHandle, char const*, v8::internal::SimpleTransitionFlag) [/usr/local/bin/node]
 7: v8::internal::Map::CopyAddDescriptor(v8::internal::Handle, v8::internal::Descriptor*, v8::internal::TransitionFlag) [/usr/local/bin/node]
 8: v8::internal::Map::CopyWithField(v8::internal::Handle, v8::internal::Handle, v8::internal::Handle, v8::internal::PropertyAttributes, v8::internal::PropertyConstness, v8::internal::Representation, v8::internal::TransitionFlag) [/usr/local/bin/node]
 9: v8::internal::Map::TransitionToDataProperty(v8::internal::Handle, v8::internal::Handle, v8::internal::Handle, v8::internal::PropertyAttributes, v8::internal::PropertyConstness, v8::internal::Object::StoreFromKeyed) [/usr/local/bin/node]
10: v8::internal::LookupIterator::PrepareTransitionToDataProperty(v8::internal::Handle, v8::internal::Handle, v8::internal::PropertyAttributes, v8::internal::Object::StoreFromKeyed) [/usr/local/bin/node]
11: v8::internal::StoreIC::LookupForWrite(v8::internal::LookupIterator*, v8::internal::Handle, v8::internal::Object::StoreFromKeyed) [/usr/local/bin/node]
12: v8::internal::StoreIC::UpdateCaches(v8::internal::LookupIterator*, v8::internal::Handle, v8::internal::Object::StoreFromKeyed) [/usr/local/bin/node]
13: v8::internal::StoreIC::Store(v8::internal::Handle, v8::internal::Handle, v8::internal::Handle, v8::internal::Object::StoreFromKeyed) [/usr/local/bin/node]
14: v8::internal::KeyedStoreIC::Store(v8::internal::Handle, v8::internal::Handle, v8::internal::Handle) [/usr/local/bin/node]
15: v8::internal::Runtime_KeyedStoreIC_Miss(int, v8::internal::Object**, v8::internal::Isolate*) [/usr/local/bin/node]
16: 0x846e05840bd
Abort trap: 6

分析

1、在编译时对CPU和内存的需求较大,当项目文件较多时可能出现内存溢出;
2、大量的订阅的数据在组件销毁时未能取消订阅,会造成大量内存被占用;
3、代码出现大量的大数据的循环或死循环;
4、模块组件或公共函数循环引用或调用引起的死循环;

解决方案 - 增加编译内存

修改 package.json 文件增加编译所需要的内存空间

...
"build": "node --max_old_space_size=8192 ./node_modules/@angular/cli/bin/ng build --prod",
...

解决方案 - 移除 *.spec.ts 文件

在项目根目录下执行以下代码,将项目中的 *.spec.ts 文件。(该文件为组件、服务、管道、指令等的测试文件)

find . -name "*.spec.ts" | xargs rm -rf

解决方案 - 降低组件或函数的耦合性

这个方案就有点搞大了,请诸君谨慎操作

你可能感兴趣的:(Angular,Angular,内存溢出)