Garbage Collection

Garbage Collection Prior to .NET 4.0
As you probably know, the CLR allocates memory for your applications as they require it and assumes an infinite amount of memory is available (you wish). This is a mad assumption, so a process called the garbage collector (GC) is needed in order to clean up unused resources. The GC keeps an eye on available memory resources and will perform a cleanup in three situations:
• When a threshold is exceeded
• When a user specifically calls the garbage collector
• When a low system memory condition occurs

To make this as efficient as possible, the GC divides items to be collected into “generations.” When an item is first created, it is considered a generation 0 item (gen 0), and if it survives subsequent collections (it is still in use), it is promoted to a later generation: generation 1 and later generation 2.

This division allows the garbage collector to be more efficient in the removal and reallocation of memory. For example, generation 0 items mainly consist of instance variables that can be quickly removed (freeing resources earlier) while the older generations contain objects such as global variables that will probably stick around for the lifetime of your application. On the whole, the GC works very well and saves you writing lots of tedious cleanup code to release memory.

The GC operates in a number of modes: workstation, concurrent workstation (default for multicore machines), and server. These modes are optimized for different scenarios. For example, workstation is the default mode and is optimized for ensuring that your applications have a quick response time (important for UI-based applications) while server mode is optimized for throughput of work (generally more important for server type applications). Server mode does pause all other managed threads during a garbage collection, however. If server mode were used for a Windows Forms application, this collection could manifest itself as intermittent pauses, which would be very annoying.



Garbage Collection in .NET 4.0
So what’s changed then? Prior to .NET 4.0, a concurrent workstation GC could do most but not all of a generation 0 and 1 collection at the same time as a generation 2 collection. The GC was also unable to start another collection when it was in the middle of a collection which meant that only memory in the current segment could be reallocated.

In .NET 4.0, however, concurrent workstation GC collection is replaced by background garbage collection. The simple explanation (and GC gets very complex) is that background garbage collection allows another GC (gen 0 and 1) to start at the same time as an existing full GC (gen 0, 1, and 2) is running, reducing the time full garbage collections take. This means that resources are freed earlier and that a new memory segment could be created for allocation if the current segment is full up.

Background collection is not something you have to worry about􀀁it just happens and will make your applications perform more quickly and be more efficient, so it’s yet another good reason to upgrade your existing applications to .NET 4.0.

Background collection is not available in server mode GC, although the CLR team has stated they are aiming to achieve this in the next version of the framework. The GC team has also done work to ensure that garbage collection works effectively on up to 128 core machines and improved the GC’s efficiency, reducing the time needed to suspend managed threads

For more information and a detailed interview with the GC team, please refer to http://blogs.msdn.com/ukadc/archive/2009/10/13/background-and-foreground-gc-in-net-4.aspx and http://channel9.msdn.com/shows/Going+Deep/Maoni-Stephens-and-Andrew-Pardoe-CLR-4-Inside-Background-GC/.



GC.RegisterForFullGCNotification()
It is worth noting that from .NET 3.5SP1, the CLR has a method called GC.RegisterForFullGCNotification() that lets you know when a generation 2 or large heap object collection occurs in your applications. You might want to use this information to route users to a different server until the collection is complete.


Source of Information : Apress Introducing dot NET 4.0 with Visual Studio 2010

0 comments


Subscribe to Developer Techno ?
Enter your email address:

Delivered by FeedBurner