Thursday, January 3, 2008

New GC Feature in Framework 2.0 SP1

There are a bevy of new functionality added with SP1 for .NET Framework 2.0, but one of particular interest to me was the changed to the garbage collector.  There are 2 major changes that I will discuss.

GC Collection Modes

There now exists a new overload to the GC.Collect method.  The new method signature is void System.GC.Collect(int generation, System.GCCollectionMode mode).  The generation parameter can be 0 to System.GC.MaxGeneration (usually 2 is upper limit on server GC).  The mode parameter can be Default, Forced or Optimized.

  • Default - same as if you call System.GC.Collect with no parameters
  • Forced - guarantees collection of all generations (currently this is the same as Default)
  • Optimized - tells GC to only collect if it is determined to be "productive"
GC Latency Modes

A new property has been added to the Runtime help in controlling the GC.  The new property signature is System.Runtime.GCLatencyMode System.Runtime.GCSettings.LatencyMode { get; set; }.  Follwing explains the differences.

  • Batch - allow GC to work with maximum throughput at the cost of responsiveness
  • Interactive - allows the GC to be balanced between max throughput and max responsiveness
  • LowLatency - allows the GC to be "stalled", useful when responsiveness or CPU load is needed by the application and GC should not interrupt.