nodejs减少了v8垃圾收集器的内存使用量 [英] nodejs decrease v8 garbage collector memory usage

查看:347
本文介绍了nodejs减少了v8垃圾收集器的内存使用量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 util 模块调试nodejs应用程序,同时 heapUsed 值保持在30-100MB左右, heapTotal 值增长到1.4GB。

以下是有关类似行为的问题



我读过这种方式,v8垃圾收集器的行为,但问题是如何减少它分配的内存量(使其小于1.4GB)如果在512 MB设备上运行,例如

解决方案

您需要控制最大内存大小标志(所有大小均以MB为单位)。 低内存设备的建议金额为

  node --max-executable-size = 96 --max-old-space-size = 128 --max-semi-space-对于32位和/或Android和

$ b,size = 1 app.js


$ b

  node --max-executable-size = 192 --max-old-space-si ze = 256  -  max-semi-space-size = 2 app.js 

适用于64位非机器人。



这些将堆总数分别限制为225mb和450mb。它不包括JS之外的内存使用。例如,缓冲区被分配为c内存,而不是在JavaScript堆中。



另外,您应该知道,越接近堆限制,浪费的时间越多在GC中。例如。如果您的内存使用率为95%,则90%的CPU将用于GC,10%用于运行实际代码(不是实数,但给出一般想法)。所以你应该尽量慷慨,并且不要超过最大内存使用量的16%(IE heapUsed / limit 不应该大于 0.16 )。 16%只是我从某篇论文中回想起来的,它可能不是最优的。标签: b
$ b


  • - max-executable-size 为可执行代码保留的堆的最大大小(即时编译的JavaScript的本地代码结果)。

  • - max-old-space-size 为长期对象保留的堆的最大大小
  • - 最大半空间大小为短期对象保留的最大堆大小

I'm debugging a nodejs application with util module and while heapUsed value stays around 30-100MB, heapTotal value grows up to 1.4GB.

Here is a question about similar behaviour

I've read that this is the way how v8 garbage collector behaves, but the question is how to decrease the amount of memory it allocates (make it less than 1.4GB) if running on 512 MB device for example

解决方案

You need to control the max memory size flags (all sizes are taken in MB).

The recommended amounts for a "low memory device" are:

node --max-executable-size=96 --max-old-space-size=128 --max-semi-space-size=1 app.js

for 32-bit and/or Android and

node --max-executable-size=192 --max-old-space-size=256 --max-semi-space-size=2 app.js

for 64-bit non-android.

These would limit the heap totals to 225mb and 450mb respectively. It doesn't include memory usage outside JS. For instance buffers are allocated as "c memory" , not in the JavaScript heap.

Also you should know that the closer you are to the heap limit the more time is wasted in GC. E.g. if you are at 95% memory usage 90% of the CPU would be used for GC and 10% for running actual code (not real numbers but give the general idea). So you should be as generous as possible with the limits and never exceed say 16% of the maximum memory usage (I.E. heapUsed/limit should not be greater than 0.16). 16% is just something I recall from some paper, it might not be the most optimal.

Flags:

  • --max-executable-size the maximum size of heap reserved for executable code (the native code result of just-in-time compiled JavaScript).
  • --max-old-space-size the maximum size of heap reserved for long term objects
  • --max-semi-space-size the maximum size of heap reserved for short term objects

这篇关于nodejs减少了v8垃圾收集器的内存使用量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆