node.js 如何增加nodejs默认内存?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34356012/
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
how to increase nodejs default memory?
提问by anil
On Server startup exporting 2GB(Approximately) data from mongodb to redis,then getting error as FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - process out of memory.
在服务器启动时将 2GB(大约)数据从 mongodb 导出到 redis,然后得到错误为FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - process out of memory.
Then started server with this command node --max-old-space-size=4076 server.jsand works fine. But needs to configure in nodejs applicaton so that node server always starts with 4gb memory. Please help me how to fix this?
Thanks.
然后用这个命令启动服务器node --max-old-space-size=4076 server.js并且工作正常。但是需要在 nodejs 应用程序中进行配置,以便节点服务器始终以 4gb 内存启动。请帮助我如何解决这个问题?谢谢。
采纳答案by M T Head
node SomeScript.js --max-old-space-size=8192
节点 SomeScript.js --max-old-space-size=8192
回答by James LeClair
one option: npm start scripts
一种选择:npm 启动脚本
https://docs.npmjs.com/misc/scripts
https://docs.npmjs.com/misc/scripts
These are added to your package.json under the "scripts" section
这些将添加到“脚本”部分下的 package.json
{
//other package.json stuff
"scripts":{
"start": "node --max-old-space-size=4076 server.js"
}
}
then to run it call npm startinstead of typing in node + args + execution point.
然后运行它调用npm start而不是输入节点 + args + 执行点。
Note: if you name it something other than start, npm run [yourScriptNameHere]will be the command to run it
注意:如果您将其命名为 start 以外的其他名称,npm run [yourScriptNameHere]则将是运行它的命令
This is a better option than trying to reconfigure node to use 4gb by default (don't even know if its possible tbh). It makes your configuration portable by using the baked in methods as it stands and allows others who encounter your code in the future to understand this is a need.
这是一个比尝试重新配置节点以默认使用 4gb(甚至不知道它是否可能 tbh)更好的选择。它通过使用现成的方法使您的配置可移植,并允许将来遇到您的代码的其他人理解这是一种需要。
回答by Kevin Cooper
You can also set NODE_OPTIONSwhen running an npm script rather than nodeitself:
您还可以NODE_OPTIONS在运行 npm 脚本而不是node它本身时进行设置:
"scripts": {
"start": "NODE_OPTIONS=--max-old-space-size=4096 serve",
},
回答by Prashant K
There are two ways to resolve this:
有两种方法可以解决这个问题:
- Setting memory size explicitly for your application, already answered above.
- Setting memory size globally for node - This is done by creating an Environment variable, with
- 为您的应用程序明确设置内存大小,上面已经回答。
- 为节点全局设置内存大小 - 这是通过创建一个环境变量来完成的,使用
variable name=NODE_OPTIONS
变量名=NODE_OPTIONS
and
和
variable value=--max-old-space-size=4096
变量值=--max-old-space-size=4096

