在pipeline跑UT时遇到了JavaScript堆内存不足的问题。
一、问题描述
项目为angular工程,CICD均在gitlab pipeline进行。在跑UT时出现如下报错。
20 04 2021 09:31:12.770:INFO [karma-server]: Karma v5.2.3 server started at http://localhost:9876/
20 04 2021 09:31:12.773:INFO [launcher]: Launching browsers ChromeHeadlessCI with concurrency unlimited
20 04 2021 09:31:12.791:INFO [launcher]: Starting browser ChromeHeadless
20 04 2021 09:32:12.827:WARN [launcher]: ChromeHeadless have not captured in 60000 ms, killing.
20 04 2021 09:32:18.178:INFO [launcher]: Trying to start ChromeHeadless again (1/2).
FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
<--- Last few GCs --->
[40:0x3f5ad80] 127426 ms: Mark-sweep 1367.8 (1447.5) -> 1357.9 (1449.5) MB, 507.1 / 0.0 ms (average mu = 0.171, current mu = 0.101) allocation failure scavenge might not succeed
[40:0x3f5ad80] 128003 ms: Mark-sweep 1369.3 (1449.5) -> 1361.1 (1453.0) MB, 545.6 / 0.0 ms (average mu = 0.113, current mu = 0.054) allocation failure scavenge might not succeed
<--- JS stacktrace --->
==== JS stack trace =========================================
0: ExitFrame [pc: 0x13c2416dbe1d]
1: StubFrame [pc: 0x13c2416d2d85]
Security context: 0x3abe3409e6e9 <JSObject>
2: split [0x3abe340906c9](this=0x3c6c672378d9 <String[128]: /builds/mindsphere-ali/platform/starterservices/webcomponents/core-ali/node_modules/echarts/lib/component/tooltip/TooltipView.js>,0x2aba6243ab99 <JSRegExp <String[3]: \/+>>)
3: /* anonymous */ [0x2aba624205e9] [/builds/mindsphere-ali/platform/starterserv...
1: 0x8fa050 node::Abort() [ng test --configuration=test --watch=false --code-coverage]
2: 0x8fa09c [ng test --configuration=test --watch=false --code-coverage]
3: 0xb0020e v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [ng test --configuration=test --watch=false --code-coverage]
4: 0xb00444 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [ng test --configuration=test --watch=false --code-coverage]
5: 0xef4952 [ng test --configuration=test --watch=false --code-coverage]
6: 0xef4a58 v8::internal::Heap::CheckIneffectiveMarkCompact(unsigned long, double) [ng test --configuration=test --watch=false --code-coverage]
7: 0xf00b32 v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::GCCallbackFlags) [ng test --configuration=test --watch=false --code-coverage]
8: 0xf01464 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [ng test --configuration=test --watch=false --code-coverage]
9: 0xf040d1 v8::internal::Heap::AllocateRawWithRetryOrFail(int, v8::internal::AllocationSpace, v8::internal::AllocationAlignment) [ng test --configuration=test --watch=false --code-coverage]
10: 0xecd554 v8::internal::Factory::NewFillerObject(int, bool, v8::internal::AllocationSpace) [ng test --configuration=test --watch=false --code-coverage]
11: 0x116d6de v8::internal::Runtime_AllocateInNewSpace(int, v8::internal::Object**, v8::internal::Isolate*) [ng test --configuration=test --watch=false --code-coverage]
12: 0x13c2416dbe1d
Aborted (core dumped)
npm ERR! Test failed. See above for more details.
二、解决方案
看了几篇博客,都尝试了下,亲测有两种方案是有效的。
方案一:
在package.json里面:加上这一句:–max_old_space_size=8192// 或 --max_old_space_size=4096(建议先设置成4G看看,如果不行在设置为8G)
"scripts": {
"test": "ng test"
},
修改为
"scripts": {
"test": "node --max_old_space_size=8192 ./node_modules/.bin/ng test"
},
--max_old_space_size
这个参数是针对node的,不能直接ng --max_old_space_size test
方案二:
设置NODE_OPTIONS
- export NODE_OPTIONS=--max_old_space_size=4096
- npm test