CHAPTER 13 UNDERSTANDING OBJECT LIFETIME
Background Garbage Collection Under .NET 4.0 and Greater
Beginning with .NET 4.0, the garbage collector is able to deal with thread suspension when it cleans up
objects on the managed heap, using background garbage collection. Despite its name, this does not
mean that all garbage collection now takes place on additional background threads of execution. Rather,
if a background garbage collection is taking place for objects living in a nonephemeral generation, the
.NET runtime is now able to collect objects on the ephemeral generations using a dedicated background
thread.
On a related note, the .NET 4.0 and higher garbage collection has been improved to further reduce
the amount of time a given thread involved with garbage collection details must be suspended. The end
result of these changes is that the process of cleaning up unused objects living in generation 0 or
generation 1 has been optimized and can result in better runtime performance of your programs (which
is really important for real-time systems that require small, and predictable, GC stop time).
Do understand, however, that the introduction of this new garbage collection model has no effect
on how you build your .NET applications. For all practical purposes, you can simply allow the .NET
garbage collector to perform its work without your direct intervention (and be happy that the folks at
Microsoft are improving the collection process in a transparent manner).
The System.GC Type
The mscorlib.dll assembly provides a class type named System.GC that allows you to programmatically
interact with the garbage collector using a set of static members. Now, do be very aware that you will
seldom (if ever) need to make use of this class directly in your code. Typically, the only time you will use
the members of System.GC is when you are creating classes that make internal use of unmanaged
resources. This could be the case if you are building a class that makes calls into the Windows C-based
API using the .NET platform invocation protocol, or perhaps due to some very low-level and
complicated COM interop logic. Table 13-1 provides a rundown of some of the more interesting
members (consult the .NET Framework 4.5 SDK documentation for complete details).
Table 13-1. Select Members of the System.GC Type
System.GC Member
Description
AddMemoryPressure()
RemoveMemoryPressure()
Allows you to specify a numerical value that represents the
calling object’s “urgency level” regarding the garbage collection
process. Be aware that these methods should alter pressure in
tandem and, thus, never remove more pressure than the total
amount you have added.
Collect()
Forces the GC to perform a garbage collection. This method has
been overloaded to specify a generation to collect, as well as the
mode of collection (via the GCCollectionMode enumeration).
CollectionCount()
Returns a numerical value representing how many times a given
generation has been swept.
GetGeneration()
Returns the generation to which an object currently belongs.
481