如何解决angular项目启动 报内存溢出

问题描述:
在使用angular启动项目时,有时候会报内存溢出的错误,导致项目启动失败。

报错信息如下:

<--- JS stacktrace --->

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

    0: ExitFrame [pc: 0000002919A5C5C1]
Security context: 0x02b9c119e6e9 <JSObject>
    1: _send [0000016FEC87BBF1] [internal/child_process.js:736] [bytecode=0000016FEC878FD9 offset=660](this=0x0013a6e4b2b9 <ChildProcess map = 
000003A8411F6819>,message=0x029b3af56a39 <Object map = 000003A8411F7371>,handle=0x029057e026f1 <undefined>,options=0x029b3af56ad9 <Object map = 000003A8411F8971>,callback=0x029057e026f1 <undefined>)
    2: send [000...

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory

启动失败的原因:nodejs 默认限制了最大可使用的内存大小。

解决办法:
在项目的node-module模块node_modules.bin模块下, 找到ng.cmd和ngc.cmd
通过配置–max_old_space_size属性,修改最大可使用的内存大小。

ng.cmd文件:

@IF EXIST "%~dp0\node.exe" (
 "%~dp0\node.exe" --max_old_space_size=8192 "%~dp0\..\._@angular_cli@1.0.0@@angular\cli\bin\ng" %*
) ELSE (
 @SETLOCAL
 @SET PATHEXT=%PATHEXT:;.JS;=;%
 node --max_old_space_size=8192 "%~dp0\..\._@angular_cli@1.0.0@@angular\cli\bin\ng" %*
)

ngc.cmd文件:

@IF EXIST "%~dp0\node.exe" (
 "%~dp0\node.exe" --max_old_space_size=8192 "%~dp0\..\._@angular_compiler-cli@4.0.1@@angular\compiler-cli\src\main.js" %*
) ELSE (
 @SETLOCAL
 @SET PATHEXT=%PATHEXT:;.JS;=;%
 node --max_old_space_size=8192 "%~dp0\..\._@angular_compiler-cli@4.0.1@@angular\compiler-cli\src\main.js" %*
)
上一篇:批处理(.bat)文件中找不到文件等路径问题


下一篇:[LeetCode] #121 买卖股票的最佳时机