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.

2 comments:

Michael said...

I keep getting that it can't load
the assembly type
System.Runtime.GCLatencyMode

Cale Teeter said...

Michael,

Are you sure you have at least 2.0 SP1 installed? I can provide some sample code but this works for me. Your mscorlib (where this resides) should be 2.0.50727.3053. Also you could use ILDASM to verify the enum and property exists in your version of mscorlib. If you want a code sample I can provide one. Thanks.