Does RUBY_GC_MALLOC_LIMIT affect the frequency of Major GC cycles?
27K reputation · 27 Apr 2021, 01:51 UTC
Ruby's generational garbage collector uses specific thresholds to determine when to trigger a Minor GC versus a full Major GC. While RUBY_GC_MALLOC_LIMIT is used to tune the threshold for memory allocations that trigger a GC run, the relationship between this limit and the promotion of objects to the old generation is not immediately clear.
In a high-allocation environment using MRI Ruby, there is a need to balance memory throughput with the stop-the-world pauses associated with Major GC. If the malloc limit is increased to reduce the frequency of GC cycles, it is uncertain whether this also delays the triggering of a Major GC or simply increases the volume of objects processed during each cycle.
- Does increasing
RUBY_GC_MALLOC_LIMITdirectly reduce the frequency of Major GC events? - What is the specific interaction between this limit and the generational promotion threshold?
1 answer
1 question comment
Use comments to ask for clarification. Post a solution as an answer.
27,025 reputation · 27 Apr 2021, 07:59 UTC
Increasing RUBY_GC_MALLOC_LIMIT raises the allocation counter that must be exceeded before the young‑generation (minor) GC runs. Because the counter is reset after each minor collection, a higher limit means fewer minor GCs per unit time, but each minor GC has to scan a larger young‑generation heap. More objects survive each scan, so the promotion rate to the old generation can rise, which in turn feeds the old‑generation occupancy counters that trigger a Major GC. Thus the effect on Major GC frequency is indirect and workload‑dependent: if most allocations are short‑lived, raising the limit may barely change promotion; if many objects have medium lifetimes, the higher limit can increase Major GC frequency.