Popular Posts

Friday, February 1, 2013

GC Algorithm in Dot Net


The Memory management in the COM Based Architectures is Reference Counting. But In Case of .NET, It Differs entirely. It Is Reference Tracing.

Reference Counting:
Whenever an Object gets created in the Heap, an Reference count gets increased and When it gets deleted, the reference count of the object is decreased. It is the Developers responsibility 'to ensure the proper release of the allocated resources.

Reference Tracing:
In .NET Garbage Collector is responsible for memory management.

Let’s first we will look, how to object allocat in Heap:

There is two part of Heap's storage like:
1. Free space
2. Reserved Space

Whenever an Object gets allocated in the heap, the following Rules have to be followed.
1. Memory allocation takes place in a Continuous Range of the Free Space.
2. The order of objects in memory remains the order in which they were created, for good locality.
3. There are never any gaps between objects in the heap.
4. The Oldest Objects are in the lowest address.

GC Algorithm:

The Reference Tracing does not occur once the object goes out of scope. The GC starts its work] when the memory in the heap is full.

The GC checks for each and every object if it is "reachable".

To determine reach ability, the GC starts at all root objects - which are basically all static (shared in VB.NET) members and all in-scope local variables - and traverses the complete object reference graph. Each object which is encountered by the GC on its way is marked as alive.

Objects in the Heap will be in any of the two states:
LIVE
DEAD

In a second pass, all non-marked objects are destroyed, their resources freed, and finally the heap is compacted again to prevent memory fragmentation.

No comments:

Post a Comment