Geeks With Blogs


Microsoft Store

Support This Site

AddThis Social Bookmark Button

Locations of visitors to this page

Subscribers to this feed

TwitterCounter for @sdorman

Creative Commons License

Scott Dorman Microsoft MVP, Software Architect, Developer, Author

The .NET garbage collector is one of the areas of the .NET Framework that is extremely important and probably one of the least understood. There are a lot of articles written about it and there have been very few changes since .NET 1.0 was first released. (There have been changes with almost each release, but they have been relatively minor.)

With .NET 4.0, however, there are some fairly substantial changes to the GC that will have some interesting performance implications (in a good way).

For a quick review, the GC in .NET is a generational garbage collector with 3 generations. Generation 0 and 1 collections are very fast since the segment (called the ephemeral segment) is small while Generation 2 collections can be relatively slow.

The GC in .NET also has two modes of operation: Server and Workstation. In Server GC, the algorithm maximizes the overall throughput but all managed code must be paused while it runs. In CLR 4, you can now subscribe to an event to be notified before a Generation 2 or Large Object Heap collection.

public static void Main(string[] args)
    // Register for a set of notifications.
    // Parameters require tuning. First is 
    // for Gen2, second, Large Object Heap 
    GC.RegisterForFullGCNotification(10, 10);

    // Start a thread using WaitForFullGCProc
    Thread thWaitForFullGC = new Thread(new ThreadStart(WaitForFullGCProc));

  catch (InvalidOperationException invalidOp)
    Console.WriteLine("GC Notifications are not supported while concurrent GC is enabled.\n” + invalidOp.Message);

public static void WaitForFullGCProc()
    while (true)
    // Wait for a notification
    GCNotificationStatus s = GC.WaitForFullGCApproach();

    if (s == GCNotificationStatus.Succeeded)
      // This call will direct new traffic
      // away from machine; wait for old
      // traffic to finish; then call
      // GC.Collect()
    // Wait for a notification of completion
    s = GC.WaitForFullGCComplete();
    if (s == GCNotificationStatus.Succeeded)

The changes in the server GC will probably only affect a small number of applications. However, the changes to the workstation GC (which is the default mode) will affect almost all .NET applications.

In all .NET Framework versions from 3.5SP1 and earlier, workstation GC used a concurrent collection method. This means that the GC can do most, but not all, of a Generation 2 collection without pausing managed code. It can’t, however, do a Generation 0 and Generation 1 collection at the same time as a Generation 2 collection.

CLR 4.0 changes that to support background collection, which can do a Generation 0 and Generation 1 collection at the same time as a Generation 2 collection. This means that now only unusual circumstances should lead to long latency times.


These charts are from some performance testing done by Microsoft and presented during PDC which shows how the new background collection algorithm should greatly reduce the latency times.

Digg This
Posted on Friday, November 7, 2008 2:37 PM .NET (General) | Back to top

Comments on this post: CLR 4.0: Garbage Collection Changes

# re: CLR 4.0: Garbage Collection Changes
Requesting Gravatar...
I've been suspicious of long latencies during simultaneous Gen0/1/2 collections, but could not pin it down. Is it possible that such latencies could run into the 5 second range on a 1GHz / 1GB machine?
Left by muebel on Nov 10, 2008 2:09 PM

# re: CLR 4.0: Garbage Collection Changes
Requesting Gravatar...
muebel, I think it is possible but probably rather unlikely that the latency times would run that high even on a 1GHz machine with 1GB of memory. The best way to tell will be to run PerfMon and monitor the amount of time spent in GC.
Left by Scott on Nov 15, 2008 10:02 PM

# re: CLR 4.0: Garbage Collection Changes
Requesting Gravatar...
So the difference between 3.5SP1 and 4.0 is, it has including Generation 1 and 2. In other words the Background Garbage Collection is not completely a background process, and still for Generation 2 it need to pause few Threads?
Left by Nazir Ahmed on Jul 30, 2010 1:12 AM

Your comment:
 (will show your gravatar)

Copyright © Scott Dorman | Powered by: | Join free