GC modes and settings

Workstation or server

The runtime has two collectors. Workstation GC is the default for a console app: one heap, one thread collecting, short pauses. Server GC is for a server process: one heap per CPU, parallel collection and a larger throughput at the cost of memory. The mode is fixed when the runtime starts, so the program can only report it:

using System.Runtime; Console.WriteLine($"server {GCSettings.IsServerGC}"); Console.WriteLine($"latency {GCSettings.LatencyMode}"); Console.WriteLine($"generations {GC.MaxGeneration}"); Console.WriteLine($"processors {Environment.ProcessorCount}"); var info = GC.GetGCMemoryInfo(); Console.WriteLine($"available {info.TotalAvailableMemoryBytes}");

dotnet run

server False
latency Interactive
generations 2
processors 4
available 12240678912

This container has four CPUs and workstation GC, which is what a plain dotnet run gives. The same program started with the variable below prints server True in the first line and the rest unchanged, which was measured here:

DOTNET_gcServer=1 dotnet run

Latency mode

GCSettings.LatencyMode is the one setting a running program may change. It tells the collector how much it may pause:

using System.Runtime; Console.WriteLine($"latency {GCSettings.LatencyMode}"); GCSettings.LatencyMode = GCLatencyMode.SustainedLowLatency; Console.WriteLine($"latency {GCSettings.LatencyMode}"); GCSettings.LatencyMode = GCLatencyMode.Batch; Console.WriteLine($"latency {GCSettings.LatencyMode}");

dotnet run

latency Interactive
latency SustainedLowLatency
latency Batch

The property reads back what was set, and a mode that the runtime refuses throws InvalidOperationException instead of being ignored. A library should not set it: the mode belongs to the host that knows its own deadlines.

HeapSizeBytes and friends

GC.GetGCMemoryInfo() returns a snapshot of the heap. After five hundred four-kilobyte arrays are kept alive, the numbers describe them:

var items = new List<byte[]>(); for (var i = 0; i < 500; i++) { items.Add(new byte[4096]); } var info = GC.GetGCMemoryInfo(); Console.WriteLine($"heap {info.HeapSizeBytes}"); Console.WriteLine($"fragmented {info.FragmentedBytes}"); Console.WriteLine($"committed {info.TotalCommittedBytes}"); Console.WriteLine($"items {items.Count}");

dotnet run

heap 1575296
fragmented 5136
committed 3235840
items 500

Two megabytes of payload are live, the heap reports about 1.5 of them plus headers and the list, and the committed memory is larger than the heap because the collector keeps room to allocate into. The fragmented part is the free space between live objects that is too small to reuse, and it is the number to watch in a long-running process.

The environment variables

VariableWhat it does
DOTNET_gcServer1 selects server GC, 0 workstation; the program cannot change it.
DOTNET_gcConcurrent0 turns off the background collection on the workstation heap.
DOTNET_GCHeapHardLimitA hard limit in bytes, written in hexadecimal in the runtime configuration.
DOTNET_GCConserveMemoryA value from 0 to 9 that makes the collector trade speed for a smaller heap.
DOTNET_TieredCompilationNot a GC setting, but in the same family: it turns tiered JIT compilation on or off.

A variable is spelled with the DOTNET_ prefix, and the same settings exist as properties in the project file (<ServerGarbageCollection>true</ServerGarbageCollection>), which writes them into the runtime configuration of the published app. A container sets the variable; a published app carries the property. The lesson on the project file shows where a property goes.

Exercises

  1. Run the first program with DOTNET_gcServer=1 and note which lines changed.
  2. Add DOTNET_GCConserveMemory=9 to the third program and compare the committed bytes.
  3. Set the latency mode to NoGCRegion without entering a region and read the exception.
  4. Print GC.GetGCMemoryInfo().GenerationInfo and find the size of generation 0 and generation 2.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Which GC mode does a plain console app use here?

    Answer

    Workstation GC

    Server GC is opt-in through the project property or DOTNET_gcServer, and this run printed False.

  2. What does server GC give up for its throughput?

    Answer

    Memory, because it keeps one heap per CPU

    A heap per CPU means more committed memory for the same live set.

  3. What does this program print? using System.Runtime; Console.WriteLine(GCSettings.IsServerGC);

    Answer

    False

    GCSettings lives in the System namespace, and workstation is the default.

  4. Which latency mode allows the longest pauses?

    Answer

    Batch

    Batch trades pause time for throughput, which suits a process with no deadline.

  5. What does GC.GetGCMemoryInfo().FragmentedBytes report?

    Answer

    Free space in the heap that is too small to reuse

    Fragmentation is the waste between live objects, and it grows in a long-running process.

  6. What does this program print? using System.Runtime; GCSettings.LatencyMode = GCLatencyMode.Batch; Console.WriteLine(GCSettings.LatencyMode);

    Answer

    Batch

    The property reads back the mode that was set.

  7. Can a program switch to server GC at run time?

    Answer

    No, the mode is fixed when the runtime starts

    The heaps are created at startup, so the choice is a configuration, not a property.

  8. Which variable selects server GC?

    Answer

    DOTNET_gcServer=1

    The runtime reads DOTNET_ names, and the measured run with that variable printed True.

  9. Why is the committed memory larger than the heap size?

    Answer

    The collector keeps room to allocate into

    Committed is the memory reserved from the OS, and the heap is the part in use.

  10. A service must keep pauses short for a minute of traffic. Which mode fits?

    Answer

    SustainedLowLatency

    It keeps pauses short for a period, at the cost of memory, which is the trade a deadline needs.

The code on this page was built with the .NET SDK 10.0.303 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev

Banner image