致命错误:接近堆限制的无效标记压缩分配失败 - JavaScript 堆内存不足 Angular 13 应用程序

Posted

技术标签:

【中文标题】致命错误:接近堆限制的无效标记压缩分配失败 - JavaScript 堆内存不足 Angular 13 应用程序【英文标题】:FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory Angular 13 application 【发布时间】:2022-01-02 16:43:34 【问题描述】:

我已将 Angular 应用程序从 12 升级到 13,并添加了 eslint 和 Prettier。

在执行 ng lint 时,我遇到了以下错误

macbook@macbooks-MacBook-Air LegalRegTech.Web.Host % ng lint

Linting "abp-zero-template"...

<--- Last few GCs --->

[18697:0x7f8814800000]   899728 ms: Mark-sweep (reduce) 2036.5 (2081.8) -> 2035.8 (2082.5) MB, 6232.4 / 0.0 ms  (average mu = 0.155, current mu = 0.048) allocation failure scavenge might not succeed
[18697:0x7f8814800000]   906650 ms: Mark-sweep (reduce) 2036.1 (2082.5) -> 2036.0 (2082.5) MB, 6313.6 / 0.0 ms  (average mu = 0.122, current mu = 0.088) allocation failure GC in old space requested


<--- JS stacktrace --->

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - javascript heap out of memory
 1: 0x101c5a0e5 node::Abort() [/usr/local/bin/node]
 2: 0x101c5a268 node::OnFatalError(char const*, char const*) [/usr/local/bin/node]
 3: 0x101dd0597 v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [/usr/local/bin/node]
 4: 0x101dd0533 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [/usr/local/bin/node]
 5: 0x101f6ee55 v8::internal::Heap::FatalProcessOutOfMemory(char const*) [/usr/local/bin/node]
 6: 0x101f72e7b v8::internal::Heap::RecomputeLimits(v8::internal::GarbageCollector) [/usr/local/bin/node]
 7: 0x101f6f75c v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::GCCallbackFlags) [/usr/local/bin/node]
 8: 0x101f6cc0a v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [/usr/local/bin/node]
 9: 0x101f79f60 v8::internal::Heap::AllocateRawWithLightRetrySlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [/usr/local/bin/node]
10: 0x101f79fe1 v8::internal::Heap::AllocateRawWithRetryOrFailSlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [/usr/local/bin/node]
11: 0x101f46f37 v8::internal::Factory::NewFillerObject(int, bool, v8::internal::AllocationType, v8::internal::AllocationOrigin) [/usr/local/bin/node]
12: 0x1022f2fbf v8::internal::Runtime_AllocateInOldGeneration(int, unsigned long*, v8::internal::Isolate*) [/usr/local/bin/node]
13: 0x102694d99 Builtins_CEntry_Return1_DontSaveFPRegs_ArgvOnStack_NoBuiltinExit [/usr/local/bin/node]
14: 0x106be229f 
zsh: abort      ng lint

这里是 eslint 配置


  "root": true,
  "ignorePatterns": [
    "projects/**/*"
  ],
  "overrides": [
    
      "files": [
        "*.ts"
      ],
      "parserOptions": 
        "project": [
          "src/tsconfig.json",
          "e2e/tsconfig.json"
        ],
        "createDefaultProgram": true
      ,
      "extends": [
        "plugin:@angular-eslint/recommended",
        "plugin:@angular-eslint/template/process-inline-templates"
      ],
      "plugins": ["prettier"],
      "rules": 
        "prettier/prettier": ["error"],
        "@angular-eslint/component-selector": [
          "error",
          
            "prefix": "app",
            "style": "kebab-case",
            "type": "element"
          
        ],
        "@angular-eslint/directive-selector": [
          "error",
          
            "prefix": "app",
            "style": "camelCase",
            "type": "attribute"
          
        ]
      
    ,
    
      "files": [
        "*.html"
      ],
      "extends": [
        "plugin:@angular-eslint/template/recommended"
      ],
      "rules": 
    
  ]

package.json 中的脚本

"scripts": 
    "ng": "ng",
    "publish": "ng build --prod",
    "start": "ng serve --host 0.0.0.0 --port 4200",
    "hmr": "ng serve --host 0.0.0.0 --port 4200 --hmr",
    "test": "node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng test",
    "pree2e": "webdriver-manager update --standalone false --gecko false",
    "e2e": "protractor",
    "verbose": "ng serve --host 0.0.0.0 --port 4200 --verbose",
    "bundle-report": "webpack-bundle-analyzer dist/stats.json",
    "analyze": "ng build --prod --stats-json && webpack-bundle-analyzer dist/stats.json",
    "memstart": "node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng serve --host 0.0.0.0 --port 4200",
    "start-dev": "node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng serve --host 0.0.0.0 --port 4200 --configuration=development --open",
    "lint": "ng lint"
  ,

【问题讨论】:

【参考方案1】:

您遇到了ng serve 的内存限制。尝试增加限制:

package.json

"serve": "node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng serve"

【讨论】:

我使用的是 ng lint 而不是 ng serve 用“lint”目标试试 你能显示代码吗,我试过了,但对我不起作用【参考方案2】:

添加 "colors": "1.4.0" 到 angular package.json 中的分辨率

示例 “决议”: "fstream/**/mkdirp": "~0.5.1", “颜色”:“1.4.0”

【讨论】:

以上是关于致命错误:接近堆限制的无效标记压缩分配失败 - JavaScript 堆内存不足 Angular 13 应用程序的主要内容,如果未能解决你的问题,请参考以下文章

致命错误:接近堆限制的无效标记压缩分配失败 - JavaScript 堆内存不足 Angular 13 应用程序

致命错误:接近堆限制的无效标记压缩分配失败 - 使用 fs 处理大文件时 JavaScript 堆内存不足

致命错误:堆限制附近的无效标记压缩分配失败 - JavaScript 堆内存不足 |反应

如何修复“致命错误:堆限制附近的无效标记压缩分配失败 - JavaScript 堆内存不足”错误

Nuxt 生成致命错误:在堆限制附近的无效标记压缩分配失败 - JavaScript 堆内存不足

错误:接近堆限制的无效标记压缩分配失败 - JavaScript 堆内存不足