Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.0.0] JavaScript heap out of memory #1247

Closed
peterpeterparker opened this issue Sep 28, 2017 · 38 comments

Comments

@peterpeterparker
Copy link

commented Sep 28, 2017

Short description of the problem:

With the update to version 3.0.0of app-scripts I'm facing the error JavaScript heap out of memory when I run the cmd ionic cordova build ios --prod

What behavior are you expecting?

No heap exception.

There are currently a couple of opened issues about this, after discussing it on the forum, we thought that it would be a good idea to report it too since I'm only facing it after the update of app-scripts. I rolled back to version 2.1.4 where I don't face that problem.

Also I could confirm that the workaround/solution described by @epetre in issue #1076, respectively giving node more memory by setting for example --max-old-space-size=8192 let me built my app with app-scripts 3.0.0 without facing the heap error

Opened issues:

#1125
#1076
#1036

Forum:

https://forum.ionicframework.com/t/3-7-0-ios-build-with-prod-not-working/107061

A similar issue on the angular-cli:

angular/angular-cli#5618

Steps to reproduce:

  1. Find a not small app
  2. Use app-scripts 3.0.0

Which @ionic/app-scripts version are you using?

cli packages:

@ionic/cli-utils  : 1.12.0
ionic (Ionic CLI) : 3.12.0

global packages:

cordova (Cordova CLI) : 7.0.1 
Gulp CLI              : CLI version 3.9.1 Local version 3.9.1

local packages:

@ionic/app-scripts : 3.0.0
Cordova Platforms  : android 6.2.3 ios 4.5.0
Ionic Framework    : ionic-angular 3.7.0

System:

Android SDK Tools : 26.1.1
ios-deploy        : 1.9.2 
ios-sim           : 5.0.13 
Node              : v8.5.0
npm               : 5.3.0 
OS                : macOS Sierra
Xcode             : Xcode 9.0 Build version 9A235 

Misc:

backend : legacy
@danbucholtz

This comment has been minimized.

Copy link
Contributor

commented Sep 29, 2017

Node just has really low memory defaults :(

Try this once:

node --max-old-space-size=4096 $(which ionic) cordova build --prod

This is sort of an ugly command. I am not sure what we can do to simplify it, though.

I'll add a help wanted label so someone much smarter than I can chime in and offer assistance.

Thanks,
Dan

@peterpeterparker

This comment has been minimized.

Copy link
Author

commented Sep 29, 2017

thx @danbucholtz your cmd do the tricks respectively

node --max-old-space-size=4096 /usr/local/bin/ionic cordova build ios --prod

It looks like max-old-space-size is definitely the solution.

This was pointed out by @epetre in issue #1076 but even if you think your cmd is "ugly", the good point is that it's only one cmd and that it don't need modifications in package.json 👍

Should I close the issue or keep it open till someday there is a solution (from node side or webpack or whatever side)?

@danbucholtz

This comment has been minimized.

Copy link
Contributor

commented Oct 2, 2017

We are looking at ways to mitigate this on our end. I'll keep you posted.

Thanks,
Dan

@Vishal-Isharani

This comment has been minimized.

Copy link

commented Oct 3, 2017

i'm also facing this error while doing ionic serve

 @ionic/cli-utils  : 1.12.0
 ionic (Ionic CLI) : 3.12.0
 @ionic/app-scripts : 3.0.0
 Cordova Platforms  : android 6.2.3
 Ionic Framework    : ionic-angular 3.6.1
 Node              : v8.4.0
 npm               : 5.4.2
 OS                : Windows 10
@Lingomat

This comment has been minimized.

Copy link

commented Oct 6, 2017

I've had this memory problem pop up frequently. My app is fairly large. Increasing node memory seems to be fixing a symptom which is intermittent. It's often turned out to be related to the version of typescript (also maximum call stack size). Right now, my app builds on ts 2.3.4 with no memory boost, in dev and prod.

I wonder if there's a clue by looking at the bundle?

@danbucholtz

This comment has been minimized.

Copy link
Contributor

commented Oct 7, 2017

It really just has to do with how many files are loaded into memory, and then probably something with ngo. I'm not sure, I haven't profiled it. It doesn't happen in most apps so the number of files loaded into memory must be sufficiently high. It is unfortunate that Node has such low default limits in my opinion.

Thanks,
Dan

@peterpeterparker

This comment has been minimized.

Copy link
Author

commented Oct 14, 2017

Today I updated my node@8.7.0 and npm@5.5.1 and it seems that I don't know face the problem anymore

@danbucholtz should I close this ticket, since I created it, or are you working on that?

@Vishal-Isharani

This comment has been minimized.

Copy link

commented Oct 15, 2017

@peterpeterparker i also updated my node@8.7.0 and npm@5.5.1 but i'm still facing this error
i'm not able to run ionic serve

@peterpeterparker

This comment has been minimized.

Copy link
Author

commented Oct 16, 2017

@Vishal-Isharani thx for checking it, then maybe I'm just lucky, weird

@sharonswli

This comment has been minimized.

Copy link

commented Oct 25, 2017

I had the same issue. Tried allocating 8gbs to node with node --max-old-space-size=8192 but the --prod building is still failing. We have a large app with 500+ components, but we never had trouble building prod/AOT with app-scripts 1.3.7(webpack 2), so I'm inclined to believe it's a memory leak coming from the new version of webpack from the latest app-scripts.

My ionic info:

 @ionic/cli-utils  : 1.10.2
   ionic (Ionic CLI) : 3.10.3

global packages:

   Cordova CLI : 7.0.1

local packages:

   @ionic/app-scripts : 3.0.1
   Cordova Platforms  : android 6.2.3 ios 4.4.0
   Ionic Framework    : ionic-angular 3.5.0

System:

   Android SDK Tools : 26.0.2
   ios-deploy        : 1.9.1
   ios-sim           : 6.0.0
   Node              : v6.11.4
   npm               : 3.10.10

I really hope this issue will be seriously looked at by the @Ionic team so larger apps can remain supported by future releases and updates.

@bkarv

This comment has been minimized.

Copy link

commented Nov 11, 2017

Similar to sharonswli we have reasonably large app with 50+ components. We can run ionic serve but unable to run the production build, getting out of memory issue. It seems like there a few open tickets related to this issue but no definite resolution. It is a bit worrisome that Ionic is unable to handle larger apps.....

@peterpeterparker

This comment has been minimized.

Copy link
Author

commented Nov 11, 2017

@sharonswli I see that you are using node v6, have you try to build your app with a more actual version of node like v8 in combination with the --max-old-space-size=8192 flag?

p.s.: more than >500 components? that's definitely an app I would love to try :)

@bkarv just checked my app, I have 56 components and 32 pages and I'm able to build it with the --prod flag. what are your ionic info, are you up-to-date with everything?

I opened this issue because I was facing the problem, temporary my problem was solved using --max-old-space-size=4096 but like I staten above, at some point the problem disappeared, not sure if it's linked with the node/npm update I run. What's sure it's that I'm update-to-date with all libs.

@bkarv

This comment has been minimized.

Copy link

commented Nov 11, 2017

@peterpeterparker thanks after weeks of trying I literally just finally got my app doing a production build.

First I increased my memory thanks to ValterSantosMatos post in #1036. This then showed an error due to rxjs "Cannot read property 'type' of undefined" I assume this was the issue causing the memory leak in the first place. I fixed this by upgrading to typescript 2.6.1.

So this may not work for everyone one else but these are the steps I did to get it working:

  1. Upgraded to Ionic to 3.9.2 and latest Ionic Cli and scripts (see Ionic Info below)
  2. Ran 'ionic doctor check' and did the suggested upgrades
  3. Increased my memory for the build by adding this to the package.json to scripts section:
"ionic:build": "node --max-old-space-size=8192 ./node_modules/@ionic/app-scripts/bin/ionic-app-scripts.js build"
  1. Updated typescript to 2.6.1 by doing
    npm install typescript@latest

  2. Ran npm run ionic:build --prod

Here is my ionic info it helps:

cli packages: (/usr/local/lib/node_modules)

    @ionic/cli-utils  : 1.18.0
    ionic (Ionic CLI) : 3.18.0

global packages:

    cordova (Cordova CLI) : 7.1.0 

local packages:

    @ionic/app-scripts : 3.1.0
    Cordova Platforms  : android 6.1.2 ios 4.4.0
    Ionic Framework    : ionic-angular 3.9.2

System:

    ios-deploy : 1.9.1 
    Node       : v8.6.0
    npm        : 5.5.1 
    OS         : macOS Sierra
    Xcode      : Xcode 9.1 Build version 9B55 

Environment Variables:

    ANDROID_HOME : not set

Misc:

    backend : pro
@peterpeterparker

This comment has been minimized.

Copy link
Author

commented Nov 11, 2017

@bkarv congrats and pretty cool to describe the details!

@sharonswli

This comment has been minimized.

Copy link

commented Nov 11, 2017

@bkarv good to hear! How long do your prod builds take now after such changes? I also followed the memory increase solution in #1036 and eventually got a successful build. There are no errors however, and build time still exceeds 30+ minutes.

@bkarv

This comment has been minimized.

Copy link

commented Nov 11, 2017

@sharonswli 30+ minues seems a long time but then again there are so many variables at play. It takes me about 7mins. To compare size my final www file is about 7.5mb. My comp is 3.3Ghz Intel Core i5 w/ 16Gb kaby lake.

@Sprauch

This comment has been minimized.

Copy link

commented Dec 2, 2017

@danbucholtz This may have been fixed in Webpack 3.9.0, where they noticed that the ConcatenatedModules were traversing an incremental list of super long strings for each module concatenated. This is likely why only projects with plenty of modules are suffering.
(specific fix: webpack/webpack#5997)

Would be nice if ionic-app-scripts dependencies could be updated to support that version.

Worth mentioning for context is that my build times increased from about 15 minutes to 5 hours after upgrading the webpack from 2.4.1 to 3.8.1, and I had to do the workaround with heap space allocation to even get a build through.

@nvirth

This comment has been minimized.

Copy link

commented Mar 2, 2018

node --max-old-space-size=4096 /usr/local/bin/ionic cordova build ios --prod

How can I run this on windows?
I've tried to put the win full path (which is C:/Users/{UserName}/AppData/Roaming/npm/ionic[.cmd] by me) into the above one, in place of the linux full path, but it did not work,

I also tried the package.json solutions mentioned above, like:

"ionic:build": "node --max-old-space-size=8192 ./node_modules/@ionic/app-scripts/bin/ionic-app-scripts.js build"

This also works on windows, but the command I need to run with 8GB memory is:

ionic cordova run android

And there seems to be no "run" command in ionic-app-scripts

@Sprauch

This comment has been minimized.

Copy link

commented Mar 2, 2018

@nvirth the easiest way to run this on windows would be to add the line:

"scripts": {
    "ionic:build": "node --max-old-space-size=8192 --stack-size=1968 node_modules/@ionic/app-scripts/bin/ionic-app-scripts.js build"
  },

into the package.json which you've already mentioned, and then run the command
npm run ionic:build --prod inside your project folder, from a terminal prompt

At least that did the trick for me.

@nvirth

This comment has been minimized.

Copy link

commented Mar 3, 2018

@Sprauch Yes this works, but how to do this with ionic cordova run android?

@andrefa

This comment has been minimized.

Copy link

commented Mar 6, 2018

How can I run it on my CI env? I'm pretty sure I do not have power over memory allocation there. I'm using NeverCode for mobile builds currently.

@ericmulcahy

This comment has been minimized.

Copy link

commented Jun 17, 2018

Any updates on this? The workaround does not work for me. The build takes > 4 hours on a Mac with --max-old-space-size=12000 then fails out of memory. On a Windows machine it is the same thing with --max-old-space-size=20000.

Is it possible to update to Webpack 3.9.0 (which presumably fixes this)? My Ionic info is in the closed thread just above.

@ChargerIIC

This comment has been minimized.

Copy link

commented Jun 18, 2018

As a test I installed webpack 3.11.0 manually to see if the aforementioned webpack patch was a solution. Unfortunately there is no change. I've been able to allocate as much as 8 gigs of RAM to the process, but it still never finishes.

I only have 6 pages and 2 components. I'm wondering if there is some kind of common trait that is causing the slowdown. I have much larger Ionic projects that compile fine and the issue seems to only occur when compiling for web.

Update: Had one finish after 6 hours with an out of memory error.

My repo is here is anyone wants to reproduce the problem:
https://github.com/ChargerIIC/HouseOfBurtFront

@ericmulcahy

This comment has been minimized.

Copy link

commented Jun 18, 2018

@ChargerIIC I checked out the source code you linked in the other thread, and I see you're using Firebase with AngularFire. I am also using that in my project. Maybe that is the common link?

Although the prod build was working fine for me for 2 months before it started failing, and I was using AngularFire the whole time, so who knows.

@bkarv

This comment has been minimized.

Copy link

commented Jun 19, 2018

@ChargerIIC @ericmulcahy Would it be worthwhile running the prod build without Firebase and AngularFire imported and see if it compiles to pinpoint the source of the error?

@Lingomat

This comment has been minimized.

Copy link

commented Jun 19, 2018

Having had this problem myself, the one thing I found that reliably caused the problem was using older versions of Typescript.

@ChargerIIC

This comment has been minimized.

Copy link

commented Jun 19, 2018

@bkarv Unfortunately, that didn't resolve the issue. Webpack took longer to finish (probably because I blew a hole in the old dependency graph), but still didn't transition to the node-sass step.

I'm about 20 minutes in, but it looks like the typescript upgrade to 2.9.2 didn't resolve the issue. I've also commented out all empty SCSS objects (like the ones you get when you generate a new component/page.

Going to let this chunk away and then re-attempt a node-sass upgrade if it fails again. Really wish I could know what the sass engine was doing while it's chunking away like this. I don't have that much SCSS.

edit: Update: Upgraded node-sass and hoek, but problem persists

@Lingomat

This comment has been minimized.

Copy link

commented Jun 20, 2018

@ChargerIIC I modified your repo to remove the abomination of AngularFire and sure enough, it compiles --prod no problem (42 seconds). To be fair, I had to comment out a fair bit but I really doubt that was your problem. AngularFire has deps of a different angular and rxjs than the one you're using.
It doesn't even do anything you can't implement in short functions directly from the firebase js API in my opinion...

@ericmulcahy

This comment has been minimized.

Copy link

commented Jun 20, 2018

Thanks @Lingomat, I will try removing AngularFire, although this will be more of an undertaking for my project.

@pavel-zdenek

This comment has been minimized.

Copy link

commented Aug 1, 2018

This is happening to me on a particular project when ionic@4.0.0 is installed. Downgrading to 3.20.0 makes the OOM go away. Looks like a regression with major version(s).

@CodyDubu

This comment has been minimized.

Copy link

commented Aug 8, 2018

I was able to get my project working by adding keystore passwords to the cli, and downgrading to @firebase/database@0.2.1.

Like this: ionic cordova build android --prod --release -- -- --keystore="..\android.keystore" --storePassword=password --alias=alias --password=password

Refrence
Android Guide

@liangwenzhong

This comment has been minimized.

Copy link

commented Aug 16, 2018

any solution to this?

@lordgreg

This comment has been minimized.

Copy link

commented Sep 23, 2018

Well... just started happening to me too. Windows. Still no solution.

@basvdijk

This comment has been minimized.

Copy link

commented Oct 5, 2018

I've found a solution when using Ionic which was pointed out in an old message on the forum: https://forum.ionicframework.com/t/3-7-0-ios-build-with-prod-not-working/107061/24

Open node_modules/@ionic/app-scripts/bin/ionic-app-scripts.js

Change the first line from:
#!/usr/bin/env node
into
#!/usr/bin/env node --max-old-space-size=4096

I tried values 1024 and 2048, but for my relatively large app I needed 4096.

Now I am able to run ionic cordova build android --prod --release with out the FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory error.

Posted this solution in #1467, #1036 and #1247

@peterpeterparker

This comment has been minimized.

Copy link
Author

commented Oct 5, 2018

Just noticed that I'm the OP of this issue therefore I gonna allow my self to close it. Firstly I didn't faced the problem anymore since the 14 Oct 2017 (see my above msgs), secondly I have migrated my app to Ionic v4 where ionic-app-scripts isn't use anymore

@zakton5

This comment has been minimized.

Copy link

commented Oct 7, 2018

For any future readers, my issue is that I had the firebase (5.5.3) package installed and did not properly import the specific modules. The fix is shown here: #848. I guess importing everything was much too large.

@kirillgroshkov

This comment has been minimized.

Copy link

commented Nov 2, 2018

Found a better solution, to specify NODE env variable before the build: export NODE_OPTIONS=--max-old-space-size=4096

Credits to: https://stackoverflow.com/a/48895989/4919972

@adil-Pathan

This comment has been minimized.

Copy link

commented Nov 28, 2018

I found a better solution just Now. Set environment variable NODE_OPTIONS with value as --max_old_space_size=4096 Also make sure that environment variable _JAVA_OPTIONS with value as -Xmx1024M is also set. My machine has Windows 7 ( 32 bit ) and 4GB RAM.

Happy coading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
You can’t perform that action at this time.