8
votes

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory

Error when running command: ionic cordova build android --prod

Note: that ionic build android, ionic run android and ionic serve all works fine.

I've read many topics that says once the project gets bigger, it stops working.

It was working the day before issue arrive. It stopped working as we added more files and JSON for translation of app in multi-language.

Is it a known issue? Is there any solution?

PLEASE CHECK FOR MORE DETAIL : https://youtu.be/oCN7iSt8rzg

Attached Error Image:

enter image description here

Ionic Info:

enter image description here

4
Even after --max_old_space_size it dont work, then it could be possible issue with the code. In security context section, it says error related to toString() So you may have to check the code where you are trying to do toString() of large object. This post should probably help - github.com/nodejs/node/issues/10137Gandhi
Try to increase memory limit to --max-old-space-size=8192 .Manish Karena
@Manish - (Thanks!) I tried, not working.Ankit Maheshwari
@Gandhi - (Thanks!) I didn't added any toString() but yes we have added large files containing JSON object (for translation of app in multi-language) is that causing problem? If yes - What would be the solution?Ankit Maheshwari
@AnkitMaheshwari you may try increasing your --max-old-space-size limit to further higher limit and give it a try. You can also try to out library like memwatch - github.com/marcominetti/node-memwatch to figure out the memory leaks.Gandhi

4 Answers

8
votes

Resolved by modifying my ionic.cmd file in C:\Users\AppData\Roaming\npm by adding --max_old_space_size. I set mine to 8096.

@if EXIST "%~dp0\node.exe" (
"%~dp0\node.exe" --max_old_space_size=8096 "%~dp0\node_modules\ionic\bin\ionic" %*
) ELSE (
@SETLOCAL
@set PATHEXT=%PATHEXT:;.JS;=;%
node --max_old_space_size=8096 "%~dp0\node_modules\ionic\bin\ionic" %*
)

Good luck! Check Actual Source: https://github.com/ionic-team/ionic-cli/issues/1453

2
votes

My ionic (Ionic CLI) : 3.20.0 and @ionic/app-scripts : 3.1.8 but was still getting the error.

I fixed the error by updating the build script in package.json

  "scripts": {
    "clean": "ionic-app-scripts clean",
    "build": "node --max-old-space-size=2048 ./node_modules/.bin/ionic-app-scripts build",
    "lint": "ionic-app-scripts lint",
    "ionic:build": "ionic-app-scripts build",
    "ionic:serve": "ionic-app-scripts serve"
  },

or

Try the below command:

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

1
votes

I also encountered this error and I told myself that increasing the allocated memory was a workaround as something must be going wrong under the hood.

If you are using Firebase, it turns out that it is the culprit.

I changed it from "^4.12.1" to "4.6.1" (without the caret symbol) and ran npm install to downgrade it to the said anterior version.

After that, I was able to run ionic cordova build android --prod without encountering the dreaded "heap out of memory" error.

Apparently, 4.12.1 (and perhaps others) has a memory leak, hence the encountered issue.

Hope this helps!

0
votes

Even though the issue is resolved by the OP, it's a temporary workaround. The actual issue is something to do with the string operations that is involved in the project.

As the OP has already mentioned that the project involves JSON strings of huge size, the ideal fix will be track down the memory consumption during build using tools like node-memwatch and fix the same. Setting heap size of approximately 8GB may not be possible all the time and this has to increase with the further enhancements in the project too.

Also as per this link, the latest webpack release will hopefully ease out some of these problems.

UPDATE: you can also try using latest Ionic CLI version 3.5.0 which may have some improvements