Garbage Collector:
GC is used for automatic memory management. GC allocates memory on Managed heap or Stack. When it reclaims memory, object is no long used. GC makes memory available for future use. It provides memory safety by making sure that an object cannot use the content of another object.
Fundamentals of Memory:
- Each process has its own, separate virtual address space. All processes on the same computer share a common physical address.
- By Default, on 32-bit computers each process allocates 2GB user-mode virtual address or virtual memory. Memory limits for windows and windows server Releases [1], User mode and kernel mode [2]
Virtual Space
- Virtual address appears more than physical address.
- A program can use a contiguous range of virtual addresses to access a large memory buffer that is not contiguous in physical memory. Virtual memory manager job makes it easy to find chunks of memory if it is Contiguous
- The virtual addresses used by different process are isolated from each other. The code in one process cannot alter the physical memory that is used by other programs or operating systems.
- There are three states of Virtual Memory:
- Reserved: is for future use. It is for your use and cannot be used for requested memory allocation. We need to commit to use.
- Commit: The block memory is assigned to physical memory.
- You run out of virtual memory to reserve or physical memory to commit.
Memory Allocation:
MYTH: value types get allocated on Stack. Reference Types get allocated on Heap.
CLR will allocate memory based on known storage lifetime.
Storage Life Time: a period of time which contains valid content.
Types of Locations:
- Heap
- Stack
- Register
Short live objects are always stored on Stack or Register locations.
The managed heap:
GC allocates a segment of memory to store managed objects. This memory is called managed heap, as opposed to a native heap in operating system. In managed heap GC will allocate and reclaim memory. In native heap programmer has to do it.
Frequency of garbage collection is the result of the volume of allocations and the amount of survived memory on the managed heap.
Types of Heaps:
- Large Object Heap (LOH): contains 85kb and large. In LOH usually Arrays, collection.
- Small Object Heap (SOH): contains lesser size than 85k.
Generations, Survival and Promotions-
The heap is organized into short lived and long lived objects. Objects are maintained as 3 Generations.
Generation 0- this generation contains short lived and youngest objects.
Example- temporary variable Objects.
More frequently object reclamation happens in this generation. Survived objects would promote to Generation 1.
Generation 1: this generation contains short lived objects and serves as a buffer between short lived and long lived objects. Survived objects are promoted to generation 2.
Freachable queue:
All dead objects go into finalization queue and put into Freachable queue. Well specialized CLR thread will monitor queue. GC will add new object and thread will call finalization method to reclaim memory.
Types of Garbage Collection:
- WorkStation Garbage Collection

Join the conversation! Your thoughts help the community grow.