angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

Angular 7/8 FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory

Open arayik-yervandyan opened this issue 7 years ago β€’ 205 comments

🐞 Bug report

Command (mark with an x)

- [ ] new
- [X] build
- [ ] serve
- [ ] test
- [ ] e2e
- [ ] generate
- [ ] add
- [ ] update
- [ ] lint
- [ ] xi18n
- [ ] run
- [ ] config
- [ ] help
- [ ] version
- [ ] doc

Description

With the last merge project build process is not working. I get failure that is pasted below. Tried to change --max-old-space-size=4096 still not working. Any suggestions what this can be ?

πŸ”¬ Minimal Reproduction

run command ng build --prod in angular.json file we have

"production": {
	"optimization": true,
	"outputHashing": "all",
	"sourceMap": false,
	"extractCss": true,
	"namedChunks": false,
	"aot": true,
	"extractLicenses": true,
	"vendorChunk": false,
	"buildOptimizer": true,
	"fileReplacements": [
	{
		"replace": "src/environments/environment.ts",
		"with": "src/environments/environment.prod.ts"
	}
	]
},

πŸ”₯ Exception or Error

Here is the log file.

{ Error: Command failed: ng build --prod --configuration=config --output-path=test --base-href=/test/
FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
 1: 0x8dbaa0 node::Abort() [ng build --prod --configuration=config --output-path=test --base-href=/test/]
 2: 0x8dbaec  [ng build --prod --configuration=config --output-path=test --base-href=/test/]
 3: 0xad83de v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [ng build --prod --configuration=config --output-path=test --base-href=/test/]
 4: 0xad8614 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [ng build --prod --configuration=config --output-path=test --base-href=/test/]
 5: 0xec5c42  [ng build --prod --configuration=config --output-path=test --base-href=/test/]
 6: 0xec5d48 v8::internal::Heap::CheckIneffectiveMarkCompact(unsigned long, double) [ng build --prod --configuration=config --output-path=test --base-href=/test/]
 7: 0xed1e22 v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::GCCallbackFlags) [ng build --prod --configuration=config --output-path=test --base-href=/test/]
 8: 0xed2754 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [ng build --prod --configuration=config --output-path=test --base-href=/test/]
 9: 0xed53c1 v8::internal::Heap::AllocateRawWithRetryOrFail(int, v8::internal::AllocationSpace, v8::internal::AllocationAlignment) [ng build --prod --configuration=config --output-path=test --base-href=/test/]
10: 0xe9d636  [ng build --prod --configuration=config --output-path=test --base-href=/test/]
11: 0xeafee7 v8::internal::Factory::NewLoadHandler(int) [ng build --prod --configuration=config --output-path=test --base-href=/test/]
12: 0xf2f4db v8::internal::LoadHandler::LoadFromPrototype(v8::internal::Isolate*, v8::internal::Handle<v8::internal::Map>, v8::internal::Handle<v8::internal::JSReceiver>, v8::internal::Handle<v8::internal::Smi>, v8::internal::MaybeHandle<v8::internal::Object>, v8::internal::MaybeHandle<v8::internal::Object>) [ng build --prod --configuration=config --output-path=test --base-href=/test/]
13: 0xf36d1f v8::internal::LoadIC::ComputeHandler(v8::internal::LookupIterator*) [ng build --prod --configuration=config --output-path=test --base-href=/test/]
14: 0xf3d94c v8::internal::LoadIC::UpdateCaches(v8::internal::LookupIterator*) [ng build --prod --configuration=config --output-path=test --base-href=/test/]
15: 0xf3dffc v8::internal::LoadIC::Load(v8::internal::Handle<v8::internal::Object>, v8::internal::Handle<v8::internal::Name>) [ng build --prod --configuration=config --output-path=test --base-href=/test/]
16: 0xf42935 v8::internal::Runtime_LoadIC_Miss(int, v8::internal::Object**, v8::internal::Isolate*) [ng build --prod --configuration=config --output-path=test --base-href=/test/]
17: 0x235e3925be1d

🌍 Your Environment


Angular CLI: 7.3.1
Node: 10.15.1
OS: linux x64
Angular: 7.2.6
... animations, common, compiler, compiler-cli, core, forms
... http, platform-browser, platform-browser-dynamic, router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.13.2
@angular-devkit/build-angular     0.13.2
@angular-devkit/build-optimizer   0.13.2
@angular-devkit/build-webpack     0.13.2
@angular-devkit/core              7.3.2
@angular-devkit/schematics        7.3.1
@angular/cdk                      7.3.3
@angular/cli                      7.3.1
@ngtools/webpack                  7.3.2
@schematics/angular               7.3.1
@schematics/update                0.13.1
rxjs                              6.4.0
typescript                        3.1.6
webpack                           4.29.0

arayik-yervandyan avatar Feb 21 '19 13:02 arayik-yervandyan

My Angular builds are taking a lot longer today and some are even failing with a FATAL ERROR.

saxenarajat avatar Jun 19 '19 05:06 saxenarajat

ng serve --prod


** Angular Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ **
 92% chunk asset optimization TerserPlugin                                       
<--- Last few GCs --->

[1730:0x103800000]   217578 ms: Scavenge 1350.0 (1422.7) -> 1349.4 (1423.2) MB, 7.1 / 0.0 ms  (average mu = 0.212, current mu = 0.135) allocation failure 
[1730:0x103800000]   217589 ms: Scavenge 1350.2 (1423.2) -> 1349.6 (1423.7) MB, 7.4 / 0.0 ms  (average mu = 0.212, current mu = 0.135) allocation failure 
[1730:0x103800000]   217606 ms: Scavenge 1350.4 (1423.7) -> 1350.1 (1424.7) MB, 12.6 / 0.0 ms  (average mu = 0.212, current mu = 0.135) allocation failure 


<--- JS stacktrace --->

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

    0: ExitFrame [pc: 0x2d9cf905be3d]
    1: StubFrame [pc: 0x2d9cf90134b0]
Security context: 0x3a5b0e11e6e1 <JSObject>
    2: new SourceNode [0x3a5b0f4ff279] [/Users/rac/Desktop/git/requisite-variety/node_modules/webpack-sources/node_modules/source-map/lib/source-node.js:~35] [pc=0x2d9cfb660621](this=0x3a5b97cf6d31 <SourceNode map = 0x3a5be9ed4219>,aLine=289,aColumn=31,aSource=0x3a5b7b5a01f1 <String[122]: /Users/rac/Desktop/git/requisite...

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
 1: 0x10003ae75 node::Abort() [/usr/local/bin/node]
 2: 0x10003b07f node::OnFatalError(char const*, char const*) [/usr/local/bin/node]
 3: 0x1001a7ae5 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [/usr/local/bin/node]
 4: 0x100572ef2 v8::internal::Heap::FatalProcessOutOfMemory(char const*) [/usr/local/bin/node]
 5: 0x1005759c5 v8::internal::Heap::CheckIneffectiveMarkCompact(unsigned long, double) [/usr/local/bin/node]
 6: 0x10057186f v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::GCCallbackFlags) [/usr/local/bin/node]
 7: 0x10056fa44 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [/usr/local/bin/node]
 8: 0x10057c2dc v8::internal::Heap::AllocateRawWithLigthRetry(int, v8::internal::AllocationSpace, v8::internal::AllocationAlignment) [/usr/local/bin/node]
 9: 0x10057c35f v8::internal::Heap::AllocateRawWithRetryOrFail(int, v8::internal::AllocationSpace, v8::internal::AllocationAlignment) [/usr/local/bin/node]
10: 0x10054bca4 v8::internal::Factory::NewFillerObject(int, bool, v8::internal::AllocationSpace) [/usr/local/bin/node]
11: 0x1007d3b54 v8::internal::Runtime_AllocateInNewSpace(int, v8::internal::Object**, v8::internal::Isolate*) [/usr/local/bin/node]
12: 0x2d9cf905be3d 
Abort trap: 6

npm v 6.4.1 node v10.13.0 "@angular/core": "~7.2.0" "@angular/cli": "~7.3.7",

bhumin3i avatar Jun 20 '19 06:06 bhumin3i

@filipesilva , I gave it a shot with the benchmark tool you gave.

On my computer, it passes, it's really on Azure that it is not. On my computer, with AOT, I get about 2GB for the build. I gave a shot with AOT on/off, because DL is not of a concern for me. I have updated Angular to 8.0.2.

On Azure, without AOT, it works, but I get a Aw Snap out of memory after 15 minutes of inactivity in Chrome. I can't point the finger to AOT just yet because I did move from Angular 6 to Angular 8. Also, on my machine, I don't get this Aw snap (the code is not minified either and I use ng serve).

This whole Angular 8 is giving me quite the headache to run on production. But we wanted to get the benefit of a higher version of TypeScript...

AOT=on

[benchmark] Benchmarking process over 5 iterations, with up to 5 retries.
[benchmark]   ng build 3cconnect --prod --configuration staging (at D:\git\clients\hbti\Portal\hbti-web-master\HBTI.ServiceTech.Web2.Client)
[benchmark] Process Stats
[benchmark]   Elapsed Time: 189010.00 ms (345029.00, 148765.00, 153791.00, 152923.00, 144542.00)
[benchmark]   Average Process usage: 1.33 process(es) (2.64, 1.00, 1.00, 1.00, 1.00)
[benchmark]   Peak Process usage: 2.40 process(es) (8.00, 1.00, 1.00, 1.00, 1.00)
[benchmark]   Average CPU usage: 13.13 % (10.44, 13.75, 14.26, 13.56, 13.65)
[benchmark]   Peak CPU usage: 184.38 % (171.90, 175.00, 196.90, 187.50, 190.60)
[benchmark]   Average Memory usage: 1024.72 MB (1143.24, 1001.46, 1041.20, 953.13, 984.56)
[benchmark]   Peak Memory usage: 1860.07 MB (3000.74, 1577.36, 1616.97, 1516.06, 1589.24)

AOT=off

[benchmark] Benchmarking process over 5 iterations, with up to 5 retries.
[benchmark]   ng build 3cconnect --prod --configuration staging (at D:\git\clients\hbti\Portal\hbti-web-master\HBTI.ServiceTech.Web2.Client)
[benchmark] Process Stats
[benchmark]   Elapsed Time: 233620.00 ms (233375.00, 231213.00, 226946.00, 249170.00, 227396.00)
[benchmark]   Average Process usage: 1.00 process(es) (1.00, 1.00, 1.00, 1.00, 1.00)
[benchmark]   Peak Process usage: 1.00 process(es) (1.00, 1.00, 1.00, 1.00, 1.00)
[benchmark]   Average CPU usage: 15.51 % (16.71, 15.90, 15.03, 14.74, 15.16)
[benchmark]   Peak CPU usage: 232.84 % (240.70, 306.30, 206.20, 236.00, 175.00)
[benchmark]   Average Memory usage: 1474.39 MB (1467.56, 1476.16, 1427.16, 1526.26, 1474.80)
[benchmark]   Peak Memory usage: 2213.60 MB (2217.81, 2224.59, 2184.01, 2224.69, 2216.90)

jsgoupil avatar Jun 20 '19 15:06 jsgoupil

same issue on ubuntu server 18.04, but this solved my problem: node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng build --prod

Just dropping this in here but if anyone is using AWS codebuild make sure to update your build environment to be configured with more than the minimum 3GB memory, I followed the steps above but had to re-configure my environment with more memory.

gpazo avatar Jun 21 '19 16:06 gpazo

Heya all,

First of all sorry for the delay. I AFK for a good chunk of last week.

I'm trying to give as much detail about what I did to investigate this sort of problem because in the past I have been asked about it (looking at you @wardbell, @johnpapa, and @DanWahlin :wave:). I also find it useful for myself to come back to these write-ups in the future because I forget the details of how to do these things, or want to share with colleagues so they can do the same. A lot of work described here is done by fiddling directly in node_modules and changing logic in there to write files that I later debug, or log information, or use different packages, stuff like that. I find that fiddling with the JS sources really is the best way of investigating things because I don't know what I'm looking for most of the time, so it helps to explore.

At this point we've already ascertained that Node 12 has a default 2048MB memory limit (https://github.com/nodejs/node/issues/28202) and is using more memory than Node 10 (https://github.com/nodejs/node/issues/28205). This is unfortunate but a reality at the moment, so we should focus on Node 10 for now while the node folks address memory issues. I'm using Node 10.16.0 myself for testing.

I've been working on profiling where memory is used in the problematic builds. The first step to do that is decide on what project to get profiling data from. I chose @johnpapa's awesome-angular-workshop because in https://github.com/angular/angular-cli/issues/13734#issuecomment-500849058 I came to the conclusion it was the project where we could see the biggest impact:

awesome-angular-workshop suffers a disproportionate increase in memory usage (45%) and build time going (150%) from CLI7 to CLI8DL. This indicates a localized regression.

I made a fork of it at https://github.com/filipesilva/awesome-angular-workshop, commit SHA 9076a3d, with local modifications to get reproducible benchmarks easily. That's the code I used in https://github.com/filipesilva/angular-cli-perf-benchmark. The build command I use is ng build 5-ngrx-end --prod, since this workspace has several projects and I just want to use this specific one.

My initial idea was to use the Chrome Node DevTools, gather a bunch of profiling data, then work away at trying to explore it to find actionable information.

To open the Chrome Node DevTools, open chrome://inspect/#devices in Chrome and click "Open dedicated DevTools for Node". When you have a node process started with the --inspect-brk flag, DevTools should automatically attach. An easy way to run Angular CLI like this is to make a npm script: "ng-inspect": "node --inspect-brk node_modules/@angular/cli/bin/ng",. Then if you run npm run ng-inspect -- build --prod and have DevTools open, it will automatically connect. At this point you can set breakpoints and take profiles.

The memory tab has a couple of different profiling tools and descriptions of what they do. When you use these, you will have to go to sources and click the resume script execution (top right) before things happen. I spent some time trialling the different types of profiles on simpler node processes to figure out which one would be better. I was already used to reading the CPU profiles from when I debug build time, so the Allocation sampling (aka heap profile) sounded interesting because it should show how much memory each function is using at a given time. Heap snapshots show whats in memory at a given time and what is retaining those objects, it would also work but isn't as easy to read and get the big picture of what's happening.

To be honest capturing profiles with DevTools has been a lot harder than I expected. It actually does not seem to deal well with capturing profiles from large running Node processes. I saw it crashing while taking profiles, or at the end when processing them, for most of the profiles I tried to take.

I tried to explore alternatives. One of them was to profile smaller parts at a time so that DevTools didn't crash, but it looked like it would always crash on some types of profiles anyway, even on new CLI projects, so that didn't seem to matter much. I tried ndb but it seemed to suffer from the same problems. automated-chrome-profiling seemed promising byt hadn't been updated in a while, and only took CPU profiles. @manekinekko suggested but as far as I could tell it wouldn't give me very granular information on memory usage. node-oom-heapdump looked good for heap snapshots but required native bindings, which I didn't like much because it would make it harder to later integrate directly into the CLI for user profiling. sampling-heap-profiler looked great for heap profiles, but also needed native bindings. @ofrobots suggested https://github.com/google/pprof-nodejs which I tried and seemed to give me really nice visualizations, but it also needed native bindings and needed a go tool to read it, which would make it harder to integrate.

Ultimately I opted to make some custom profiling code based on the Node experimental profiler support described here. I drew inspiration from code in sampling-heap-profiler and the profiler API to figure out what to use. From this I prototyped some automated profiling code that looks like https://github.com/angular/angular-cli/pull/14936. At the time of writing I manually control flags inside the file but maybe in the future we can expose it somehow. It can take CPU profiles of the whole process, or heap snapshot/profiles at set intervals. This seemed like a good way of doing it because I might want to ask people with build speed or memory problems in the future to take these profiles and send to us.

With this setup I was able to take reproducible profiles with minimal fiddling around. Reducing the fiddling is really important when it takes a long time to gather your information, because the more fiddling you need to do, the higher the likelyhood of you botching a given sample. If you botch a sample and only find it hours later, then you might have to throw away most of your conclusions for that period since your starting points were bad and so conclusions and comparisons weren't accurate.

Armed with this need tool I managed to capture a few heap profiles of CLI7 and CLI8 (without differential loading) builds to compare them. I opened them on DevTools and tried to figure out what parts of the build pipeline were involved in each section of the profile.

The CLI7 one looked like this: cli7-annotated

The CLI8 one looked like this: cli8-annotated

CLI8 uses around 180mb more memory, for a total of 760mb, versus the 580mb used by CLI7. What stood out the most to me was the difference in CSS processing. It looks like it takes a lot longer in CLI8. In CLI8 we started using dart-sass (published in npm as just sass) by default instead of node-sass (see https://github.com/angular/angular-cli/pull/11791, https://github.com/angular/angular-cli/pull/13950) so it's a great candidate for investigation.

We expected dart-sass to not be as performant as node-sass so we let you go back to node-sass by installing it in your project (npm install node-sass --save-dev). As long as it's installed, we'll pick it up automatically. You can also use dart-sass with the fibers package for better performance by installing it in the same way as node-sass. Both node-sass and fibers use native bindings and might not work on all machines, which is why we don't install them by default.

Then I compared profiles for CLI8, CLI8 with node-sass, CLI8 with fibers, and CLI 7 side by side to see how they changed. sass-comparisons

So using node-sass seems to return memory usage CLI7-levels, at least on this project. Using just fibers only reduces memory by some 20mb.

As @clydin pointed out, this might not be the full picture since the heap profiles are only for memory managed by V8. node-sass might be using more memory that doesn't show up in the profile. So I benchmarked the processes themselves:

  • CLI 8
[benchmark] Benchmarking process over 5 iterations, with up to 5 retries.
[benchmark]   ng build 5-ngrx-end --prod (at D:\sandbox\memory-debug\awesome-angular-workshop)
[benchmark] Process Stats
[benchmark]   Elapsed Time: 157486.80 ms (157292.00, 159517.00, 156464.00, 159606.00, 154555.00)
[benchmark]   Average Process usage: 1.00 process(es) (1.00, 1.00, 1.00, 1.00, 1.00)
[benchmark]   Peak Process usage: 1.00 process(es) (1.00, 1.00, 1.00, 1.00, 1.00)
[benchmark]   Average CPU usage: 18.31 % (18.68, 18.63, 18.04, 18.76, 17.46)
[benchmark]   Peak CPU usage: 205.60 % (237.50, 204.60, 198.40, 196.90, 190.60)
[benchmark]   Average Memory usage: 1008.24 MB (1048.68, 975.86, 1017.15, 1020.31, 979.21)
[benchmark]   Peak Memory usage: 1560.48 MB (1588.38, 1489.02, 1583.70, 1574.51, 1566.81)
  • CLI 8 with node-sass
[benchmark] Benchmarking process over 5 iterations, with up to 5 retries.
[benchmark]   ng build 5-ngrx-end --prod (at D:\sandbox\memory-debug\awesome-angular-workshop)
[benchmark] Process Stats
[benchmark]   Elapsed Time: 103204.00 ms (98623.00, 103302.00, 105260.00, 104674.00, 104161.00)
[benchmark]   Average Process usage: 1.00 process(es) (1.00, 1.00, 1.00, 1.00, 1.00)
[benchmark]   Peak Process usage: 1.00 process(es) (1.00, 1.00, 1.00, 1.00, 1.00)
[benchmark]   Average CPU usage: 16.70 % (15.46, 15.94, 17.36, 17.48, 17.26)
[benchmark]   Peak CPU usage: 152.50 % (160.90, 89.10, 190.60, 128.10, 193.80)
[benchmark]   Average Memory usage: 676.14 MB (681.11, 674.49, 676.65, 675.08, 673.38)
[benchmark]   Peak Memory usage: 1443.15 MB (1444.21, 1439.97, 1435.09, 1449.96, 1446.53)
  • CLI 8 with fibers
[benchmark] Benchmarking process over 5 iterations, with up to 5 retries.
[benchmark]   ng build 5-ngrx-end --prod (at D:\sandbox\memory-debug\awesome-angular-workshop)
[benchmark] Process Stats
[benchmark]   Elapsed Time: 126098.00 ms (136145.00, 125063.00, 123654.00, 123849.00, 121779.00)
[benchmark]   Average Process usage: 1.00 process(es) (1.00, 1.00, 1.00, 1.00, 1.00)
[benchmark]   Peak Process usage: 1.00 process(es) (1.00, 1.00, 1.00, 1.00, 1.00)
[benchmark]   Average CPU usage: 14.55 % (16.18, 14.48, 14.25, 14.05, 13.79)
[benchmark]   Peak CPU usage: 190.30 % (215.50, 173.40, 184.40, 170.30, 207.90)
[benchmark]   Average Memory usage: 746.22 MB (752.84, 741.56, 745.46, 747.52, 743.72)
[benchmark]   Peak Memory usage: 1560.88 MB (1603.68, 1555.96, 1575.40, 1569.62, 1499.73)

So it does look like the node-sass really is the one that uses less memory, and takes less time to build. fibers is the second best. The average memory used in the fibers case seems to conflict with the results we got from the heap profiles though, here we see CLI8 with fibers using ~750mb versus the original CLI8 ~1000mb, but in the heap profile using fibers only reduced memory by ~20mb. I don't know why this is. Maybe it's because of the nature of averaging used memory, or maybe it's because there are more garbage collection cycles taking heap profiles. But at the end of the day node-sass seems to be the winner as far as performance goes.

I also found it weird that a big chunk of extra memory in the CLI8 heap profile seems to come from PostCSS processing. I wasn't expecting PostCSS to need to do anything different. I thought that maybe I just misinterpreted what that section was used for. But maybe the dart-sass output could be different from node-sass and that could cause more work for PostCSS. It was worth looking into so I added some code directly in sass-loader to write the results of sass compilation to disk. The output for dart-sass and node-sass seemed like it was the same really, aside for some whitespace, so I think I just misinterpreted the memory assigned to Sass processing as being assigned to PostCSS.

@johnpapa, as an aside, I did notice that most component CSS files in this project are importing mixin.scss. This file actually ~has no mixins and is just~ includes the whole material theme at 70kb (pre-optimizations) of toplevel CSS definitions. Component CSS is standalone and isolated from other CSS, so there is no deduplication or anything. When it's imported into component CSS, you're adding 70kb worth of CSS into that component, which is probably several times over the size of the component itself. @jelbourn has described importing non-mixin Sass this as an anti-pattern but it's not the sort of information that's obvious all round. We could do a better job of informing people about it, and warning them when component CSS is very large (cc @vikerman).

Another aside is that I also noticed that compiling just the single project via ng build 5-ngrx-end --prod actually compiles all .ts and .scss files inside src/. This happens because all projects in this workspace use the same ./src/tsconfig.app.json, and this tsconfig includes all ts files (except unit tests). Which is to say, compiling a single project here actually compiles all projects. The only way to truly address this is to have one tsconfig for each app. I wonder how common this mistake is. I wouldn't be surprised larger projects accidentally include extra TS files. Maybe we could warn users when we see the TS compilation has a lot more files than those that webpack loads (cc @vikerman, @clydin).

So I guess the takeaway here is that if you have a large project, you should install node-sass to your devDependencies. This will make building faster and take less memory.

Although it didn't seem to change between CLI7 and CLI8, the Angular compilation itself is still by far the largest contributor to the memory footprint, at around 230mb. Maybe we can fully drop all references to it when not building in watch mode, allowing Node to free up that memory. I'll explore that next.

filipesilva avatar Jun 28 '19 14:06 filipesilva

@filipesilva just a short update: I tried using node-sass with angular 8 for super-productivity. The result is the same:

  • all builds work without sourcemaps
  • ng build --aot & ng serve both work with sourcemaps
  • ng build --prod throws the JavaScript heap out of memory error

I tried both Node 10 and 12. The results are the same.

johannesjo avatar Jun 28 '19 15:06 johannesjo

@johannesjo ng build --aot and ng serve are vastly different than ng build --prod. I don't think there's anything to be gained by comparing them. Production builds will always use more memory than non-production builds.

When I benchmarked super-productivity I got these results for Node 10.16.0:

  • CLI version 8 with differential loading
[benchmark] Benchmarking process over 5 iterations, with up to 5 retries.
[benchmark]   ng build --prod (at /home/circleci/project/project)
[benchmark] Process Stats
[benchmark]   Elapsed Time: 203022.00 ms (320720.00, 185220.00, 170860.00, 155420.00, 182890.00)
[benchmark]   Average Process usage: 1.35 process(es) (2.75, 1.00, 1.00, 1.00, 1.00)
[benchmark]   Peak Process usage: 1.80 process(es) (5.00, 1.00, 1.00, 1.00, 1.00)
[benchmark]   Average CPU usage: 164.51 % (154.16, 163.92, 168.56, 171.13, 164.77)
[benchmark]   Peak CPU usage: 557.11 % (650.00, 555.56, 520.00, 520.00, 540.00)
[benchmark]   Average Memory usage: 1333.09 MB (1709.25, 1234.43, 1236.67, 1225.88, 1259.21)
[benchmark]   Peak Memory usage: 2019.59 MB (2671.33, 1807.51, 1857.44, 1883.19, 1878.46)
  • CLI version 8 without differential loading
[benchmark] Benchmarking process over 5 iterations, with up to 5 retries.
[benchmark]   ng build --prod (at /home/circleci/project/project)
[benchmark] Process Stats
[benchmark]   Elapsed Time: 87418.00 ms (91750.00, 94150.00, 92950.00, 77520.00, 80720.00)
[benchmark]   Average Process usage: 1.03 process(es) (1.13, 1.00, 1.00, 1.00, 1.00)
[benchmark]   Peak Process usage: 1.60 process(es) (4.00, 1.00, 1.00, 1.00, 1.00)
[benchmark]   Average CPU usage: 160.13 % (162.86, 155.83, 158.79, 161.31, 161.87)
[benchmark]   Peak CPU usage: 546.89 % (650.00, 544.44, 520.00, 510.00, 510.00)
[benchmark]   Average Memory usage: 1032.07 MB (1048.05, 1040.10, 1004.07, 999.71, 1068.42)
[benchmark]   Peak Memory usage: 1659.71 MB (1813.00, 1615.65, 1684.55, 1571.28, 1614.09)
  • CLI version 7
[benchmark] Benchmarking process over 5 iterations, with up to 5 retries.
[benchmark]   ng build --prod (at /home/circleci/project/project)
[benchmark] Process Stats
[benchmark]   Elapsed Time: 89746.00 ms (145060.00, 78430.00, 77020.00, 77210.00, 71010.00)
[benchmark]   Average Process usage: 1.36 process(es) (2.81, 1.00, 1.00, 1.00, 1.00)
[benchmark]   Peak Process usage: 1.80 process(es) (5.00, 1.00, 1.00, 1.00, 1.00)
[benchmark]   Average CPU usage: 156.10 % (152.71, 155.33, 157.43, 156.50, 158.52)
[benchmark]   Peak CPU usage: 548.44 % (680.00, 522.22, 520.00, 520.00, 500.00)
[benchmark]   Average Memory usage: 1086.97 MB (1592.65, 963.42, 960.56, 971.31, 946.89)
[benchmark]   Peak Memory usage: 1702.50 MB (2539.70, 1438.24, 1606.59, 1474.61, 1453.35)

For production builds I didn't see a real difference between "CLI version 7" and "CLI version 8 without differential loading". There is a difference of ~200mb when using differential loading though. But you don't seem to be using differential loading currently (your root tsconfig has a es5 target).

We might be comparing different things though. I'm comparing using Angular 8 with both CLI 7 and 8. If you're testing with both Angular 7 and Angular 8 you might different results. What comparison are you making? Do you have two commits I can look at where only the version of Angular packages changed but ng build --prod went from completing successfully to failing with out of memory errors?

filipesilva avatar Jun 28 '19 15:06 filipesilva

@filipesilva The crucial setting seems to be the buildOptimizer flag (or it is at least the one with the biggest impact on memory usage). When I disable it. The build runs fine regardless of the other config params even with sourceMap set to true.

I've no idea how all the internals work, so this is probably not too helpful, but my wild guesses about the cause would be the following:

  • The build optimizations are somehow affected by the existence of the sourcemaps or the related code inside the css
  • Sourcemap Extraction is run in parallel with the build optimizations
  • Sourcemap Extraction is run before build optimizations but memory is not cleaned up until the whole build task is run or vice versa
  • Both processes are somehow intertwined

Maybe there is some inspiration about the real cause in it ;)

johannesjo avatar Jun 28 '19 15:06 johannesjo

@johannesjo both Build Optimizer and source maps need extra memory to process so that's expected. It's just more work to be done, and more intermediate data to store in memory. We are looking at moving the Build Optimizer step from app builds to library builds, but that would be in 9 only at best.

Edit: tagged @JohannesHoppe instead of @johannesjo. sorry about that!

filipesilva avatar Jul 01 '19 11:07 filipesilva

Still working on this problem. Notes from the last few days below.

While discussing it with @clydin, he mentioned that the average memory isn't really what busts the limit. It's the peak memory. Average is ~750mb while peak is ~1500mb. We should focus on that instead.

First I need to figure out what's using memory at the peak. I guess it's uglify and build optimizer but I don't know. It's weird that the profiles I take never show peak usage though. I wonder I I'm doing something wrong there. I just started a process without any profiling and see memory at ~1200 during what seems like the ng compilation, then going down to ~900mb when loading files, then ~1300 and ~1500mb at the 90%+ stages. But my profiles show a ~800mb allocation max.

Maybe I need to control the timing of when I take these profiles. If that's it, then I need to find the exact time when usage is high and take a profile there. Using process.memoryUsage() as described in https://www.valentinog.com/blog/memory-usage-node-js/ might let me identify high memory usage periods. I can set a memory threshold and only take profiles at that time.

It might be that taking a heap profile triggers GC, or just that the increased running time prompts GCs more often. I can try to trigger gc right before each heap profile and see if the result is the same. https://stackoverflow.com/questions/27321997/how-to-request-the-garbage-collector-in-node-js-to-run shows how. Did that, and saw no real difference in the profiles. Whatever the profiles show seems to actually be retained.

I used setInterval with a log for process.memoryUsage() inside, and also kept an eye on the reported task manager memory usage, and noticed a couple of things. When I say that I saw 1234/5678 I mean 1234 was on the task manager and 5678 was on the process.memory logging.

The highest 1300/???? happened during the Angular Compiler phase, but only with dart-sass. With node-sass it lower at 900/700 and saw interval logging. I got almost no logging for that phase with dart-sass, I think because the setInterval never got on the event loop properly, I guess because a lot of things are sync? With node-sass I saw a lot of logging at that time. Maybe it's a native binding thing, it might free up the event loop. Anyway dart-sass seems to make resource loading in the angular compiler extra bad by piling on memory on an already expensive set of operations.

I saw some peaks of 1200/900 while progress showed loading individual files. I think that's Build Optimizer since its a js loader.

I barely saw any memory usage during Terser, but I bet that's because of the cache. I turned off the terser-webpack-plugin cache and saw 1100/940, but also a bunch of spawned processes between 40 and 400mb.

So as far as I could tell, the sass thing was actually a big part of reaching the peak because it runs during the ng compilation and piles on something that's already memory intensive.

I bet it doesn't help that we're doing webpack subcompilations for that either. This also means that even if we freed all references to memory using during the ng compilation, it wouldn't matter as much because that compilation is still part of the peak.

We already have plans to remove Build Optimizer so that doesn't bother me much

In regards to terser... Well if we had a smaller main process memory footprint, obviously the total would be reduced. But maybe we can also make that plugin smarter. It seems to run on chunks proper https://github.com/webpack-contrib/terser-webpack-plugin/blob/a0d83170168e786004fa12b94525b2934a17a4f5/src/index.js#L416-L419 But we know terser can't break out of the webpack module loader so there's no point is processing whole chunks. Might as well process the individual webpack modules, that should be much smaller.

So important things to follow up:

  • improve terser-webpack-plugin performance (https://github.com/webpack-contrib/terser-webpack-plugin/issues/104)
  • improve sass-loader performance (https://github.com/webpack-contrib/sass-loader/issues/701)
  • reduce peak ngc memory usage
  • free ngc memory after emit
  • remove build optimizer

filipesilva avatar Jul 03 '19 14:07 filipesilva

Finally, as the other had suggested, running node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng build --prod fixes the problem, and node.exe could use up to 5.5 GB memory for compiling my big fat NG app. Apparently Angular 8 build use much more memory than Angular 7 build.

now for debug build, I use: node --max_old_space_size=8192 "node_modules/@angular/cli/bin/ng" build --base-href /ng/ --aot

for production build, I use: node --max_old_space_size=8192 "node_modules/@angular/cli/bin/ng" build --configuration=production

However, I would prefer to use ng build ... rather than ``node --max_old_space_size=8192 "node_modules/@angular/cli/bin/ng" build`.

It will be nice that NG CLI team would make ng build read max_old_space_size somewhere in config.

BTW, with almost identical code base before moving to NG8, the node memory usage with NG7 build is 1.3 GB. I had seen the others had reported such too much memory usage in NG 8 build.

zijianhuang avatar Jul 04 '19 02:07 zijianhuang

@zijianhuang going from 1.3 to 5.5 is a lot more than in other projects I have tested. Is your project somewhere I could see to run some benchmarks on it? Also, what Node version are you using?

filipesilva avatar Jul 04 '19 11:07 filipesilva

@filipesilva, If you could provide a secured upload location or your Email address, I may zip both the ng7 codes and the ng8 code so you could build both and compare. Node and npm are basically the latest.

zijianhuang avatar Jul 04 '19 12:07 zijianhuang

I'd love to have a look. WDYT of you having a private github project that you grant me access? The private ones are now free for up to three collaborators so you wouldn't pay anything. You can also remove access at any time.

filipesilva avatar Jul 04 '19 13:07 filipesilva

In my case a big difference is having es2015 as target in the tsconfig.json, I wasn't able to use your benchmark tool (haven't found anywhere how to use it) but with time the max rss of es5 is 1.3GB with es2015 5.9GB, after going from node10+angular7 to node12+angular8 all of our builds failed because of the memory usage

alex88 avatar Jul 04 '19 16:07 alex88

@filipesilva , invitation sent. Please also check my benchmark result in readme.txt.

zijianhuang avatar Jul 04 '19 22:07 zijianhuang

@alex88 you can find the benchmark tool I used in https://github.com/filipesilva/angular-cli-perf-benchmark#benchmark-package. If you want to provide me a repro via a private github repository, I'd love to have a look too.

@zijianhuang a big thank you for giving me a repro to look at. I had a look at it today using the benchmark package described in https://github.com/filipesilva/angular-cli-perf-benchmark.

One thing to be aware of is that the memory numbers collected by that tool include spawned processes. In the CLI we use a minifier package (terser-webpack-plugin) that spawns multiple processes, so it's useful to capture the memory the child processes use as well.

But that package also caches the minification results. So the first run does more work than subsequent ones. So when I benchmarked your packages I manually edited ./node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/common.js and changed cache: true to cache: false to disable that cache and get consistent numbers.

I also added these two npm scripts to help me benchmark things:

		"benchmark": "benchmark -- node --max_old_space_size=8000 node_modules/@angular/cli/bin/ng build --prod",
		"ng-high-memory": "node --max_old_space_size=8000 node_modules/@angular/cli/bin/ng build --prod",

I set the same memory limit for all projects because I know Node will behave differently near the limit. So this way we should get more accurate numbers.

I had to remove the Bounds import from avatar.component.ts because that gave me a build error in NG7. It was just used as a type so it was easy to remove. I replaced "target": "es2015" with "target": "es5" in ./tsconfig.json. This disables Differential Loading as described here. I know it's an important part of CLI8 applications but we are already looking at a different implementation of it that shouldn't require as much resources, so I didn't want to also benchmark it. This way I could compare mostly equal applications.

I noticed that your package.json between the two versions changes a lot of packages that aren't just the Angular packages. That might have an impact on the benchmarks. After trying to reproduce your initial numbers I'll also try to alter only Angular packages to see if I get different results.

Dev environment

In your reproduction your said:

Dev Environment:
* Intel Nuc i7, 16 GB memory, SSD
* Windows 10

Benchmarking tool: Process Explorer

I'm using my work machine (Win10, i7, 16GB RAM, SSD) which seems similar to your env. To benchmark I used the tool mentioned in https://github.com/filipesilva/angular-cli-perf-benchmark#benchmark-package. I'm also using Node 10.16.0 and npm 6.9.0.

Your test cases

In your reproduction your said:

Test case 1:
* Node 10.15.3
* npm 6.4.0
* NG 7 and NG CLI 7

Node memory usage peak: 1.3 GB.

I read from the other post that the default limit of memory usage of Node is 1.5 GB. The "NG CLI" command apparently is using such default.


Test case 2:
* Node 10.15.3 / 10.16.0
* npm 6.4.0 / 6.10.0
* NG 8.1 and NG CLI 8.1

Memory usage peak in the DEBUG build: 3.6 GB / 3.5 GB
Memory usage peak in the Production build: 3.3 GB / 3.4 GB


Test case 3:
* Locally installed Node 12.6 through "npm i"
* npm 6.9.0
* NG 8.1 and NG CLI 8.1

Memory usage peak: 5.5 GB

I'm not really going to try Test case 3 because we already know that Node 12 is using more memory in general (https://github.com/nodejs/node/issues/28205). I don't think there's much we can do about that.

Your Test case 1 is the CLI7/NG7/NG7deps scenario. I also say NG7deps because those dependencies are different from your NG8 project, which I'll say has NG8deps. Later on that might make a difference. Ideally we'd only change CLIx/NGx to get numbers.

So for CLI7/NG7/NG7deps (your Test case 1) I got:

$ ng version

Angular CLI: 7.3.9
Node: 10.16.0
OS: win32 x64
Angular: 7.2.15
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, platform-server
... router, service-worker

Package                            Version
------------------------------------------------------------
@angular-devkit/architect          0.13.9
@angular-devkit/build-angular      0.13.9
@angular-devkit/build-optimizer    0.13.9
@angular-devkit/build-webpack      0.13.9
@angular-devkit/core               7.3.3
@angular-devkit/schematics         7.3.3
@angular/cdk                       7.3.3
@angular/cli                       7.3.9
@angular/flex-layout               7.0.0-beta.24
@angular/material                  7.3.3
@angular/material-moment-adapter   7.3.3
@angular/pwa                       0.13.3
@ngtools/webpack                   7.3.9
@schematics/angular                7.3.3
@schematics/update                 0.13.9
rxjs                               6.5.2
typescript                         3.2.2
webpack                            4.29.0
[benchmark] Benchmarking process over 5 iterations, with up to 5 retries.
[benchmark]   node --max_old_space_size=8000 node_modules/@angular/cli/bin/ng build --prod (at D:\sandbox\memory-debug\testngcli8\NG7Source)
[benchmark] Process Stats
[benchmark]   Elapsed Time: 288429.80 ms (297613.00, 292811.00, 282489.00, 284134.00, 285102.00)
[benchmark]   Average Process usage: 2.65 process(es) (2.60, 2.69, 2.66, 2.64, 2.64)
[benchmark]   Peak Process usage: 8.00 process(es) (8.00, 8.00, 8.00, 8.00, 8.00)
[benchmark]   Average CPU usage: 15.64 % (14.82, 16.84, 15.28, 15.27, 16.00)
[benchmark]   Peak CPU usage: 252.80 % (251.60, 300.00, 224.90, 261.00, 226.50)
[benchmark]   Average Memory usage: 1932.92 MB (1885.25, 1947.53, 1937.02, 1953.46, 1941.31)
[benchmark]   Peak Memory usage: 4024.00 MB (4115.83, 4092.19, 3943.85, 4018.98, 3949.16)

And for CLI8/NG8/NG8deps (your Test case 2) I got:

$ ng version

Angular CLI: 8.1.0
Node: 10.16.0
OS: win32 x64
Angular: 8.1.0
... animations, cli, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, platform-server
... router, service-worker

Package                            Version
------------------------------------------------------------
@angular-devkit/architect          0.801.0
@angular-devkit/build-angular      0.801.0
@angular-devkit/build-optimizer    0.801.0
@angular-devkit/build-webpack      0.801.0
@angular-devkit/core               8.1.0
@angular-devkit/schematics         8.1.0
@angular/cdk                       8.0.2
@angular/flex-layout               8.0.0-beta.26
@angular/material                  8.0.2
@angular/material-moment-adapter   8.0.2
@angular/pwa                       0.801.0
@ngtools/webpack                   8.1.0
@schematics/angular                8.1.0
@schematics/update                 0.801.0
rxjs                               6.5.2
typescript                         3.4.5
webpack                            4.35.2
[benchmark] Benchmarking process over 5 iterations, with up to 5 retries.
[benchmark]   node --max_old_space_size=8000 node_modules/@angular/cli/bin/ng build --prod (at D:\sandbox\memory-debug\testngcli8\NG8Source)
[benchmark] Process Stats
[benchmark]   Elapsed Time: 253091.40 ms (281761.00, 251054.00, 242851.00, 244676.00, 245115.00)
[benchmark]   Average Process usage: 2.72 process(es) (3.34, 2.52, 2.61, 2.58, 2.54)
[benchmark]   Peak Process usage: 8.80 process(es) (12.00, 8.00, 8.00, 8.00, 8.00)
[benchmark]   Average CPU usage: 12.94 % (15.28, 12.50, 12.20, 12.55, 12.18)
[benchmark]   Peak CPU usage: 210.62 % (223.50, 287.50, 173.50, 189.00, 179.60)
[benchmark]   Average Memory usage: 1997.28 MB (1980.34, 1920.54, 2051.03, 2025.27, 2009.23)
[benchmark]   Peak Memory usage: 4313.95 MB (4445.09, 4162.86, 4258.15, 4380.61, 4323.05)

So for these two cases I actually got very similar numbers.

Variants

Something I know I did different from you was to ensure the minifier output was never cached. That has a big impact on resource usage. On CI machines you won't get this cache unless you cache node_modules (that's where it is stored) so I always turn it off manually for benchmarks. If it turn it back on this is what I see in CLI8/NG8/NG8deps:

[benchmark] Benchmarking process over 5 iterations, with up to 5 retries.
[benchmark]   node --max_old_space_size=8000 node_modules/@angular/cli/bin/ng build --prod (at D:\sandbox\memory-debug\testngcli8\NG8Source)
[benchmark] Process Stats
[benchmark]   Elapsed Time: 184542.00 ms (240928.00, 167820.00, 169525.00, 176387.00, 168050.00)
[benchmark]   Average Process usage: 1.31 process(es) (2.57, 1.00, 1.00, 1.00, 1.00)
[benchmark]   Peak Process usage: 2.40 process(es) (8.00, 1.00, 1.00, 1.00, 1.00)
[benchmark]   Average CPU usage: 11.67 % (11.08, 11.67, 11.74, 12.13, 11.73)
[benchmark]   Peak CPU usage: 173.76 % (268.80, 187.50, 167.20, 121.90, 123.40)
[benchmark]   Average Memory usage: 1569.17 MB (1997.59, 1421.48, 1607.34, 1424.31, 1395.14)
[benchmark]   Peak Memory usage: 2788.43 MB (4277.19, 2291.22, 2705.78, 2327.46, 2340.52)

Here you can see that the first build (first place inside the parenthesis) uses up more resources than the others. Notably build time and peak memory goes down by about 1/3, and average memory down by around 1/4. These numbers are very similar to the ones you reported for Test case 1.

The other thing I did different was disabling Differential Loading (DL) in the CLI8/NG8/NG8deps case, so I also took a benchmark with it turned back on in CLI8/NG8/NG8deps:

[benchmark] Benchmarking process over 5 iterations, with up to 5 retries.
[benchmark]   node --max_old_space_size=8000 node_modules/@angular/cli/bin/ng build --prod (at D:\sandbox\memory-debug\testngcli8\NG8Source)
[benchmark] Process Stats
[benchmark]   Elapsed Time: 448765.60 ms (450442.00, 441786.00, 453261.00, 442406.00, 455933.00)
[benchmark]   Average Process usage: 2.62 process(es) (2.63, 2.62, 2.65, 2.61, 2.61)
[benchmark]   Peak Process usage: 8.00 process(es) (8.00, 8.00, 8.00, 8.00, 8.00)
[benchmark]   Average CPU usage: 11.07 % (11.65, 11.65, 11.10, 10.18, 10.78)
[benchmark]   Peak CPU usage: 280.28 % (307.70, 356.30, 237.40, 256.20, 243.80)
[benchmark]   Average Memory usage: 2484.00 MB (2467.08, 2548.74, 2480.70, 2471.22, 2452.25)
[benchmark]   Peak Memory usage: 5050.35 MB (5043.85, 5075.19, 5051.67, 5053.34, 5027.68)

So here we see that the build time roughly doubled with DL, which is expected because we do two builds one after the other. We're planning on doing a single builds with an extra step at the end instead soon.

We also see that average used memory has increased by 500mb, and peak memory by 700mb. In https://github.com/angular/angular-cli/issues/13734#issuecomment-500849058 I said:

Using differential loading seems to add a ~200mb to memory usage. This value does not change much, which seems to indicate that something that doesn't depend much on project size is being retained in memory between builds.

The DL results from your project contradict that. In your project it increased by more than 200mb. So there is a scaling aspect to the DL cost that we see in your project. I don't know what it is, but it's worth investigating. It points to memory being retained between builds, which might also imply that memory is being retained between build steps. If it's memory used in an earlier step of the build, maybe we can free it and reduce average memory usage in latter build steps.

@alex88 also mentioned that es2015 builds took more resources so I tried leaving "target": "es2015" in tsconfig.json and disabling DL via the browserslist approach, which in your case is in package.json. I removed the not in "not dead", "not IE 9-11" but that still made DL builds (you can see that by the two builds, one after the other). I had to remove all entries in browserslist before I stopped getting DL builds. We should check why these instructions aren't correct (cc @alan-agius4, I think you worked on this). After I managed to get only ES2015 builds, I took another benchmark:

[benchmark] Benchmarking process over 5 iterations, with up to 5 retries.
[benchmark]   node --max_old_space_size=8000 node_modules/@angular/cli/bin/ng build --prod (at D:\sandbox\memory-debug\testngcli8\NG8Source)
[benchmark] Process Stats
[benchmark]   Elapsed Time: 246739.80 ms (222235.00, 246069.00, 244327.00, 252636.00, 268432.00)
[benchmark]   Average Process usage: 2.55 process(es) (2.55, 2.59, 2.58, 2.56, 2.46)
[benchmark]   Peak Process usage: 8.00 process(es) (8.00, 8.00, 8.00, 8.00, 8.00)
[benchmark]   Average CPU usage: 12.83 % (11.53, 12.41, 12.92, 13.34, 13.97)
[benchmark]   Peak CPU usage: 229.42 % (287.40, 268.90, 197.00, 212.50, 181.30)
[benchmark]   Average Memory usage: 1947.94 MB (1941.51, 2076.98, 1938.72, 1926.15, 1856.36)
[benchmark]   Peak Memory usage: 4129.88 MB (4141.96, 4347.47, 4064.12, 4071.99, 4023.87)

This doesn't seem to be very different from the ES5 benchmark. @alex88 maybe when you used es2015 you were actually getting the DL builds? You should also not be using Node 12 for now because of https://github.com/nodejs/node/issues/28205.

@zijianhuang so to wrap up, I think the reason your project uses more resources with NG8/CLI8 than with NG7/CLI7 is that DL was turned on and that some builds were without cache. I really can't be sure of the second point because I don't know how you're caching things locally and in CI, but the numbers I got here seem to point to that.

For us the follow up from investigating your project's transition from Angular version 7 to 8 is that we really need to figure out what memory is retained between DL builds because that points to a leak, and that we need to have a more performant DL setup (@clydin has some ideas on that).

filipesilva avatar Jul 05 '19 16:07 filipesilva

Hello @filipesilva , I have written a while back on this thread mentioning my benchmarks and what was not working for my case. It seems that the AOT is really not working for me. Is there any investigation being done in this area?

jsgoupil avatar Jul 05 '19 17:07 jsgoupil

@jsgoupil in https://github.com/angular/angular-cli/issues/13734#issuecomment-506760767 I saw a mimimum of 240mb and a maximum of about 1gb of memory being used in the AOT compilation. I tried for a couple of days to figure out exactly where but didn't have much success. There's still a couple more things I want to try though. And I also think the minimum is related to the "carry-over" memory I see on DL builds.

But that being said there is no getting around the fact that AOT does a lot more work and requires more resources. That's not a bug. It's just a much more involved build step. It might be requiring disproportionally more resources than non-AOT, but it will never require less.

Did you see the end comments in https://github.com/angular/angular-cli/issues/13734#issuecomment-506760767 about the compilation units? It might be that your tsconfig is including a lot of files you don't use as well, or that component sass is larger than you expect (if you use sass).

If you give me access to your code somehow I can take a look and see if I find anything odd.

filipesilva avatar Jul 05 '19 17:07 filipesilva

@filipesilva , thank you very much for such detailed and inspirational analysis. I had done some minor changes to the build config, some of which are similar to you CLI team had done during the analysis. I had altered the browserslist:

	"browserslist": [
		"last 2 Chrome versions",
		"last 2 Safari versions",
		"last 2 Firefox versions",
		"not dead",
		"not IE 9-11"
	],

to ensure only es2015 build is produced.

For the DEBUG build, the peak memory usage is 1.9 GB. For the Production build, the peak memory usage is 2.0 GB.

This is actually a small jump from 1.3 GB comparing with build with NG7/NG CLI 7, which also produce only es2015 build. The default 1.5 GB memory limit of Node is the pitfall, however overall the small jump is reasonable. Similar to you had analyzed.

zijianhuang avatar Jul 05 '19 22:07 zijianhuang