nodejs 减少 v8 垃圾收集器的内存使用量 [英] nodejs decrease v8 garbage collector memory usage
问题描述
我正在使用 util 模块和 heapUsed<调试 nodejs 应用程序/strong> 值保持在 30-100MB 左右,heapTotal 值增长到 1.4GB.
I'm debugging a nodejs application with util module and while heapUsed value stays around 30-100MB, heapTotal value grows up to 1.4GB.
我了解到这是 v8 垃圾收集器的行为方式,但问题是如果在 512 MB 设备上运行,如何减少它分配的内存量(使其小于 1.4GB)
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
推荐答案
您需要控制最大内存大小标志(所有大小均以 MB 为单位).
You need to control the max memory size flags (all sizes are taken in MB).
node --max-executable-size=96 --max-old-space-size=128 --max-semi-space-size=1 app.js
适用于 32 位和/或 Android 和
for 32-bit and/or Android and
node --max-executable-size=192 --max-old-space-size=256 --max-semi-space-size=2 app.js
适用于 64 位非安卓系统.
for 64-bit non-android.
这些会将堆总数分别限制为 225mb 和 450mb.它不包括 JS 之外的内存使用情况.例如,缓冲区被分配为c 内存",而不是在 JavaScript 堆中.
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.
您还应该知道,越接近堆限制,在 GC 中浪费的时间就越多.例如.如果您的内存使用率为 95%,则 90% 的 CPU 将用于 GC,10% 用于运行实际代码(不是实数,但给出总体思路).所以你应该尽可能地慷慨,并且永远不要超过最大内存使用量的 16%(即 heapUsed/limit
不应大于 0.16
).16% 只是我从一些论文中回忆的,它可能不是最理想的.
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.
标志:
--max-executable-size
为可执行代码保留的最大堆大小(即时编译的 JavaScript 的本机代码结果).--max-old-space-size
为长期对象保留的最大堆大小--max-semi-space-size
为短期对象保留的最大堆大小
--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屋!