.NET 3.5 Garbage Collection Notifications

As of .NET 3.5 it possible to know when the GC is about to kick in, allowing you that have better control over the side-effects of the Garbage Collection (GC).

For example the GC will block the execution of threads in the process it is running for, during its execution. The impact on the threads differs depending on your GC configuration and if you’re running a workstation or server OS but in both scenarios threads will be blocked during some time. This could make server applications unresponsive resulting in timeouts when clients would connect on this exact moment.
During the time the GC is active, based on the notifications you can get from the .NET 3.5 GC classes, you could for example forward the client requests to another server.

There are two methods to indicate the GC is about to start and when it is finished, respectively WaitForFullGCApproach and WaitForFullGCComplete of the GC class.

The trouble is these are methods and not (yet ?) events.  So you will need to have a thread polling the WaitForFullGCApproach method which I personally find a bit of hassle. For more details have a look at the MSDN website: Garbage Collection Notifications

A good general introduction and best practices about the GC you’ll find on Maon’s weblog. He wrote some articles about using the GC efficiently. Even though they were written in 2004 and 2005 they still contain some useful insight when dealing with memory performance problems. Just follow the links below:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.