DSpace / dspace-angular

DSpace User Interface built on Angular.io
https://wiki.lyrasis.org/display/DSDOC8x/
BSD 3-Clause "New" or "Revised" License
131 stars 430 forks source link

"JavaScript heap out of memory" error during development build/run (yarn start:dev) #2259

Open alanorth opened 1 year ago

alanorth commented 1 year ago

Not an issue with DSpace Angular frontend per se, but I'm running into this often and haven't seen any discussion here so I will file this as a reference.

$ yarn run start:dev
yarn run v1.22.19
$ nodemon --exec "cross-env NODE_ENV=development yarn run serve"
[nodemon] 2.0.22
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): config/**/*
[nodemon] watching extensions: json
[nodemon] starting `cross-env NODE_ENV=development yarn run serve`
$ yarn base-href
$ ts-node --project ./tsconfig.ts-node.json scripts/base-href.ts
Building development app config
Overriding app config with /home/aorth/src/git/dspace-angular/config/config.yml
Overriding app config with /home/aorth/src/git/dspace-angular/config/config.dev.yml
Setting baseHref to / in angular.json
$ ts-node --project ./tsconfig.ts-node.json scripts/serve.ts
Building development app config
Overriding app config with /home/aorth/src/git/dspace-angular/config/config.yml
Overriding app config with /home/aorth/src/git/dspace-angular/config/config.dev.yml
⠋ Generating browser application bundles (phase: setup)...Building development app config
Overriding app config with /home/aorth/src/git/dspace-angular/config/config.yml
Overriding app config with /home/aorth/src/git/dspace-angular/config/config.dev.yml
Angular config.json file generated correctly at /home/aorth/src/git/dspace-angular/src/assets/config.json 

⠋ Generating browser application bundles (phase: sealing)...
<--- Last few GCs --->

[217287:0x5b6cb60]    91532 ms: Scavenge 2017.2 (2076.5) -> 2012.9 (2078.5) MB, 12.8 / 0.0 ms  (average mu = 0.628, current mu = 0.326) allocation failure; 
[217287:0x5b6cb60]    91546 ms: Scavenge 2019.0 (2078.7) -> 2015.0 (2080.4) MB, 8.2 / 0.0 ms  (average mu = 0.628, current mu = 0.326) allocation failure; 
[217287:0x5b6cb60]    92007 ms: Scavenge 2020.6 (2080.4) -> 2016.5 (2097.4) MB, 456.3 / 0.0 ms  (average mu = 0.628, current mu = 0.326) allocation failure; 

<--- JS stacktrace --->

FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory
 1: 0xb7b3e0 node::Abort() [ng serve --host localhost --port 4000 --serve-path / --ssl false --configuration development]
 2: 0xa8c8aa  [ng serve --host localhost --port 4000 --serve-path / --ssl false --configuration development]
 3: 0xd69100 v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [ng serve --host localhost --port 4000 --serve-path / --ssl false --configuration development]
 4: 0xd694a7 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [ng serve --host localhost --port 4000 --serve-path / --ssl false --configuration development]
 5: 0xf46ba5  [ng serve --host localhost --port 4000 --serve-path / --ssl false --configuration development]
 6: 0xf5908d v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [ng serve --host localhost --port 4000 --serve-path / --ssl false --configuration development]
 7: 0xf3378e v8::internal::HeapAllocator::AllocateRawWithLightRetrySlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [ng serve --host localhost --port 4000 --serve-path / --ssl false --configuration development]
 8: 0xf34b57 v8::internal::HeapAllocator::AllocateRawWithRetryOrFailSlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [ng serve --host localhost --port 4000 --serve-path / --ssl false --configuration development]
 9: 0xf15d2a v8::internal::Factory::NewFillerObject(int, v8::internal::AllocationAlignment, v8::internal::AllocationType, v8::internal::AllocationOrigin) [ng serve --host localhost --port 4000 --serve-path / --ssl false --configuration development]
10: 0x12dacdf v8::internal::Runtime_AllocateInYoungGeneration(int, unsigned long*, v8::internal::Isolate*) [ng serve --host localhost --port 4000 --serve-path / --ssl false --configuration development]
11: 0x1707b79  [ng serve --host localhost --port 4000 --serve-path / --ssl false --configuration development]
[nodemon] clean exit - waiting for changes before restart

This is on the main branch around DSpace 7.5 and DSpace 7.6 (Angular 13 and 15). Node version is v18.15.0. System has 16GB of memory and there are several gigs free.

alanorth commented 1 year ago

I found a few references to this that say increasing the "old" space helps:

I just tried this and it worked:

$ export NODE_OPTIONS=--max_old_space_size=8192
$ yarn run start:dev

So there may be something we can do in package.json if this becomes an issue more people are having. Also, note that I picked 8192 arbitrarily rather than based on profiling how much memory the build actually used. The real number should be related to how many assets we have, or how large our bundle is.

pnbecker commented 1 year ago

We already set NODE_OPTIONS in the Dockerfile. It is set to 4096 there. Can we do anything to reduce the amount of memory that is used to compile a dev version of dspace-angular or do we have to raise the memory again and again?

alanorth commented 1 year ago

Hi @pnbecker I am not using Docker. :) I am building using the normal yarn CLI workflow.

saschaszott commented 1 year ago

As discussed in the angular-ui Slack channel: there is a hard-coded value (4GB) of max_old_space_size in webpack.js 👉 https://github.com/DSpace/dspace-angular/blob/9c780200b2136621517cff034234c5c29c6d7762/scripts/webpack.js#L4

alanorth commented 1 year ago

But this hard-coded value in webpack.js was for something else and is unused as I understood it?

saschaszott commented 1 year ago

But this hard-coded value in webpack.js was for something else and is unused as I understood it?

Yes, as @artlowel mentioned in the angular-ui Slack channel, webpack.js is no longer needed. I will open a Github issue and ask for deletion. 👉 https://github.com/DSpace/dspace-angular/issues/2285

tdonohue commented 1 year ago

I'm not sure exactly how to move this forward, as I cannot reproduce this locally. For now, I'll keep the ticket open but move to "Needs Info" column. I think we need some reproducible steps where we can reliably trigger this out of memory error... or more examples from others who seen the same thing.

alanorth commented 1 year ago

@tdonohue no problem, I understand. We can actually close it. At least it is on the record now for anyone searching.

lmaylein commented 1 year ago

Same problem here (only in development mode "yarn start:dev" not in for build:prod"). export NODE_OPTIONS=--max-old-space-size=4096 fixes the problem. Can this be fixed permanently so that not all run into the same problem?

lmaylein commented 1 year ago

only occurs with 7.6.

MW3000 commented 1 year ago

Me and my colleague ran into the same problem with our Ubuntu development machines and DSpace 7.6. Perhaps we can just make sure there is enough memory in package.json for start:dev

Like this?

"start:dev": "nodemon --exec \"cross-env NODE_OPTIONS=--max-old-space-size=4096 NODE_ENV=development yarn run serve\"",

And of course it would be interesting to know, why there is an increase in memory usage for start:dev in 7.6.

tdonohue commented 1 year ago

Reopening, as this still seems to be occurring for some folks.

Via Slack, @MW3000 found an important clue in https://stackoverflow.com/a/64120041 . It appears the default value for V8/Node memory may differ on various systems/OSes.

My best guess is that when this memory defaults to a lower value, occasionally not enough memory may be provided to allow the DSpace UI to run in development mode. It's unclear why this started with 7.6. 7.6 did include an upgrade from Angular 13->15. It could have been a side effect of that, or a side effect of one of the other major features added to 7.6.

Regardless, for anyone encountering this issue, the known fix is to increase max-old-space-size to 4GB or greater via NODE_OPTIONS:

export NODE_OPTIONS=--max-old-space-size=4096

Notes on this have been added to our "Common Installation Issues": https://wiki.lyrasis.org/display/DSDOC7x/Installing+DSpace#InstallingDSpace-Javascriptheapoutofmemory

emilialobo commented 5 months ago

NODE_OPTIONS=--max-old-space-size=4096 yarn start:prod yarn run v1.22.19 $ yarn run build:prod && cross-env NODE_ENV=production yarn run serve:ssr $ cross-env NODE_ENV=production yarn run build:ssr $ ng build --configuration production && ng run dspace-angular:server:production ⠦ Generating browser application bundles (phase: building)...Killed error Command failed with exit code 137. info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command. error Command failed with exit code 137. info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command. error Command failed with exit code 137. info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.


This error occurs in production