8

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

Ankit Maheshwari
  • 1,620
  • 6
  • 28
  • 55
  • 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 - https://github.com/nodejs/node/issues/10137 – Gandhi Jul 07 '17 at 14:58
  • Try to increase memory limit to --max-old-space-size=8192 . – Manish Karena Jul 08 '17 at 10:37
  • @Manish - (Thanks!) I tried, not working. – Ankit Maheshwari Jul 08 '17 at 13:36
  • @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 Jul 08 '17 at 13:38
  • @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 - https://github.com/marcominetti/node-memwatch to figure out the memory leaks. – Gandhi Jul 08 '17 at 16:49
  • @Gandhi thanks! I'll try and revert you. – Ankit Maheshwari Jul 09 '17 at 04:02
  • @Gandhi when I installed, It gives me this error - https://i.stack.imgur.com/rudux.png – Ankit Maheshwari Jul 09 '17 at 05:38
  • @AnkitMaheshwari i dont see any error in the screenshot. All were just warnings – Gandhi Jul 09 '17 at 18:19
  • @AnkitMaheshwari check out this link - https://stackoverflow.com/questions/5733665/how-to-prevent-memory-leaks-in-node-js you may have to handle your code for proper garbage collection – Gandhi Jul 10 '17 at 05:54
  • Ok @Gandhi thanks! I'm looking at this.. – Ankit Maheshwari Jul 10 '17 at 08:39
  • @Ankit Maheshwari - look at accepted answer [here](https://stackoverflow.com/questions/26094420/fatal-error-call-and-retry-last-allocation-failed-process-out-of-memory). It might be helpful for you. – Manish Karena Jul 11 '17 at 07:18
  • @ManishKarena the accepted answer is tried and it seems its not working. Check earlier comments please – Gandhi Jul 11 '17 at 12:59
  • Thanks a lot @Gandhi and ManishKarena - Unfortunately these answers were not working for me. BTW, I just found a working solution for me - added answer below. Thanks Again! – Ankit Maheshwari Jul 11 '17 at 13:20
  • @AnkitMaheshwari You can try out it again in latest version of Ionic CLI (3.5.0) Please update – Gandhi Jul 14 '17 at 12:23

4 Answers4

8

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

Ankit Maheshwari
  • 1,620
  • 6
  • 28
  • 55
2

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

Biranchi
  • 16,120
  • 23
  • 124
  • 161
  • 1
    i too getting the same issue `My ionic (Ionic CLI) : 3.20.0 and @ionic/app-scripts : 3.1.9` and then i tried with your answer but still the error is coming, i have installed `angularfire2(5.0.0.-rc.6)` i think this is causing the memory leak in my app, i removed `angularfire2` then i tried it worked, but now i want to use `angularfire2` and also i want to build my app in `--prod` mode, please help me in this @Biranchi – Midhunsai May 04 '18 at 05:10
  • 1
    @Midhunsai Try the below command, $sudo node --max-old-space-size=8192 /usr/local/bin/ionic cordova build android --prod – Biranchi May 04 '18 at 16:36
  • @Midhunsai: I have the same problems, do you find any solution/ workaround? – Georg Kastenhofer May 28 '18 at 16:31
  • @Midhunsai, Try installing the node version v6.14.0 and check if it works. – Biranchi May 29 '18 at 04:32
1

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!

Gregordy
  • 597
  • 7
  • 18
0

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

Gandhi
  • 11,875
  • 4
  • 39
  • 63