1、运行项目提示内存溢出

出现内存溢出的原因有多方面,因为node.js是依靠V8引擎去运行的环境,自身配置的内存不一定满足的项目的运行,因而需要去手动配置内存的大小。在运行的系统方面,部分符号的转译也不尽相同,所以下面列举的解决方案请根据自生的问题去选择。

1、控制台出现警告

invalid array length Allocation failed - JavaScript heap out of memory

无效的数组长度分配失败- JavaScript堆内存

提示项目运行内存溢出。

<--- Last few GCs --->

[24140:0000022A8599D210]   705610 ms: Scavenge 1677.2 (1731.8) -> 1661.5 (1731.8) MB, 0.5 / 0.0 ms  allocation failure
[24140:0000022A8599D210]   705627 ms: Scavenge 1677.2 (1731.8) -> 1661.5 (1731.8) MB, 0.7 / 0.0 ms  allocation failure
[24140:0000022A8599D210]   705647 ms: Scavenge 1677.2 (1731.8) -> 1661.5 (1731.8) MB, 0.4 / 0.0 ms  allocation failure
[24140:0000022A8599D210]   705664 ms: Scavenge 1677.2 (1731.8) -> 1661.5 (1731.8) MB, 0.7 / 0.0 ms  allocation failure


<--- JS stacktrace --->

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

Security context: 000001286CCA5879 <JSObject>
    1: processNodeList(aka processNodeList) [C:\Users\Administrator\Desktop\cmd\node_modules\eslint-plugin-vue\lib\utils
\indent-common.js:~383] [pc=000002899F735BB8](this=0000036D7CB822D1 <undefined>,nodeList=00000302538BB1F9 <JSArray[11]>,
left=000001DE330466A9 <Object map = 000003AD6BC2C9F1>,right=0000036D7CB82201 <null>,offset=1,alignVertically=0000036D7CB
82371 <true>)
    2: VStartTag [C:\U...

FATAL ERROR: invalid array length Allo 54% building 369/390 modules 21 active ...esktop\cmd\src\api\platform\org\user.js
 1: node_module_register
 2: v8::internal::FatalProcessOutOfMemory
 3: v8::internal::FatalProcessOutOfMemory
 5:  54% building 370/390 modules 20 active ...esktop\cmd\src\api\platform\org\user.jsv8::internal::Factory::NewUninitia
lizedFixedArray
 6: v8::internal::WasmDebugInfo::SetupForTesting
 7: v8::internal::interpreter::BytecodeArrayRandomIterator::UpdateOffsetFromIndex
 8: 000002899EC843C1                   69% building 1356/1370 modules 14 active ...node_modules\video.js\dist\video.cjs.
js
<--- Last few GCs --->

[57528:00000144962E2480]    45955 ms: Scavenge 1017.1 (1108.5) -> 1001.4 (1108.5) MB, 0.8 / 0.0 ms  allocation failure
[57528:00000144962E2480]    45971 ms: Scavenge 1017.1 (1108.5) -> 1001.4 (1108.5) MB, 0.7 / 0.0 ms  allocation failure
[57528:00000144962E2480]    45987 ms: Scavenge 1017.1 (1108.5) -> 1001.4 (1108.5) MB, 0.4 / 0.0 ms  allocation failure
[57528:00000144962E2480]    46004 ms: Scavenge 1017.1 (1108.5) -> 1001.4 (1108.5) MB, 0.5 / 0.0 ms  allocation failure


<--- JS stacktrace --->

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

Security context: 00000332E3925879 <JSObject>
    1: processNodeList(aka processNodeList) [C:\Users\Administrator\Desktop\cmd\node_modules\eslint-plugin-vue\lib\utils
\indent-common.js:~383] [pc=000000E541BB9738](this=000000D7AD7022D1 <undefined>,nodeList=000001AB2CD0D039 <JSArray[11]>,
left=00000219EC4651D1 <Object map = 000002BB8FAD1779>,right=000000D7AD702201 <null>,offset=1,alignVertically=000000D7AD7
02371 <true>)
    2: VStartTag [C:\U...

FATAL ERROR: invalid array length Allocation failed - JavaScript heap out of memory
 1: node_module_register
 2: v8::internal::FatalProcessOutOfMemory
 3: v8::internal::FatalProcessOutOfMemory
 4: v8::internal::Heap::CreateFillerObjectAt
 5: v8::internal::Factory::NewUninitializedFixedArray
 6: v8::internal::WasmDebugInfo::SetupForTesting
 7: v8::internal::interpreter::BytecodeArrayRandomIterator::UpdateOffsetFromIndex
 8: 000000E5417043C1
npm ERR! code ELIFECYCLE
npm ERR! errno 3
npm ERR! ibps-app-ui@2.8.15 dev: `vue-cli-service serve`
npm ERR! Exit status 3
npm ERR!
npm ERR! Failed at the ibps-app-ui@2.8.15 dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Administrator\AppData\Roaming\npm-cache\_logs\2020-01-06T02_24_15_569Z-debug.log

2、处理方案

1、package.json添加内存

在脚本scripts里的dev指令内添加npx --max_old_space_size=40964096配置的内存大小,可根据自身内存使用情况进行配置。

2、vue.config.js设置lintOnSave值为false

3、系统运行差异

对于系统运行差异导致的内存报错,采用排除法去解决是目前最有效的方法之一,当然方案2也是可以处理这点问题,不过要解决根本还是需要找到问题文件。一些符号的转译,譬如空格、换行、tab缩进等在不同系统的转译也是不同。vue-cli3框架运行时需要注意,如果时mac上开发的文件,在window上开发运行时会出现这个内存溢出的问题,具体的解决方案,通过排除法去差找,然后替换掉这些问题符号。

文档更新时间: 2020-01-06 11:24   作者:陈杰