Skip to content

rts: Gradually return retained memory to the OS

Matthew Pickering requested to merge wip/return-memory-fd into master

Related to #19381 (closed) #19359 (closed) #14702 (closed)

After a spike in memory usage we have been conservative about returning allocated blocks to the OS in case we are still allocating a lot and would end up just reallocating them. The result of this was that up to 4 * live_bytes of blocks would be retained once they were allocated even if memory usage ended up a lot lower.

For a heap of size ~1.5G, this would result in OS memory reporting 6G which is both misleading and worrying for clients. In long-lived server applications this results in consistent high memory usage when the live data size is much more reasonable (for example ghcide)

Therefore we have a new (2021) strategy which starts by retaining up to 4 * live_bytes of blocks before gradually returning uneeded memory back to the OS on subsequent major GCs which are NOT caused by a heap overflow.

Each major GC which is NOT caused by heap overflow increases the consec_idle_gcs counter and the amount of memory which is retained is inversely proportional to this number. By default the excess memory retained is oldGenFactor (controlled by -F) / 2 ^ (consec_idle_gcs * returnDecayFactor)

On a major GC caused by a heap overflow, the consec_idle_gcs variable is reset to 0 (as we could continue to allocate more, so retaining all the memory might make sense).

Therefore setting bigger values for -Fd makes the rate at which memory is returned slower. Smaller values make it get returned faster. Setting -Fd0 means no additional memory is retained.

The default is -Fd4 which results in the following scaling:

> mapM print [(x, 1/ (2**(x / 4))) | x <- [1 :: Double ..20]]

So after 13 consecutive GCs only 0.1 of the maximum memory used will be retained.

Further to this decay factor, the amount of memory we attempt to retain is also influenced by the GC strategy for the oldest generation. If we are using a copying strategy then we will need at least 2 * live_bytes for copying to take place, so we always keep that much. If using compacting then we need a lower number, so we just retain at least 1.2 * live_bytes for some protection.

Merge request reports