25-12-2010
Allocating memory: When an object is created by the application, garbage collector will allocate memory for that object with in the managed heap where exactly next object pointer is providing.
According to above diagram managed heap occupied by objA, objB, objC, objD & objE
Objects: Let us assume an application is created a new object like below.
myclass objF = new myclass();
Here a new object is created that is objF.
Immediately garbage collector will allocate memory for that object with in the heap where exactly next object pointer is pointing like below.
In this way garbage collector will allocates memory for the objects.
Releasing memory (or) De-allocating memory:
When an object is not using by the application, since long time then garbage collector will recognize that object as unused & it will destroy that unused object & that destroyed released memory garbage collector will provide to newly created object
De-allocating memory process garbage collector is implementing in 2 steps:
Step-1: Recognizing unused object.
Step-2: Collection process.
Step-1: Recognizing unused object:
To recognize unused object garbage collector is depending on roots.
Root is a pointer (or) reference variable which are maintaining by Jit-compiler.
When an application is initialized on behalf of that application some collection of root variable will creating within the JIT-Compiler.
Root variable is acting as mediator between application & application created using object.
That means, Root variable will maintain the link between heap object & application at a particular time span if the object is not consuming by application, then the link between root variable & object of the heap will disconnected & concern root variable will be initialized as null.
When an object is not pointing by the root variable that particular object will be recognized as unused object (or) unreachable object.
Diagram for all used objects:
According to above diagram on behalf of application1, 3-root variables are created within the JIT complier, they are r1, r2, r3.
An be-half of application2 there are 3-root variables are created within the JIT-Compiler. They are r4, r5, r6.
Application1 is created 3-Objects. They are ObjA, ObjB, ObjC.
Application2 is created 3-Objects. They are objD, objE, objF.
Above discussion.
"If any object is not pointing by the root variable then garbage collector will recognize that object is unused-object."
As per above diagram.
All the objects are pointing by concern root variables due to that reasons we can say that all the above objects are used objects.
Diagram for used & unused objects:
As per above diagram,
ObjA, ObjC, ObjF objects are not pointing by root variables & concern root variables are initialized as null value due to that reason these objects are recognized as unused objects.
ObjB, ObjD, ObjF objects are pointing by some root variables, due to that reason we can say that these objects are used objects.
In this way, Garbage collector will recognize unused objects.