Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • .Net Framework : Garbage collection

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 155
    Comment on it

    Garbage collection is an important technique in .Net that is used to free the memory for unused object that are no longer referenced.It has the following benefits.

     

    1. It enables you to develop an application without having worry to free memory.  

    2.  It provide memory safety by making sure that the object cannot use the content of other object.

    3. It clean memory for no longer used objects and keep memory available for future allocation.

    4. It allocates memory for object efficiently on managed heap.

     

    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 memory for objects on the same heap.

     

    Generations:

    A managed heap is organized into three generations to handle short lived and long lived objects efficiently.  Garbage Collections first reclaim short lived objects that occur only a small part of heap.
    The three generations are:

    Generation 0: It contains the newly created objects. It has short lived objects and collected frequently and the objects that survive in Generation 0 is moved to Generation 1. An example of short lived is temporary variable or objects.

    Generation 1 : It contains short lived objects and the objects that are moved from Generation 0. It serves as a buffer between short lived and long lived objects.

    Generation 2 : It contains long lived objects and the objects that are moved from Generation 1.It collected infrequently. Example of long lived objects is a object that contain data static data which is available during the process of application.

     

    Garbage Collection  phases:

    A garbage collection has 3 phases.

    1. Marking phase- In this phase garbage collector finds and creates a list of all live objects.

    2. Relocating phase-  In this phase garbage collector updates the reference of objects that will be compacted.

    3. Compacting phase- In this phase garbage collector reclaim the memory occupied by dead objects and compact the surviving objects.

     

    Types of Objects:

    1. Dead Objects: If the objects are unreachable for your code then this are dead objects. Example of dead objects are local variables in a method.

    2. Live Objects: If the objects are accessible in your code then this are Live Objects. Example of live objects are global variables.

     

    How Garbage Collection Works:

    Garbage collection is implicitly handled by .Net Framework. Garbage collection uses an algorithm that checks the objects in generation, if the object life time get over then it removed from the memory.There are two kinds of objects: Live Objects and Dead Objects. The garbage collection collects all dead objects in the generation. If the live object are running for a long time then it will be move to the next generation based on their life time.

     

    When it happens:

    A garbage collection is triggered in three ways:

    1. If your system has low physical memory, this can trigger garbage collection.

    2. Threshold is defined which indicates an acceptable level of memory on the heap which can be used by allocated objects. If this threshold is surpassed, then a garbage collection is triggered

    3. Garbage collection can explicitly be triggered by calling the GC.Collect method. Only under very rare circumstances is this ever required.

     

    Referred Link :
    https://msdn.microsoft.com/en-us/library/ee787088(v=vs.110).aspx
    http://www.c-sharpcorner.com/UploadFile/skumaar_mca/garbage-collection-in-net-framework/

     

     

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: