[node.js] FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - process out of memory

Node version is v0.11.13

Memory usage during crash according to sudo top not raises over 3%

Code that reproduces this error:

var request = require('request')
var nodedump = require('nodedump')

request.get("http://pubapi.cryptsy.com/api.php?method=marketdatav2",function(err,res)
{
    var data
    console.log( "Data received." );
    data = JSON.parse(res.body)
    console.log( "Data parsed."   );
    data = nodedump.dump(data)
    console.log( "Data dumped."   ); 
    console.log( data )
})

To check if that a recursion stack size problem I have ran next code with --stack-size=60000 parameter

var depth = 0;

(function recurse() {
    // log at every 500 calls
    (++depth % 500) || console.log(depth);
    recurse();
})();

and have got

264500 
Segmentation fault

Then I ran code which gives me FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - process out of memory with the same --stack-size=60000 parameter and haven't got Segmentation fault.

So I conclude CALL_AND_RETRY_LAST has nothing common with the recursion stack size.

How could I solve this problem? I believe there is enough free memory on my computer to finish this task successfully.

There are similar questions on stackoverflow but none of this questions are about CALL_AND_RETRY_LAST that's why I created separate question.

This question is related to node.js

The answer is


I was facing this issue in ionic and tried many solutions but solved this by running this.

For MAC: node --max-old-space-size=4096 /usr/local/bin/ionic cordova build android --prod

For Windows: node --max-old-space-size=4096 /Users/{your user}/AppData/Roaming/npm/node_modules/ionic/bin/ionic cordova build windows --prod


In a Windows Machine run below command

set NODE_OPTIONS=--max_old_space_size=4096


#!/usr/bin/env node --max-old-space-size=4096 in the ionic-app-scripts.js dint work

But after Modifying: the following file it worked

node_modules/.bin/ionic-app-scripts.cmd

By adding:

@IF EXIST "%~dp0\node.exe" ( "%~dp0\node.exe" "%~dp0..@ionic\app-scripts\bin\ionic-app-scripts.js" %* ) ELSE ( @SETLOCAL @SET PATHEXT=%PATHEXT:;.JS;=;% node --max_old_space_size=4096 "%~dp0..@ionic\app-scripts\bin\ionic-app-scripts.js" %* )


$ sudo npm i -g increase-memory-limit

Run from the root location of your project:

$ increase-memory-limit

This tool will append --max-old-space-size=4096 in all node calls inside your node_modules/.bin/* files.


Node.js version >= 8 - DEPRECATION NOTICE

Since NodeJs V8.0.0, it is possible to use the option --max-old-space-size. NODE_OPTIONS=options...

$ export NODE_OPTIONS=--max_old_space_size=4096

The increase-memory-limit module has been deprecated now. As of Node.js v8.0 shipped August 2017, we can now use the NODE_OPTIONS env variable to set the max_old_space_size globally.

export NODE_OPTIONS=--max_old_space_size=4096

Ref URL: https://github.com/endel/increase-memory-limit


I was seeing this issue when I was creating a bundle to react-native. Things I tried and didn't work:

  1. Increasing the node --max_old_space_size, intrestingly this worked locally for me but failed on jenkins and I'm still not sure what goes wrong with jenkins
  2. Some places mentioned to downgrade the version of node to 6.9.1 and that didn't work for me either. I would just like to put this here as it might work for you.

Thing that did work for me: I was importing a really big file in the code. The way I resolved it was by including it in the ignore list in .babelrc something like this:

{
    "presets": ["react-native"],
    "plugins": ["transform-inline-environment-variables"],
    "ignore": ["*.json","filepathToIgnore.ext"]
}

It was a .js file which did not really needed transpiling and adding it to the ignore list did help.


My working solution is:

  • Install cross-env
    npm install --save-dev cross-env or npm install -g cross-env.
  • File package.json add new build script
    e.g.
    ... "build:prod:ios": "cross-env NODE_OPTIONS='--max-old-space-size=8192' ionic cordova build ios --prod --release" ...
  • Use that command to build next time.
    npm run build:prod:ios

  • Problem solved.


I found that max_new_space_size is not an option in node 4.1.1 and max_old_space_size alone did not solve my problem. I am adding the following to my shebang and the combination of these seems to work:

#!/usr/bin/env node --max_old_space_size=4096 --optimize_for_size --max_executable_size=4096 --stack_size=4096

[EDIT]: 4096 === 4GB of memory, if your device is low on memory you may want to choose a smaller amount.

[UPDATE]: Also discovered this error while running grunt which previously was run like so:

./node_modules/.bin/grunt

After updating the command to the following it stopped having memory errors:

node --max_old_space_size=2048 ./node_modules/.bin/grunt 

I lost some days with this problem.... until I found that in some file I was importing one static file, a built file. It make the build to never end. Something like:

import PropTypes from "../static/build/prop-types"; 

Fixing to the real source solved all the problem.

Sharing my solution. :)


Anyone getting this error with Azure build pipelines, try the below step to change environment variable of build agent

Add an Azure build pipeline task -> Azure powershell script:Inlinescript before Compile with below settings

- task: AzurePowerShell@3
  displayName: 'Azure PowerShell script: InlineScript'
  inputs:
    azureSubscription: 'NYCSCA Azure Dev/Test (ea91a274-55c6-461c-a11d-758ef02c2698)'
    ScriptType: InlineScript
    Inline: '[Environment]::SetEnvironmentVariable("NODE_OPTIONS", "--max_old_space_size=16384", "Machine")'
    FailOnStandardError: true
    azurePowerShellVersion: LatestVersion

npm install -g increase-memory-limit

increase-memory-limit

OR

  1. Navigate to %appdata% -> npm folder or C:\Users\{user_name}\AppData\Roaming\npm
  2. Open ng.cmd in your favorite editor
  3. Add --max_old_space_size=8192 to the IF and ELSE block

now ng.cmd file looks like this after the change:

@IF EXIST "%~dp0\node.exe" (
  "%~dp0\node.exe" "--max_old_space_size=8192" "%~dp0\node_modules\@angular\cli\bin\ng" %*
) ELSE (
  @SETLOCAL
  @SET PATHEXT=%PATHEXT:;.JS;=;%
  node "--max_old_space_size=8192" "%~dp0\node_modules\@angular\cli\bin\ng" %*
)

Just a variation on the answers above.

I tried the straight up node command above without success, but the suggestion from this Angular CLI issue worked for me - you create a Node script in your package.json file to increase the memory available to Node when you run your production build.

So if you want to increase the memory available to Node to 4gb (max-old-space-size=4096), your Node command would be node --max-old-space-size=4096 ./node_modules/@angular/cli/bin/ng build --prod. (increase or decrease the amount of memory depending on your needs as well - 4gb worked for me, but you may need more or less). You would then add it to your package.json 'scripts' section like this:

"prod": "node --max-old-space-size=4096 ./node_modules/@angular/cli/bin/ng build --prod"

It would be contained in the scripts object along with the other scripts available - e.g.:

"scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "prod": "node --max-old-space-size=4096./node_modules/@angular/cli/bin/ng build --prod"
}

And you run it by calling npm run prod (you may need to run sudo npm run prod if you're on a Mac or Linux).

Note there may be an underlying issue which is causing Node to need more memory - this doesn't address that if that's the case - but it at least gives Node the memory it needs to perform the build.


To solve this issue you need to run your application by increasing the memory limit by using the option --max_old_space_size. By default the memory limit of Node.js is 512 mb.

node --max_old_space_size=2000  server.js 

An alternative solution is to disable the AOT compiler:

ng build --prod --aot false

Note: see the warning in the comments about how this can affect Electron applications.

As of v8.0 shipped August 2017, the NODE_OPTIONS environment variable exposes this configuration (see NODE_OPTIONS has landed in 8.x!). Per the article, only options whitelisted in the source (note: not an up-to-date-link!) are permitted, which includes "--max_old_space_size". Note that this article's title seems a bit misleading - it seems NODE_OPTIONS had already existed, but I'm not sure it exposed this option.

So I put in my .bashrc:
export NODE_OPTIONS=--max_old_space_size=4096


this error occurs when required memory allocated for execution is less than memory required for running process. By default, Node memory size is 512 mb to increase this you need to type the following command :

node --max-old-space-size= <NewSize> <fileName>