nodejs decrease v8 garbage collector memory usage

前端 未结 1 799
渐次进展
渐次进展 2020-12-09 11:03

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

1条回答
  •  有刺的猬
    2020-12-09 11:54

    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

    0 讨论(0)
提交回复
热议问题