Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Garbage Collection in .Net

    • 0
    • 1
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 187
    Comment on it

    "Garbage Collection in .Net"

        Memory Management is one of the major concern for every Application, In .Net the CLR is responsible for that.

    Overview:

      Garbage Collection is an automatic process of freeing up the memory taken up by the objects that are no longer required by the application.
    Talking about .Net, each time we instantiate an object the memory is allocated to store that object, but after some time that object may not be required by the application. So its the task of CLR to perform Garbage Collection in which it frees the memory of such objects so that our application do not run out of memory and stop working.

      Earlier in C and C++ the programmer has to explicitly deallocate the memory assigned to the objects that are no longer required, while .Net has made the programmer free from such issues.

    Introduction:

    Garbage Collection is a process performed by the Garbage Collector that lives in CLR,In this process the Garbage Collector frees up the memory occupied by the Dead Objects in the Managed Heap.

    Note:-> The Dead Objects are the objects that are unreachable by the code.

    For Example:

    When the Function returns the value that local variable has declared inside it are no longer required and hence becomes the Dead Object for the Application.

      The Garbage Collector is triggered when the System run low on physical memory. In some cases a threshold memory is defined that indicates the amount of memory on the memory heap that can be consumed by the allocated objects. When this threshold is crossed this indicates that the System will now run low on memory and the Garbage Collector is triggered.
    Whereas sometimes at very rare circumstances we explicitly trigger Garbage Collector by calling GC.Collect method.

    Working:

    Now let us see how it works:-
      As it will take a very long time for Garbage Collector to scan the entire managed heap for searching the dead object.
      Therefore what happens in the managed heap is that the object is categorised into one of the three Generations i.e Generation 0, Generation 1 and Generation 2.These Generations tells how old the object is since it was created, therefore we can say that Generation 0 is the younger object, Generation 1 is middle aged and the Generation 2 is the Oldest object.

      Now when the Garbage Collector is triggered, it occurs on a specific Generation and deallocates all the dead objects of that and younger Generations.(i.e if the Garbage Collection is performed over Generation 1 then it will deallocate all the dead objects of the Generation 0 and Generation 1).

    Assumptions made by Garbage Collector:

    Garbage Collection usually occurs on Generation 0 due to the assumption that the objects recently allocated are more likely to become dead objects.The object that is not deallocated after the Garbage Collection is performed is promoted to the next Generation, i.e the object at Generation 0 which is not deallocated will be promoted to Generation 1 due to an assumption that if the object is alive after this collection it has chances to be alive after the next collection also.

      When the object is created it moves in Generation 0 but if the size of the object is larger than 85,000 bytes then it directly moves into Generation 2 due to an assumption that the larger objects are more likely to live longer.

    Hope it helps to understand the basics of Garbage Collection

 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: