The GC supports a variety of latency modes (e.g. System.Runtime.GCLatencyMode). These modes guide the behavior of the GC such that it manages its level of intrusiveness to the process it’s servicing. Of particular interest is the LowLatency mode, which can offer temporary performance enhancements. Setting this can be tricky and can actually degrade performance if not used correctly; however, under the correct circumstances this can be quite beneficial.
This setting is only meant to be used for very short-periods of time when select processes need to run with minimal (but not zero) interruptions by GC. Here are a few rules to guide its consideration?
- Only consider this option if you’re using Workstation GC. If you’re using Server GC, this will provide you no value because LowLatency is not supported for Server GC
- If your application causes relatively high rates of Generation 2 collection this could be a candidate. This will not reduce time in GC of Generation 0 or 1
- If the system hosting your process is always under memory pressure, this will likely have little effect. This is because LowLatency constraints are bypassed if the OS signals low memory conditions
- If you enable LowLatency, make sure any processes you have that would explicitly request collection are also disabled (especially gen 2), otherwise the effects will be lost
- Only consider LowLatency if you have a clearly definable line-of-execution that you feel needs to run with little interruption
- For Example: If your product follows a circuit-breaker pattern to guarantee a QoS, when the breaker is tripped you could enable Low Latency long enough to allow your process to recover itself, catch-up, etc. When the breaker is reset, revert LowLatency mode