Background GC
Context: Background GC (also called concurrent GC) allows the garbage collector to collect generations 0 and 1 while a full generation 2 collection is in progress. This reduces application pauses because the GC can do most of its work on a background thread. Before .NET 4.5, full collections would freeze all application threads. With background GC, only short pauses occur, improving responsiveness, especially for server workloads.
Usage Example
Section titled “Usage Example”using System;using System.Runtime;
public class BackgroundGCDemo{ public static void ShowStatus() { // Check if background GC is enabled (it is by default) GCSettings.LatencyMode mode = GCSettings.LatencyMode; Console.WriteLine($"Latency mode: {mode}"); // For background GC, mode is usually "Interactive" or "Batch" }}Output console
Section titled “Output console”dotnet runLatency mode: InteractiveImportant notes
Section titled “Important notes”- Enabled by default in modern .NET (Core 3.0+).
- Reduces visible pauses in interactive applications.
- Can be disabled via configuration (not recommended).
Real‑world usage example
Section titled “Real‑world usage example”Real‑time dashboard – A financial dashboard that updates every second benefits from background GC because it avoids UI freezes during full collections.
See .NET docs on background GC.