Garbage Collector

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:

  1. Each process has its own, separate virtual address space. All processes on the same computer share a   common physical address.
  2. 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:
Free: Block of memory has no reference to it and is available for allocation.
  • 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
Long lived objects are always stored Heap locations.

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:

  1. Large Object Heap (LOH): contains 85kb and large. In LOH usually Arrays, collection.
  2. 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.

Generation 2: this generation contains long lived objects. Example: A static variable lives up to duration of process. Survived objects remains in 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:

  1. WorkStation Garbage Collection
            Modes of GC:

a. Concurrent Garbage Collection

b. Background Garbage Collection

c. Non-Concurrent Garbage Collection

      2. Server Garbage Collection
 
              Modes of GC:

a. Background Garbage Collection

b. Non-Concurrent Garbage Collection

WorkStation Garbage Collection:

This works in client work station and standalone computer. Garbage collection triggers on user’s thread will run on normal priority. GC thread must compete with others for CPU time. Workstation GC can be Concurrent or non-concurrent GCs. This is always used on a computer that has one processor. By default concurrent mode is disabled by CLR.

  1. <configuration>  
  2.   
  3. <runtime>  
  4.   
  5.   <gcServer enabled="true|false"/>  
  6.   
  7.    <gcConcurrent enabled="true|false"/>  
  8. </runtime>  
  9.   
  10. </configuration>  

 

Server Garbage Collection:

This is intended for server applications which have multiple processors.  Server GC is faster than aa workstation because multiple garbage collection threads work. In server GC mode all threads run on Highest priority.

Server garbage collection is resource intensive. For example, if you have 10 processes and 8 processors there will be 80 dedicated garbage collections threads.

Concurrent and Background Garbage Collection:

Concurrent Garbage collection affects Gen 2. Gen 1 and Gen 0 are always non-concurrent garbage collection because they may finish less than 50ms.

It allows allocating of objects while collecting object if we don’t do ephemeral segments collection (gen 0, gen 1). Continuing allocations objects to the end of segment, we need to wait for the concurrent GC to finish so managed thread needs to suspend allocation. Couple of times we  need to suspend all threads during concurrent garbage Collection.

Background Garbage collection (BGC) is enhancement and replacement after version 4. Background garbage collection is performed on a dedicated thread and is applicable only to generation 2 collections.

While collecting on ephemeral segment all managed threads are suspended. This happens during background garbage collection  and is called foreground garbage collection.

When Background garbage collection is in progress, memory is running out in gen 0. BGC checks frequently for safe points and suspends itself. After Foreground Garbage collection is finished, BGC and managed threads can resume their work.

Performance Consideration:

Reduce as much as possible creating unnecessary new objects. If we create new objects, GC has to allocate memory. When memory is running out, it has to suspend all threads and that means Execution stopped.

Then GC will do mark - sweep objects, compacting and defragmented objects into contiguous pages, and update the track pointers. Doing more operations on heap will degrade system performance and application; also it may increase GC pause time. [3]

[1] https://msdn.microsoft.com/en-us/library/windows/desktop/aa366778(v=vs.85).aspx

[2] https://msdn.microsoft.com/en-in/library/windows/hardware/ff554836(v=vs.85).aspx

[3] https://msdn.microsoft.com/en-us/library/ms810466.aspx#heap3_commonproblems

Next Recommended Reading Garbage Collector in C#