Garbage Collection
Advantage of Garbage Collector
-
Allow us to develop an application without having worry to free memory.
-
Allocates memory for objects efficiently on the managed heap.
-
-Reclaims the memory for no longer used objects and keeps the free memory for future allocations.
-
Provides memory safety by making sure that an object cannot use the content of another object.
Memory Allocation in Managed Heap
The managed heap is a series of allocated memory segments (approx 16Mb
in size each) to store and manage objects. The memory for newly created
object is allocated at the next available location on the managed heap.
If there is available free memory, the garbage collector doesn't search
the dead objects for memory reclaim and memory allocations has been
done very fast. If the memory is insufficient to create the object, the
garbage collector search the dead objects for memory reclaim for the
newly object.An object is created using the new operator. This operator first makes sure that the bytes required by the new object fit in the reserved region (committing storage if necessary). If the object fits, NextObjPtr points to the object in the heap and object's constructor is called and the new operator returns the address of the object.
Impotants points
- All objects in the heap are allocated from one contiguous range of
memory address and heap is divided into generations so that it is easy
to eliminate the garbage objects by looking at only a small fraction of
the heap.
- Gen 0 and Gen 1 occupy a single segment known as the ephemeral
segment. Gen 2 is a set of further segments and the large object heap
is yet another group of segments.
-
Almost, all objects with-in a generation are of the same age.
- The newest objects are created at higher memory address while
oldest memory objects are at lowest memory address with in the heap.
-
The allocation pointer for the new objects marks the boundary between the allocated and free memory.
- Periodically the heap is compacted by removing the dead objects
and sliding up the live objects towards the lower memory address end of
the heap as shown in above fig.
-
The order of objects (after memory reclaims) in memory remains the same as they were created.
-
There are never any gaps among the objects in the heap.
- Only some of the free memory is committed when required and more
memory is acquired from the OS in the reserved address range.
Generations in Managed Heap
The managed heap is organized into three generations so that it can
handle short lived and long lived objects efficiently. Garbage
collector first reclaim the short lived objects that occupy a small
part of the heap.-
Generation 0
This is the youngest generation and contains the newly created objects. Generation 0 has short-lived objects and collected frequently. The objects that survive the Generation 0 are promoted to Generation 1.
Example : A temporary object.
-
Generation 1
This generation contains the longer lived objects that are promoted from generation 0. The objects that survive the Generation 1 are promoted to Generationen 2. Basically this generation serves as a buffer between short-lived objects and longest-lived objects.
-
Generation 2
This generation contains the longest lived objects that are promoted from generation 1 and collected infrequently.
Example : An object at application level that contains static data which is available for the duration of the process.
Garbage Collector Working Phase
-
Marking Phase
In this phase garbage collector finds and creates a list of all live objects.
-
Relocating Phase
In this phase garbage collector updates the references to the objects that will be compacted.
-
Compacting Phase
In this phase garbage collector reclaims the memory occupied by the dead objects and compacts the surviving objects. The compacting phase moves the surviving objects toward the older end of the memory segment.
No comments:
Post a Comment