简体   繁体   English

Javascript 堆出 memory

[英]Javascript heap out of memory

I am getting the following error while using npm start for my angular pages.在为我的 angular 页面使用npm start时出现以下错误。

C:\TPO\Client\Angular>ng serve
Your global Angular CLI version (1.3.2) is greater than your local
version (1.0.4). The local Angular CLI version is used.

To disable this warning use "ng set --global warnings.versionMismatch=false".
** NG Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200 **
10% building modules 4/4 modules 0 active
<--- Last few GCs --->

  118169 ms: Mark-sweep 1348.4 (1404.3) -> 1348.1 (1404.3) MB, 1338.9 / 0.0 ms [allocation failure] [GC in old space requested].
  119374 ms: Mark-sweep 1348.1 (1404.3) -> 1348.1 (1404.3) MB, 1204.7 / 0.0 ms [allocation failure] [GC in old space requested].
  121097 ms: Mark-sweep 1348.1 (1404.3) -> 1362.7 (1403.3) MB, 1723.2 / 0.0 ms [last resort gc].
  122416 ms: Mark-sweep 1362.7 (1403.3) -> 1377.4 (1403.3) MB, 1317.9 / 0.0 ms [last resort gc].


<--- JS stacktrace --->

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

Security context: 0000034B7C13FA99 <JS Object>
    1: set [native collection.js:~252] [pc=00000174AF1BC2E1] (this=000003AD36FBC469 <a Map with map 0000002CF18113B1>,r=000002D7A3418CD9 <String[12]: 32741,580445>,z=1)
    2: /* anonymous */ [C:\TPO\Client\Angular\node_modules\typescript\lib\typescript.js:~2177] [pc=00000174ADC9EBD0] (this=000001D0EA536CE1 <JS Global Object>,value=1,key=000002D7A3418CD9 <String[12]: 32741,580445>)
    3: arg...

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

Unless there is something wrong with your application, the simplest way to get rid of the problem (as suggested by this answer ) is to increase heap size for node.js 除非您的应用程序有问题,否则解决问题的最简单方法(如本答案所示 )是增加node.js的堆大小

On Windows this can be done by navigating to 在Windows上,可以通过导航到

C:\Users\your_user_name\%AppData%\Roaming\npm

and then editing your ng.cmd in there: 然后在其中编辑ng.cmd

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

The --max_old_space_size=4096 sets the heap size for node's V8 engine to corresponding value in MBs (in our case this is 4096, but probably you may fine-tune it depending on your workstation's capabilities) instead of default value of 1400 MB --max_old_space_size=4096节点的V8引擎的堆大小设置为以MB为单位的对应值(在我们的情况下为4096,但您可能会根据工作站的性能对其进行微调),而不是默认值1400 MB

This question showed up when I was looking to solve the Javascript heap out of memory error while building my Next.js project.当我在构建我的 Next.js 项目时试图解决Javascript heap out of memory时出现了这个问题。

Therefore, adding a more generic answer for those that stumble across here.因此,为那些在这里偶然发现的人添加一个更通用的答案。

I solved the error by adding NODE_OPTIONS='--max-old-space-size=4096' to the build script in package.json as shown in the following example.我通过在 package.json 中的build脚本中添加package.json NODE_OPTIONS='--max-old-space-size=4096'解决了该错误,如下例所示。

{
  "scripts": {
    "build": "NODE_OPTIONS='--max-old-space-size=4096' next build"
  }
}

It increases the Node.js heap size.它增加了 Node.js 堆大小。 For me 4096 was enough but please adjust it as per your project needs.对我来说4096就足够了,但请根据您的项目需要进行调整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM