angular: JavaScript heap out of memory when using cleanup-unused-imports
Which @angular/* package(s) are the source of the bug?
core
Is this a regression?
Yes
Description
Every time I execute the ng generate @angular/core:cleanup-unused-imports schematic on my project, I get a “FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory”
Is it possible to add a parameter to make it run on a folder level like the control flow schematic?
Please provide a link to a minimal reproduction of the bug
No response
Please provide the exception or error you saw
Preparing analysis for projects/first-app/tsconfig.app.json
Preparing analysis for projects/first-lib/tsconfig.lib.json
Preparing analysis for projects/first-lib/tsconfig.lib.prod.json
Preparing analysis for projects/shared-lib/tsconfig.lib.json
Preparing analysis for projects/shared-lib/tsconfig.lib.prod.json
Preparing analysis for projects/second-app-cockpit/tsconfig.app.json
Preparing analysis for projects/second-app-shared-lib/tsconfig.lib.prod.json
Preparing analysis for projects/second-app-shared-lib/tsconfig.lib.json
Preparing analysis for projects/second-app-dashboard/tsconfig.app.json
Preparing analysis for projects/third-app-cockpit/tsconfig.app.json
Preparing analysis for projects/third-app-shared-lib/tsconfig.lib.prod.json
Preparing analysis for projects/third-app-shared-lib/tsconfig.lib.json
Preparing analysis for projects/fourth-app-cockpit/tsconfig.app.json
Preparing analysis for projects/fourth-app-shared-lib/tsconfig.lib.prod.json
Preparing analysis for projects/fourth-app-shared-lib/tsconfig.lib.json
Preparing analysis for projects/first-app/tsconfig.spec.json
<--- Last few GCs --->
[13008:000001AA4F9FC0B0] 204999 ms: Mark-Compact 4051.8 (4141.3) -> 4037.2 (4142.5) MB, 1323.39 / 0.00 ms (average mu = 0.236, current mu = 0.125) allocation failure; scavenge might not succeed
[13008:000001AA4F9FC0B0] 207571 ms: Mark-Compact 4055.6 (4145.1) -> 4043.1 (4148.1) MB, 2470.53 / 0.00 ms (average mu = 0.123, current mu = 0.039) allocation failure; scavenge might not succeed
<--- JS stacktrace --->
FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory
1: 00007FF63B18413F node::SetCppgcReference+15695
2: 00007FF63B0FEA56 EVP_MD_meth_get_input_blocksize+78566
3: 00007FF63B100841 EVP_MD_meth_get_input_blocksize+86225
4: 00007FF63BB6A5E1 v8::Isolate::ReportExternalAllocationLimitReached+65
5: 00007FF63BB53D78 v8::Function::Experimental_IsNopFunction+1336
6: 00007FF63B9B55E0 v8::Platform::SystemClockTimeMillis+659552
7: 00007FF63B9B2668 v8::Platform::SystemClockTimeMillis+647400
8: 00007FF63B9C797A v8::Platform::SystemClockTimeMillis+734202
9: 00007FF63B9C81F7 v8::Platform::SystemClockTimeMillis+736375
10: 00007FF63B9D6B1F v8::Platform::SystemClockTimeMillis+796063
11: 00007FF63B6970E5 v8::CodeEvent::GetFunctionName+116773
12: 00007FF5DBBDAAFA
Please provide the environment you discovered this bug in (run ng version)
Angular CLI: 19.1.5
Node: 22.13.0
Package Manager: npm 11.0.0
OS: win32 x64
Angular: 19.1.4
... animations, common, compiler, compiler-cli, core, forms
... language-service, localize, platform-browser
... platform-browser-dynamic, router
Package Version
---------------------------------------------------------
@angular-devkit/architect 0.1901.5
@angular-devkit/build-angular 19.1.5
@angular-devkit/core 19.1.5
@angular-devkit/schematics 19.1.5
@angular/cdk 19.1.2
@angular/cli 19.1.5
@schematics/angular 19.1.5
ng-packagr 19.1.2
rxjs 7.8.1
typescript 5.7.3
zone.js 0.15.0
Anything else?
No response
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 18
- Comments: 16 (5 by maintainers)
Commits related to this issue
- fix(core): reduce total memory usage of various migration schematics This commit changes Tsurge's operation within angular-devkit (i.e. the CLI) to no longer retain all programs across all migrations... — committed to JoostK/angular by JoostK 2 years ago
- fix(core): reduce total memory usage of various migration schematics This commit changes Tsurge's operation within angular-devkit (i.e. the CLI) to no longer retain all programs across all migrations... — committed to JoostK/angular by JoostK 2 years ago
- fix(core): reduce total memory usage of various migration schematics (#60774) This commit changes Tsurge's operation within angular-devkit (i.e. the CLI) to no longer retain all programs across all m... — committed to angular-robot/angular by JoostK 2 years ago
- fix(core): reduce total memory usage of various migration schematics (#60776) This commit changes Tsurge's operation within angular-devkit (i.e. the CLI) to no longer retain all programs across all m... — committed to angular/angular by JoostK 2 years ago
I think I have a fix for this in #60774. It chooses a different trade-off w.r.t. run time versus memory usage, considering that memory usage has revealed to cause OOM crashes.
For “complex” migrations (signal-inputs and signal-queries) this change does introduce overhead in the form of having to create the TypeScript program for all entrypoints (i.e. tsconfig paths) twice, but the reduced memory pressure is expected to reduce garbage collection overhead. I don’t have a feeling for the overall impact on execution time, but the reduced memory usage should allow the migration to run to completion instead of crashing.
I work on a pretty large angular project with several apps built from the same codebase and ended up having to do this to get some migrations to run.
Set the heap size in the terminal.
export NODE_OPTIONS=--max_old_space_size=16384Then start running the problematic migrations.
This error seems applicable to more migrations, but I’ll go into detail about this imports one in a moment.
I just tried
ng generate @angular/core:signal-input-migrationscoped to a single folder but since the schematic still reads all thetsconfigs in a project it ran out of memory in a big workspace monorepo at about 20 “Preparing analysis for:” steps. This seems on point with my testing of this particular issue’s schematics for unused importsI tested the unused imports schematic. The bottleneck seems to definitely be the “Preparing”
tsconfig.xyz.jsonsteps.I stress tested this with a repro with 20 applications (10 SPA, 10 SSR) aka 42 configs with the apps + test + 2 root ones. Freshly generated apps with no modifications. https://github.com/michael-small/test-cleanup-unused/tree/20-apps. On my WSL installation on the latest versions(edit: I guess it was 19.1, but the same type of errors in this applied in my latest versions projects), my particular specs gave up on the 29th prepare step consistently.edit: I made a repro with 10 SPA and 20 SSR fresh apps with the actual latest versions. Dropping the
ng versionin the README to save space on this page lol. I stress tested this with a repro with 30 applications (10 SPA, 20 SSR) aka 62 configs with teh apps + test + 2 root ones. Fresh apps. My smaller repro blew over at around 29 prepare steps consistently and my macbook in this repro failed around 57 in.I know that number surely varies with specs, but the consistent thing is I noticed the slowdown near my memory threshhold is during the “preparing” steps. However, if the CLI can manage all of those and get a couple of “Scanning” steps, then it could consistently finish with no memory issues. And anything under my particular out of memory threshold with prepare steps seemed to consistently pass.
Generating more repos by +1ing the project name and then removing their entry in the
angular.jsonin my opinion is a good repro benchmarking for this bug.@devversion Other migrations run fine. It’s just this one that runs out of memory. It’s a big Nx repo. It would be nice if it allowed us to run this on a subset of the repo at a time. Or just didn’t run out of memory 😄
Thanks! @devversion and @chrisczach . Just tried it with
export NODE_OPTIONS=--max_old_space_size=16384and it worked!Hey all, just discovered this issue and would like to help.
node --max_old_space_size=8000 ./node_modules/@angular/cli/bin/ng?This behavior is also encountered for
@angular/core:output-migrationand@angular/core:signal-queries-migrationand@angular/core:cleanup-unused-imports, impacting any process that requires extensive preparation.