angular 服务端渲染npm run dev:ssr报错

提示内存溢出
FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory

  1. <--- Last few GCs --->
  2. [10668:0x4948140] 136804 ms: Scavenge (reduce) 1927.9 (2037.3) -> 1927.3 (2037.3) MB, 2.5 / 0.0 ms (average mu = 0.258, current mu = 0.228) allocation failure
  3. [10668:0x4948140] 136811 ms: Scavenge (reduce) 1928.3 (2037.5) -> 1927.7 (2037.5) MB, 2.5 / 0.0 ms (average mu = 0.258, current mu = 0.228) allocation failure
  4. [10668:0x4948140] 136900 ms: Scavenge (reduce) 1928.5 (2037.8) -> 1927.9 (2037.8) MB, 5.1 / 0.0 ms (average mu = 0.258, current mu = 0.228) allocation failure
  5. <--- JS stacktrace --->
  6. FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
  7. 1: 0xb00e10 node::Abort() [ng run blogWeb:serve-ssr]
  8. 2: 0xa1823b node::FatalError(char const*, char const*) [ng run blogWeb:serve-ssr]
  9. 3: 0xcee09e v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [ng run blogWeb:serve-ssr]
  10. 4: 0xcee417 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [ng run blogWeb:serve-ssr]
  11. 5: 0xea65d5 [ng run blogWeb:serve-ssr]
  12. 6: 0xea70b6 [ng run blogWeb:serve-ssr]
  13. 7: 0xeb4fee [ng run blogWeb:serve-ssr]
  14. 8: 0xeb5a30 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [ng run blogWeb:serve-ssr]
  15. 9: 0xeb89ae v8::internal::Heap::AllocateRawWithRetryOrFailSlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [ng run blogWeb:serve-ssr]
  16. 10: 0xe79b12 v8::internal::Factory::AllocateRaw(int, v8::internal::AllocationType, v8::internal::AllocationAlignment) [ng run blogWeb:serve-ssr]
  17. 11: 0xe72124 v8::internal::FactoryBase<v8::internal::Factory>::AllocateRawWithImmortalMap(int, v8::internal::AllocationType, v8::internal::Map, v8::internal::AllocationAlignment) [ng run blogWeb:serve-ssr]
  18. 12: 0xe73e40 v8::internal::FactoryBase<v8::internal::Factory>::NewRawOneByteString(int, v8::internal::AllocationType) [ng run blogWeb:serve-ssr]
  19. 13: 0x1105a28 v8::internal::String::SlowFlatten(v8::internal::Isolate*, v8::internal::Handle<v8::internal::ConsString>, v8::internal::AllocationType) [ng run blogWeb:serve-ssr]
  20. 14: 0xcf9989 v8::String::Utf8Length(v8::Isolate*) const [ng run blogWeb:serve-ssr]
  21. 15: 0xad74e7 [ng run blogWeb:serve-ssr]
  22. 16: 0xd4a82e [ng run blogWeb:serve-ssr]
  23. 17: 0xd4bc4f v8::internal::Builtin_HandleApiCall(int, unsigned long*, v8::internal::Isolate*) [ng run blogWeb:serve-ssr]
  24. 18: 0x15e7dd9 [ng run blogWeb:serve-ssr]
  25. 已放弃 (核心已转储)

原因

在 Node 中通过 JavaScript 使用内存时只能使用部分内存(64位系统下约为1.4 GB,32位系统下约为0.7 GB),这就是我们编译项目时为什么会出现内存泄露了,因为前端项目如果非常的庞大,webpack 编译时就会占用很多的系统资源,如果超出了V8对 Node 默认的内存限制大小就会出现刚刚我截图的那个错误了。

解决方法

在项目根目录node_modeles文件夹下的.bin目录里面找到一个叫ng的文件,在该文件的首行写上#!/usr/bin/env node —max_old_space_size=4096,这样也就可以解除v8对node的内存使用限制了。

more

参考文章:基于node的前端项目编译时内存溢出问题