NodeJS 推荐的 max-old-space-size

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48387040/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:39:55  来源:igfitidea点击:

NodeJS recommended max-old-space-size

node.jsv8

提问by Borjante

I'm having some trouble to understand how NodeJS acts based on the parameter max-old-space-size.

我在理解 NodeJS 如何根据参数 max-old-space-size 进行操作时遇到了一些麻烦。

In my case for example, I'm running two t2.small aws instances (2GB of RAM).

例如,在我的情况下,我正在运行两个 t2.small aws 实例(2GB 的 RAM)。

Not sure why but I did set max-old-space-size=4096 (4GB). What does node do in this case? Could this configuration lead to a posible memory allocation failure?

不知道为什么,但我确实设置了 max-old-space-size=4096 (4GB)。节点在这种情况下做什么?这种配置会导致可能的内存分配失败吗?

How do I determine the correct value of max-old-space-size based on the server resources?

如何根据服务器资源确定 max-old-space-size 的正确值?

PD: My application is constantly growing the memory usage and I'm trying to understand everything about node internals.

PD:我的应用程序不断增加内存使用量,我正在尝试了解有关节点内部结构的所有内容。

回答by jmrk

"Old space" is the biggest and most configurable section of V8's managed (aka garbage-collected) heap (i.e. where the JavaScript objects live), and the --max-old-space-sizeflag controls its maximum size. As memory consumption approaches the limit, V8 will spend more time on garbage collection in an effort to free unused memory.

“旧空间”是 V8 的托管(也称为垃圾收集)堆(即 JavaScript 对象所在的位置)中最大和最可配置的部分,该--max-old-space-size标志控制其最大大小。随着内存消耗接近极限,V8 将花费更多时间在垃圾收集上以释放未使用的内存。

If heap memory consumption (i.e. live objects that the GC cannot free) exceeds the limit, V8 will crash your process (for lack of alternative), so you don't want to set it too low. Of course, if you set it too high, then the additional heap usage that V8 will allow might cause your overall system to run out of memory (and either swap or kill random processes, for lack of alternative).

如果堆内存消耗(即 GC 无法释放的活动对象)超过限制,V8 将使您的进程崩溃(由于缺乏替代方案),因此您不想将其设置得太低。当然,如果您将其设置得太高,那么 V8 允许的额外堆使用可能会导致您的整个系统内存不足(并且由于缺乏替代方案而交换或终止随机进程)。

In summary, on a machine with 2GB of memory I would probably set --max-old-space-sizeto about 1.5GB to leave some memory for other uses and avoid swapping.

总之,在具有 2GB 内存的机器上,我可能会设置--max-old-space-size为大约 1.5GB,以便为其他用途留出一些内存并避免交换。

回答by satish suryawanshi

If you just want to make it work from the project you are working on, Follow the two steps:

如果您只是想让它从您正在处理的项目中工作,请按照以下两个步骤操作:

  1. In your project cd to node_module/@angular cd node_module/@angular

  2. Now run the below command export NODE_OPTIONS=--max-old-space-size=8192

  1. 在你的项目中 cd 到 node_module/@angular cd node_module/@angular

  2. 现在运行以下命令 export NODE_OPTIONS=--max-old-space-size=8192