接近堆限制的无效标记压缩分配失败 - Windows Angular

Ineffective mark-compacts near heap limit Allocation failed - Windows Angular

我在 Windows10 中的 Angular 应用程序,在使用 ng build --prod 时出现错误。这适用于 ng build

我的项目使用 assets 文件夹中的 4 个 json 文件。一个文件大小为 21 MB,另外 3 个文件大小为 4-5 MB。如果我删除 21MB 的文件,它工作正常。

PS D:\ProjectPath\Project1> ng build --prod
Option "extractCss" is deprecated: Deprecated since version 11.0. No longer required to disable CSS extraction for HMR.
⠏ Generating browser application bundles (phase: building)...
<--- Last few GCs --->

[17748:00000150A9FB1CE0]   191749 ms: Mark-sweep 2033.1 (2052.3) -> 2032.4 (2053.3) MB, 1579.1 / 0.1 ms  (average mu = 0.135, current mu = 0.011) allocation failure scavenge might not succeed
[17748:00000150A9FB1CE0]   194133 ms: Mark-sweep 2034.3 (2053.3) -> 2032.9 (2053.1) MB, 2361.8 / 0.1 ms  (average mu = 0.063, current mu = 0.009) allocation failure scavenge might not succeed


<--- JS stacktrace --->

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

    0: ExitFrame [pc: 00007FF7D832B3BD]
    1: StubFrame [pc: 00007FF7D82B1861]
Security context: 0x00b2bd4808d1 <JSObject>
    2: __staticCloseRef [00000081B4335C81] [D:\ProjectPath\Project1\node_modules\@angular-devkit\build-angular\node_modules\eslint-scope\lib\scope.js:~293] [pc=0000014C9D9EFFFD](this=0x02d92c6dbc51 <Scope map = 000000A0B9AAA739>,0x02d92c6dcba9 <Reference map = 000000A...

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
 1: 00007FF7D7715EBF napi_wrap+114095
 2: 00007FF7D76C0B46 v8::base::CPU::has_sse+66998
 3: 00007FF7D76C1946 v8::base::CPU::has_sse+70582
 4: 00007FF7D7ED6E4E v8::Isolate::ReportExternalAllocationLimitReached+94
 5: 00007FF7D7EBEF21 v8::SharedArrayBuffer::Externalize+833
 6: 00007FF7D7D8B18C v8::internal::Heap::EphemeronKeyWriteBarrierFromCode+1436
 7: 00007FF7D7D963C0 v8::internal::Heap::ProtectUnprotectedMemoryChunks+1312
 8: 00007FF7D7D92EE4 v8::internal::Heap::PageFlagsAreConsistent+3204
 9: 00007FF7D7D886E3 v8::internal::Heap::CollectGarbage+1283
10: 00007FF7D7D86D54 v8::internal::Heap::AddRetainedMap+2452
11: 00007FF7D7DA809D v8::internal::Factory::NewFillerObject+61
12: 00007FF7D7B0E1E1 v8::internal::interpreter::JumpTableTargetOffsets::iterator::operator=+1665
13: 00007FF7D832B3BD v8::internal::SetupIsolateDelegate::SetupHeap+546637
14: 00007FF7D82B1861 v8::internal::SetupIsolateDelegate::SetupHeap+48113
15: 0000014C9D9EFFFD

这是我的 Angular.json 文件

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
"Project1": {
  "projectType": "application",
  "schematics": {},
  "root": "",
  "sourceRoot": "src",
  "prefix": "app",
  "architect": {
    "build": {
      "builder": "@angular-devkit/build-angular:browser",
      "options": {
        "outputPath": "dist/Project1",
        "index": "src/index.html",
        "main": "src/main.ts",
        "polyfills": "src/polyfills.ts",
        "tsConfig": "tsconfig.app.json",
        "aot": true,
        "assets": [
          "src/favicon.ico",
          "src/assets"
        ],
        "styles": [
          "./node_modules/@angular/material/prebuilt-themes/indigo-pink.css",
          "node_modules/bootstrap/dist/css/bootstrap.min.css",
          "src/styles.css"
        ]          
      },
      "configurations": {
        "production": {
          "fileReplacements": [
            {
              "replace": "src/environments/environment.ts",
              "with": "src/environments/environment.prod.ts"
            }
          ],
          "optimization": true,
          "outputHashing": "all",
          "sourceMap": false,
          "extractCss": true,
          "namedChunks": false,
          "aot": true,
          "extractLicenses": true,
          "vendorChunk": false,
          "buildOptimizer": true,
          "budgets": [
            {
              "type": "initial",
              "maximumWarning": "5mb",
              "maximumError": "10mb"
            },
            {
              "type": "anyComponentStyle",
              "maximumWarning": "6kb",
              "maximumError": "10kb"
            }
          ]
        }
      }
    },
    "serve": {
      "builder": "@angular-devkit/build-angular:dev-server",
      "disableHostCheck": true,
      "options": {
        "browserTarget": "Project1:build"
      },
      "configurations": {
        "fr": { "browserTarget": "qwikCollaborator:build:fr" },
        "en": {"browserTarget": "qwikCollaborator:build:en" } 
        }       
    },
    "extract-i18n": {
      "builder": "@angular-devkit/build-angular:extract-i18n",
      "options": {
        "browserTarget": "Project1:build"
      }
    },
    "test": {
      "builder": "@angular-devkit/build-angular:karma",
      "options": {
        "main": "src/test.ts",
        "polyfills": "src/polyfills.ts",
        "tsConfig": "tsconfig.spec.json",
        "karmaConfig": "karma.conf.js",
        "assets": [
          "src/favicon.ico",
          "src/assets"
        ],
        "styles": [
          "./node_modules/@angular/material/prebuilt-themes/indigo-pink.css",
          "../node_modules/bootstrap/dist/css/bootstrap.min.css",
          "src/styles.css"
        ],
        "scripts": [
          "node_modules/jquery/dist/jquery.min.js",
          "node_modules/bootstrap/dist/js/bootstrap.bundle.min.js",              
          "/node_modules/popper.js/dist/umd/popper.min.js"
        ]
      }
    },
    "lint": {
      "builder": "@angular-devkit/build-angular:tslint",
      "options": {
        "tsConfig": [
          "tsconfig.app.json",
          "tsconfig.spec.json",
          "e2e/tsconfig.json",
          "tsconfig.worker.json"
        ],
        "exclude": [
          "**/node_modules/**"
        ]
      }
    },
    "e2e": {
      "builder": "@angular-devkit/build-angular:protractor",
      "options": {
        "protractorConfig": "e2e/protractor.conf.js",
        "devServerTarget": "Project1:serve"
      },
      "configurations": {
        "production": {
          "devServerTarget": "Project1:serve:production"
        }
      }
    }
  }
}
  },
  "defaultProject": "Project1",
  "cli": {
    "analytics": "8062720d-180a-4964-84ea-93f7438a70dd"
  }
}

这是我的 package.json 文件

{
  "name": "project1",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",   
    "build:prod": "node --max_old_space_size=4096 node_modules/@angular/cli/bin/ng build --prod --aot",
    "build:": "node --max-old-space-size=4096 node_modules/@angular/cli/bin/ng build --aot",
    "start": "ng serve", 
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@amcharts/amcharts4": "^4.10.13",
    "@amcharts/amcharts4-geodata": "^4.1.19",
    "@angular-devkit/build-webpack": "^0.1102.3",
    "@angular/animations": "~11.2.4",
    "@angular/cdk": "~11.1.0",
    "@angular/common": "~11.2.4",
    "@angular/compiler": "~11.2.4",
    "@angular/core": "~11.2.4",
    "@angular/forms": "~11.2.4",
    "@angular/google-maps": "^11.1.0",
    "@angular/localize": "^11.2.4",
    "@angular/material": "^11.1.0",
    "@angular/platform-browser": "~11.2.4",
    "@angular/platform-browser-dynamic": "~11.2.4",
    "@angular/router": "~11.2.4",
    "@ng-bootstrap/ng-bootstrap": "^9.0.0",
    "bluebird": "^3.7.2",
    "bootstrap": "^4.6.0",
    "bootstrap4-toggle": "^3.6.1",
    "chart.js": "^2.9.4",
    "chartjs-plugin-datalabels": "^0.7.0",
    "hammerjs": "^2.0.8",
    "increase-memory-limit": "^1.0.7",
    "jquery": "^3.6.0",
    "main.js": "0.0.1",
    "mdbootstrap": "^4.19.2",
    "moment": "^2.29.1",
    "ngx-pagination": "^5.0.0",
    "popper.js": "^1.16.1",
    "rxjs": "~6.6.3",
    "stream": "0.0.2",
    "time-ago-pipe": "^1.3.2",
    "timers": "^0.1.1",
    "tslib": "^2.1.0",
    "webpack": "^4.36.0",
    "xml-js": "^1.6.11",
    "zone.js": "~0.11.3"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^0.1102.3",
    "@angular/cli": "^11.2.3",
    "@angular/compiler-cli": "^11.2.4",
    "@angular/language-service": "~11.2.4",
    "@types/jasmine": "~3.6.3",
    "@types/jasminewd2": "~2.0.8",
    "@types/node": "~14.14.22",
    "@types/xml2js": "^0.4.7",
    "codelyzer": "^6.0.1",
    "cross-env": "^7.0.3",
    "jasmine-core": "~3.6.0",
    "jasmine-spec-reporter": "~6.0.0",
    "karma": "~6.0.1",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage-istanbul-reporter": "~3.0.3",
    "karma-jasmine": "~4.0.1",
    "karma-jasmine-html-reporter": "^1.5.4",
    "protractor": "~7.0.0",
    "ts-node": "~9.1.1",
    "tslint": "~6.1.3",
    "typescript": "^4.1.3"
  }
}

我也在 Angular.json 中添加了这个解决方案 也试过这个 https://www.npmjs.com/package/increase-memory-limit 我在 cmd 中执行了 setx NODE_OPTIONS --max_old_space_size=10240,但仍然无法正常工作。

Angular CLI:10.0.5 节点:12.18.3 OS: win32 x64

我该如何解决这个问题?

我在 Visual Studio Code 终端中通过 运行 下面的命令修复了这个问题。

 node --max_old_space_size=8192 ./node_modules/@angular/cli/bin/ng build --prod

ng build --prod的地方我用了上面的命令。

Script 标签在 package.json

  "scripts": {
    "ng": "ng",   
    "build-prod": "node --max_old_space_size=5048 ./node_modules/@angular/cli/bin/ng build --prod",
    "build:": "node --max_old_space_size=8192 ./node_modules/@angular/cli/bin/ng build",
    "start": "ng serve", 
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },