Introduction
.NET offers many useful features like Base Class Library [BCL], Just in Time compiler [JIT], intermediate code known as MSIL, code access security and Garbage Collector aka GC. GC is responsible for memory management and is credited with the success of .NET platform as well as some really strange stories [1].
Basics of Memory Allocation
In .NET, value types are stored on the stack and reference types [objects] are stored on heap. On deeper examination – you will find that pointers to the object are stored on the stack but the actual object memory is allocated on heap. In .NET, heap based memory is managed by GC. GC frees up the programmer from keeping count of elements stored in an array, reference counting, etc. and provides protection from common memory leaks and buffer overrun problems.

Figure 1: Memory Allocation
GC Roots – A brief explanation
GC roots represent memory locations always reachable from the program. There are four types of GC roots – a local variable in the method, static variables, managed object passed to COM and objects that implements Finalizers. Reference counting logic is used periodically to examine GC roots and to mark objects not required in future.
All objects for which we have active reference in GC roots will be marked as “live” and objects for which GC cannot find reference will be marked as “ready for collection”. Objects marked as ready for collection are removed from memory during GC sweep process. [GC operates on “mark and sweep” principal -more on this will be covered in future posts].
GC distributes available memory in different generations as Generation 0, 1 and 2 as explained below.

Figure 2: Managed Memory
Objects always start in Generation 0 of GC. All objects that survive one cycle of Generation 0 collection get promoted to Generation 1. All objects that survive one cycle of Generation 1 collection get promoted to Generation 2. Garbage Collection for Generation 2 will cause collection for Gen1 as well as Gen 0 - also known as full collection.
GC Mode of operations: Concurrent Vs Synchronous [5]
GC supports concurrent or workstation as well as synchronous or server mode of operations. Usually, concurrent mode is used in desktop applications and synchronous mode is used in server applications like ASP.NET. In concurrent mode, GC will avoid stopping the application while garbage collection is in progress. In Synchronous mode, GC will suspend the application operation while garbage collection is in progress. Mode in which GC will operate has direct impact on application performance. CLR supports Workstation GC with Concurrent GC off [default option], Workstation GC with Concurrent GC on and Server GC mode of operations. GC mode can be set in the configuration file of the application [4].
In future posts we will delve deeper into the generational logic of GC and how memory is managed. We will also explore the impact of application level code on GC behavior.
References:
1. http://www.codeproject.com/KB/showcase/IfOnlyWedUsedANTSProfiler.aspx
2. http://en.wikipedia.org/wiki/Heap_(data_structure)
3. http://msdn.microsoft.com/en-us/magazine/bb985010.aspx
4. http://vineetgupta.spaces.live.com/blog/cns!8DE4BDC896BEE1AD!1104.entry
5. http://www.simple-talk.com/dotnet/.net-framework/understanding-garbage-collection-in-.net/