node.js 致命错误:CALL_AND_RETRY_LAST 分配失败 - 进程内存不足
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26094420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - process out of memory
提问by user619271
Node version is v0.11.13
节点版本是 v0.11.13
Memory usage during crash according to sudo topnot raises over 3%
崩溃期间的内存使用量根据sudo top不提高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
要检查是否存在递归堆栈大小问题,我使用 --stack-size=60000 参数运行了下一个代码
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.
然后我运行了给我致命错误的代码:CALL_AND_RETRY_LAST 分配失败 - 使用相同的 --stack-size=60000 参数处理内存不足并且没有得到Segmentation fault.
So I conclude CALL_AND_RETRY_LASThas nothing common with the recursion stack size.
所以我得出结论CALL_AND_RETRY_LAST,递归堆栈大小没有什么共同之处。
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_LASTthat's why I created separate question.
stackoverflow 上也有类似的问题,但这些问题都不是关于CALL_AND_RETRY_LAST这就是我创建单独问题的原因。
采纳答案by CFrei
If you have a look at the source: github/v8, it seems that you try to reserve a very big object.According to my experience it happens if you try to parse a huge JSON object, but when I try to parse your output with JSON and node0.11.13, it just works fine.
如果您查看源代码:github/v8,似乎您尝试保留一个非常大的对象。根据我的经验,如果您尝试解析一个巨大的 JSON 对象,就会发生这种情况,但是当我尝试使用以下命令解析您的输出时JSON 和 node0.11.13,它工作正常。
You don't need more --stack-size, you need more memory: --max_new_space_sizeand/or --max_old_space_size.
你不需要更多--stack-size,你需要更多的内存:--max_new_space_size和/或--max_old_space_size。
The only hint I can give you beside that is trying another JSON-parser and/or try to change the input format to JSON line instead of JSON only.
我可以给你的唯一提示是尝试另一个 JSON 解析器和/或尝试将输入格式更改为 JSON 行而不是仅 JSON。
回答by sol404
$ 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_modules/.bin/* 文件内的所有节点调用中附加 --max-old-space-size=4096 。
Node.js version >= 8 - DEPRECATION NOTICE
Node.js 版本 >= 8 - 弃用通知
Since NodeJs V8.0.0, it is possible to use the option --max-old-space-size. NODE_OPTIONS=options...
从 NodeJs V8.0.0 开始,可以使用选项--max-old-space-size. NODE_OPTIONS=选项...
$ export NODE_OPTIONS=--max_old_space_size=4096
回答by Jijo Paulose
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.
要解决此问题,您需要通过使用选项增加内存限制来运行您的应用程序--max_old_space_size。默认情况下,Node.js 的内存限制为 512 MB。
node --max_old_space_size=2000 server.js
回答by jfunk
I found that max_new_space_sizeis not an option in node 4.1.1 and max_old_space_sizealone did not solve my problem. I am adding the following to my shebang and the combination of these seems to work:
我发现这max_new_space_size不是节点 4.1.1 中的一个选项,并且max_old_space_size单独没有解决我的问题。我将以下内容添加到我的 shebang 中,这些组合似乎有效:
#!/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.
[编辑]:4096 === 4GB 内存,如果您的设备内存不足,您可能需要选择较小的数量。
[UPDATE]: Also discovered this error while running grunt which previously was run like so:
[更新]:在运行 grunt 时也发现了这个错误,以前是这样运行的:
./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
回答by Ben Creasy
Note: see the warning in the comments about how this can affect Electron applications.
注意:请参阅评论中关于这如何影响 Electron 应用程序的警告。
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.
从 2017 年 8 月发布的 v8.0 开始,NODE_OPTIONS 环境变量公开了此配置(请参阅NODE_OPTIONS 已登陆 8.x!)。根据文章,仅允许在源中列入白名单的选项(注意:不是最新链接!),其中包括"--max_old_space_size". 请注意,这篇文章的标题似乎有点误导 - 似乎 NODE_OPTIONS 已经存在,但我不确定它是否公开了此选项。
So I put in my .bashrc:export NODE_OPTIONS=--max_old_space_size=4096
所以我输入了我的.bashrc:export NODE_OPTIONS=--max_old_space_size=4096
回答by Ali David
The increase-memory-limitmodule has been deprecated now.
As of Node.js v8.0shipped August 2017, we can now use the NODE_OPTIONSenv variable to set the max_old_space_sizeglobally.
该increase-memory-limit模块现已弃用。从2017 年 8 月发布的Node.js v8.0 开始,我们现在可以使用NODE_OPTIONSenv 变量来设置max_old_space_size全局变量。
export NODE_OPTIONS=--max_old_space_size=4096
回答by Mugshep
Just a variation on the answers above.
只是上述答案的一个变体。
I tried the straight up node command above without success, but the suggestion from this Angular CLI issueworked for me - you create a Node script in your package.jsonfile to increase the memory available to Node when you run your production build.
我尝试了上面的直接节点命令但没有成功,但是这个 Angular CLI 问题的建议对我有用- 你在你的package.json文件中创建一个 Node 脚本来增加运行生产构建时 Node 可用的内存。
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:
因此,如果您想将 Node 可用的内存增加到 4gb ( max-old-space-size=4096),您的 Node 命令将是node --max-old-space-size=4096 ./node_modules/@angular/cli/bin/ng build --prod. (也可以根据您的需要增加或减少内存量 - 4GB 对我有用,但您可能需要更多或更少)。然后你将它添加到你的 package.json 'scripts' 部分,如下所示:
"prod": "node --max-old-space-size=4096 ./node_modules/@angular/cli/bin/ng build --prod"
"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 prodif you're on a Mac or Linux).
您可以通过调用来运行它npm run prod(sudo npm run prod如果您使用的是 Mac 或 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.
请注意,可能存在导致 Node 需要更多内存的潜在问题——如果是这种情况,这并不能解决这个问题——但它至少为 Node 提供了执行构建所需的内存。
回答by ChokYeeFan
My working solution is:
我的工作解决方案是:
- Install cross-env
npm install --save-dev cross-envornpm install -g cross-env. - File
package.jsonadd 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:iosProblem solved.
- 安装cross-env
npm install --save-dev cross-env或npm install -g cross-env. - 文件
package.json添加新的构建脚本,
例如... "build:prod:ios": "cross-env NODE_OPTIONS='--max-old-space-size=8192' ionic cordova build ios --prod --release" ... 下次使用该命令进行构建。
npm run build:prod:ios问题解决了。
回答by Saad Mahmood
I was facing this issue in ionic and tried many solutions but solved this by running this.
我在 ionic 中遇到了这个问题并尝试了很多解决方案,但通过运行它解决了这个问题。
For MAC:node --max-old-space-size=4096 /usr/local/bin/ionic cordova build android --prod
对于 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
对于 Windows:node --max-old-space-size=4096 /Users/{your user}/AppData/Roaming/npm/node_modules/ionic/bin/ionic cordova build windows --prod
回答by Khandelwal-manik
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 :
当为执行分配的所需内存小于运行进程所需的内存时,会发生此错误。默认情况下,节点内存大小为 512 mb 要增加此大小,您需要键入以下命令:
node --max-old-space-size= <NewSize> <fileName>

