Thursday, 4 July 2013

Garbage Collection usage and How it works

Garbage collection is the process of allocate memory for the object and release the memory used by the objects, Which is no longer referenced or no longer used.

normally in c++ when we allocate a memory for object , then we have to dealloc the memory There is no automatic process of deallocation of unused objects .

In dotnet we have a automatic process of memory deallocation of unused objects using Garbage collection.

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 in the managed heap. 

Whenever we create a new object it will allocate the memory in Managed heap.
2   What is Managed ? When object memory allocation and deallocation is handled by CLR is   
      called Managed.

When and how does a collection occur?
A collection occurs
· When you allocate a new object and the generation 0 budget is reached, 
i.e. if the new object would cause it to go over budget.· 
When someone calls GC.Collect (Induced GC)· 
Based on memory pressure

How it allocates the memory for the objects?
Garbage Collection have three buckets , Gen0, Gen1, Gen2




Whenever we create a new object it is placed in Gen0 bucket for example

What are generations and why do we use a generational GC?
In a generational GC objects are created in Gen 0 and if they are still alive when a collection happens they get promoted to Gen 1. If they are still alive when a Gen 1 collection happens they are promoted to Gen 2 etc. until they finally end up in their final resting place in the highest generation.

The idea behind a generational GC is that most objects are very temporary, like locals, parameters etc. i.e. they go out of scope while in generation 1. If we can keep collecting just these objects without having to go through all the memory we will save a lot of time and CPU power when cleaning up the objects.
The longer an object has been alive, the more likely it is that the object will be around for a very long time. Think about it, most objects that survive a couple of collections are objects that are stored in cache, session scope or in other long term storage like static variables. If we know that this is the case then we don’t have to bother constantly searching through them all.
In the .net GC there are 3 generations (0, 1, and 2) and then there are the large objects (objects over 85000) that end up in a separate segment. The LOH objects are different in the sense that even if they survive a collection they are not promoted since they exist outside of Gen 0, 1, and 2.

There are a few other benefits to a generational garbage collector because of how allocations are done.

Large and small objects

.NET handles large objects and small objects differently:
  • Large objects are all objects over 85k, and double (multidimensional) arrays over 8k.
  • All other objects are considered small objects.

The small object heaps

Small objects are stored in the small object heaps.
Objects are allocated first to the generation 0 (Gen 0) heap.
When the Gen 0 heap is full, the .NET garbage collector runs. The garbage collector performs two tasks:
  1. It disposes of all objects on the Gen 0 heap that are no longer needed.
  2. It moves objects that are still needed to the generation 1 (Gen 1) heap.
This leaves the Gen 0 heap empty, ready for new objects to be allocated on it.
Eventually, the Gen 1 heap becomes full. When this happens, the garbage collector runs again, and it performs three tasks:
  1. It disposes of all objects on the Gen 1 heap that are no longer needed.
  2. It moves objects that are still needed to the generation 2 (Gen 2) heap.
  3. It performs a garbage collection on Gen 0, as before.
If Gen 2 becomes full, a full garbage collection takes place. When this happens, the garbage collector performs four tasks:
  1. It disposes of all objects on the Gen 2 heap that are no longer needed.
  2. It performs a garbage collection on Gen 1, as before.
  3. It performs a garbage collection on Gen 0, as before.
  4. It disposes of objects on the large object heap that are no longer needed (see below).
If insufficient memory has been freed at the end of a full garbage collection, an OutOfMemory exception is thrown.
Note that whenever the garbage collector runs on any of the small object heaps, the heaps are compacted, so that the available space is used efficiently.

The large object heap

The large object heap is where large objects are stored.
There is only one large object heap, and objects stored on it are only tidied-up during a full garbage collection.
Importantly, because compacting the large object heap would adversely affect your application's performance, the large object heap is never compacted. This means that the heap can fragment over time. If a new object, which is bigger than an existing fragment, needs to be allocated on the heap, the large object heap will grow so that the new object can be allocated to the end of the heap. Eventually, the fragmentation may mean that there is insufficient memory to allocate new large objects.

No comments:

Post a Comment