执行 npm 命令时报错:

  1. <--- Last few GCs --->
  2. [312:0x2816b40] 138898 ms: Scavenge 1342.4 (1433.8) -> 1332.9 (1437.3) MB, 8.6 / 0.0 ms (average mu = 0.282, current mu = 0.296) allocation failure
  3. [312:0x2816b40] 138917 ms: Scavenge 1345.4 (1437.3) -> 1336.3 (1441.8) MB, 7.4 / 0.0 ms (average mu = 0.282, current mu = 0.296) allocation failure
  4. [312:0x2816b40] 139468 ms: Mark-sweep 1348.5 (1441.8) -> 1338.7 (1440.8) MB, 538.2 / 0.0 ms (average mu = 0.253, current mu = 0.222) allocation failure scavenge might not succeed
  5. <--- JS stacktrace --->
  6. ==== JS stack trace =========================================
  7. 0: ExitFrame [pc: 0x29ad1bcdbe1d]
  8. Security context: 0x19b0dc61e6e9 <JSObject>
  9. 1: /* anonymous */ [0x248515843229] [/home/jenkins/agent/workspace/xxx/node_modules/@umijs/deps/compiled/webpack/4/bundle4.js:~87103] [pc=0x29ad1e5665a8](this=0x32e33579ad81 <JSGlobal Object>,chunk=0x12ead34d2eb1 <String[34]\: filename: props.name,\n>,original=0x3ae8fa2d3f59 <Object map = 0x356256be7301>)
  10. 2: Source...
  11. FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
  12. 1: 0x8dc510 node::Abort() [umi]
  13. 2: 0x8dc55c [umi]
  14. 3: 0xad9b5e v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [umi]
  15. 4: 0xad9d94 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [umi]
  16. 5: 0xec7bf2 [umi]
  17. 6: 0xec7cf8 v8::internal::Heap::CheckIneffectiveMarkCompact(unsigned long, double) [umi]
  18. 7: 0xed3dd2 v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::GCCallbackFlags) [umi]
  19. 8: 0xed4704 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [umi]
  20. 9: 0xed7371 v8::internal::Heap::AllocateRawWithRetryOrFail(int, v8::internal::AllocationSpace, v8::internal::AllocationAlignment) [umi]
  21. 10: 0xea07f4 v8::internal::Factory::NewFillerObject(int, bool, v8::internal::AllocationSpace) [umi]
  22. 11: 0x114018e v8::internal::Runtime_AllocateInNewSpace(int, v8::internal::Object**, v8::internal::Isolate*) [umi]
  23. 12: 0x29ad1bcdbe1d

原因是 JavaScript堆内存不足,Node 是基于V8引擎,在 Node 中通过 JavaScript 使用内存时只能使用部分内存(64位系统下约为1.4 GB),如果内存不足,可以用下面方法来放宽V8默认的内存限制。

法一:添加参数 --max_old_space_size=4096

  • npm 执行命令直接添加参数

    1. npm run start --max_old_space_size=4096
  • 也可以修改cmd文件

在目录node_modules/.bin下打开ng.cmd和ngc.cmd文件,添加 —max_old_space_size=4096

法二:通过 increase-memory-limit 插件

安装插件:npm install -g increase-memory-limit
执行命令:npx cross-env LIMIT=4096 increase-memory-limit

通过 log 可以发现它会将所有执行 node 命令的地方加上—max-old-space-size=4096 参数

参考:[

](https://blog.csdn.net/weixin_38883338/article/details/109346148)
https://stackoverflow.com/questions/48387040/how-do-i-determine-the-correct-max-old-space-size-for-node-js
https://banyudu.com/posts/dynamic-change-nodejs-memory-limit.3c34c2