Pages

Wednesday, September 21, 2011

Garbage Collection in .NET

The .NET Framework's garbage collector manages the allocation and release of memory for your application. Each time you create a new object, the common language runtime allocates memory for the object from the managed heap.

Fundamentals of Memory
     - Each process has its own separate virtual address space.
     - By default, on 32- bit computers, each process has 2 gb user-mode virtual address space.
     - As an application developer, you work only with virtual address space and never manipulate physical memory directly. The garbage collector allocates and frees virtual memory for you on the managed heap.
     - Virtual memory can be in three states:
               - Free: The block of memory has no references to it and is available for allocation.
               - Reserved: The block of memory is available for your use and cannot be used for any other allocation request. However, you cannot store data to this memory block until it is committed.
               - Committed: The block of memory is assigned to physical storage.
    - Virtual address space can get fragmented.  When a virtual memory allocation is requested, the virtual memory manager has to find a single free block that is large enough to satisfy that allocation request. 
      - You can run out of memory if you run out of virtual address space to reserve or physical space to commit.  In the common language runtime (CLR), the garbage collector serves as an automatic memory manager. It provides the following benefits:

 Fundamentals of Garbage Collection:

              - Enables you to develop your application without having to free memory.
              - Allocates objects on the managed heap efficiently.
              - Reclaims objects that are no longer being used, clears their memory, and keeps the memory available for future allocations. 
             - Provides memory safety by making sure that an object cannot use the content of another object

Conditions for a Garbage Collection:
        Garbage collection occurs when one of the following conditions is true:
              - The system has low physical memory.
              - The memory that is used by allocated objects on the managed heap surpasses an acceptable threshold. This means that a threshold of acceptable memory usage has been exceeded on the managed heap. This threshold is continuously adjusted as the process runs.
               - The GC.Collect method is called.

The Managed 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.
       - To reserve memory, the garbage collector calls the Win32 VirtualAlloc function, and reserves one segment of memory at a time for managed applications. The garbage collector also reserves segments as needed, and releases segments back to the operating system by calling the Win32 VirtualFree function.
       - The fewer objects allocated on the heap, the less work the garbage collector has to do.
         - When a garbage collection is triggered, the garbage collector reclaims the memory that is occupied by dead objects. The reclaiming process compacts live objects so that they are moved together, and the dead space is removed, thereby making the heap smaller.
         - The intrusiveness (frequency and duration) of garbage collections is the result of the volume of allocations and the amount of survived memory on the managed 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.        


Generations:
      - The heap is organized into generations so it can handle long-lived and short-lived objects. Garbage collection primarily occurs with the reclamation of short-lived objects that typically occupy only a small part of the heap. There are three generations of objects on the heap:
         Generation 0:
               - Youngest generation.
               - Contains short lived objects.
               - Example - Temporary variables / Local variable.
               - GC occurs most frequestly.
         Generation 1:
               - Contains short lived objects and servs as a buffer between short lived and long lived objects
         Generation 2:
               - Contains long lived objects
               - Example - Server application that contains static data that live for the duration of the process.
     - Collecting a generation means collection objects in that generation and all its younger generation.

     - The allocation of objects starts with Generation 0. when the Generation 0 is filled completely, a collect method is called and reclaims the unused memory. And now these objects are moved to Generation 1 in a continous memory allocation (thus utilising the memory the best way.). Similarly the Generation 1 objects are moved to Generation 2.


What happens during a Garbage Collection:
Phases:
     -Making Phase:
            Finds and creates a list of all live objects.
     -Relocating Phase:
            Updates the references to the objects taht will be compacted.
     -Compacting Phase:
            Reclaims the space occupied by the dead objects and compacts the surviving ojects.
    The large object heap is not compacted, as this would increase memory usage over an unacceptable leangth of time.


Types of Garbage Collection:
     - Workstation Garbage Collection:
              - Which is for all client workstation and stand alone PCs.
              - Can be concurrent or non concurrent.
              - Concurrent garbage collection enables managed threads to continue operations during a garbage collection.
              - Starting with .Net framework 4, background garbage collection replaces concurrent garbage collection.
      - Server Garbage Collection:
              - Which is intended for server applications that need high throughput and scalability.
              - Always non concurrent.


Configuring Garbage Collection:
       - To specify the type of garbage collectionuse  <gcServer> element with attribute enabled set to false/true. Default it is set to false. Thus workstation.
       - To specify concurrency / non concurrency, use <gcConcurrent > element with enable attribute set to false / true. Default it is enabled.
     




 

No comments:

Post a Comment