In .Net framework 4.0, Microsoft has added new feature related to garbage collection called as Background garbage collection. Before going to Background garbage collection let’s first revise the basic garbage collection.
What is garbage collection?

In .net world, Garbage collection is nothing but collecting unused memory locations by freeing unused objects from managed heap memory.
How it works?
Have you notice a how cleaning staff remove all the garbage from a trash bag. They just open the bag and empty it in a bigger pot. So when they empty the trash bag, new garbage items comes out first and then the old one and then older ones and so on.
Imagine a trash bag with three section new garbage section, older garbage Section and oldest garbage Section. Garbage automatically moves from one section to another as it gets older.
In the similar way managed heap memory is managed in three sections generation 0 (young objects), generation 1 (older objects) and generation 2 (oldest objects).
Garbage collector first runs a check on generation 0, if it doesn’t found any free objects it runs a check on generation 1 and then checks on generation 2.
- Generation 0. This is the youngest generation and contains short-lived objects. E.g. temp variable. Garbage collection occurs most frequently in this generation.
- Generation 1. This generation contains short-lived objects and serves as a buffer between short-lived objects and long-lived objects.
- Generation 2. This generation contains long-lived objects. An example of a long-lived object is an object in a server application that contains static data that is live for the duration of the process.
Life Cycle of object in Garbage Collector context

| Sr.No. |
Concurrent Garbage Collection |
Background Garbage Collection |
Foreground Garbage Collection |
| 1 |
Garbage collector thread runs concurrently with the other threads |
In Background Garbage Collection, generation 0 and 1 (ephemeral generations) are collected as needed while generation 2 collection is in progress |
Being a part of background garbage collection, Collection of generation 0 and 1 (ephemeral generations) during the background garbage collection is known as foreground garbage collection. |
| 2 |
Only applicable to generation 2 while generation 0 and 1 are non-concurrent. |
Only applicable to generation 2. |
Only applicable to generation 0 and 1. |
| 3 |
For freeing the generation 0 and 1 object you have to wait till concurrent garbage collector finishes the generation 2. |
Background garbage collection suspends itself when foreground garbage collection is in progress. |
Foreground garbage collection is not running when background garbage collection is running. |
| 4 |
Framework 3.5 or below |
Framework 4.0 or above |
Framework 4.0 or above |
| 5 |
You can configure this collection using <gcServer> element in the run time configuration schema. |
There is no configuration option for this collection. It gets automatically enable with the concurrent garbage collection. |
There is no configuration option for this collection. It gets automatically enable with the concurrent garbage collection. |
| 6 |
Allocation restrictions |
No Allocation restrictions |
|
| 7 |
Server and workstation option available |
Only workstation option available |
Only workstation option available |
Hope this article will help you understanding existing and new feature of garbage collection in .net framework 4.0.
Feel free to send me your queries regarding garbage collection and I will try my level best to address those queries.
- Abhi
________________________________________________________________________
Glossary
Manage Heap – After the garbage collector is initialized by the CLR, it allocates a segment of memory to store and manage objects. This memory is called the managed heap, as opposed to a native heap in the operating system.
There is a managed heap for each managed process. All threads in the process allocate objects on the same heap. The heap can be considered as the accumulation of two heaps: the large object heap and the small object heap. The large object heap contains objects that are 85,000 bytes and larger. Very large objects on the large object heap are usually arrays. It is rare for an instance object to be extremely large.
A marking phase that finds and creates a list of all live objects.
A relocating phase that updates the references to the objects that will be compacted.
A compacting phase that reclaims the space occupied by the dead objects and compacts the surviving objects. The compacting phase moves objects that have survived a garbage collection toward the older end of the segment.
Because objects in generations 0 and 1 are short-lived, these generations are known as the ephemeral generations.