working of garbage collector in java.....!!

Well in this post i will give u brief introduction and working of garbage collector....!!





Garbage collector:-in java garbage means derefrenced or unrefrenced object.it is the process to reclaim the unused memory automatically.In words it is the way to destroy unused objects.

To do so we are using free( )in c and delete( ) in cpp. But in java it is provided automatically so it gives a better concept to destroy unused or derefrenced object to be destroyed automatically.

Advantages of garbage collector:-

 It makes memory efficient as it destroys the members which are consumed in the object and unused which and are stored in heap is done by the JVM(it is the part of jvm) which makes it run at several time intervals.

Now generally how could the object get derefrenced .....!!!

Generally the basic concept behind the derefrenced object is that the object gets derefrenced when it's scope gets over generally when the program ends its scope would get over definately but if we talk about the local body.

for eg                               let us think to create the object in a for loop .
here actually what happens if for first condition the for loop gets executed there we have wriiten the statement for the creation of the object so if the control enters the for loop for the first time an object is created then the loop gets over the refrence correspondingly gets destroyed.again for the second condition new object is created and after the next condition to be checked for the creation of object the previous one's refrence from the stack gets destroyed..

I would take this eg at the end of this post

Now what are the other techniques by which we could derefrence the refrence or destroy the refrence..!!

1) By nulling the refrence

     for e.g. employee e = new employee( );
                  e=null;
2) By assigning refrence to other

      its simple.....

      lets take an example

      employee k1 = new employee();
      employee k2 = new employee();
       k1=k2;


so here the refrence of object k1 is lost.

There are other methods too.. but i am just saying few methods so as knowledge could be gathered that how can an object be derefrenced ......!!

Now a brief about gc that is garbage collector..

garbage collector is a program which is automatically runned by JVM at certain interval of time to clean up the memory which is occupied by the mess that is the object contains several variables of different data types so acutually what happens when we create an object in javais that the members  of the object occupy the memory in virtual memory called heap after the allocation of the members the refrence is stored in the stack.

so at the time of derefrencing only the refrence of the object from the stack gets destroyed not the members allocated in the heap gets destroyed so if the object gets derefrenced there is no usage of that object later on in the program but the members of that object still occupies the memory in the heap.

so later on many derefrenced objects gets collected  and the members in large amount occupy the memory..in the heap later on errors might occured of the types that cant run the program due to inaccessable memory or insufficent memory space .

The garbage collector is the program which is run by by jvm at certain interval of time which would destroy all the members related to that object from the heap and frees the memory.

As we talked earlier about that for loop example
lets take practically

for eg we say that we have taken class A

so in that body declare a for loop

for(i=0;i<3;i++)
{
   A o1=new A( );
}

now lets understand how it works 


let us assume that we have two variables in class as b and c now for condition for the first time control will enter in the loop first time and the members will occupy in the heap that are b and c respectively now their refrence are stored in the stack say it is 1000 now again as soon as the object is created now again for i=1 again new object will be created but the refrence of the old one is gone this time now say this object has refrence 1004 again for i=2 an object is craeted but this time the object that is the previous one is derefrenced that is the 1004 and say the new one with refrence for i=2 is 1006.

so at last only the object with the refrence 1006 would be alive but all the members from the previous one would be intact in the stack..

so to destroy this occupied memory in heap garbage collector runs and sees that which object are derefrenced..............?????????than removes the members of that object from the
heap and updates the pointer as in the heap this object are stored top to bottom so the empty space created by the members which are destroyed, this member of the alive object should be brought there.. and also updates the refrence and assignes the new refrence to it as same is the case in the stack from bottom to top the bottom one contains 1000 then 1004 than 1006 so 1006 is at the top there so it comes down to the stack and gets the refrence 1000. that is the new refrence.


                         This is how the garbage collector runs internally 




Comments

Popular posts from this blog

State space search / blocks world problem by heuristic approach /TIC TAC TOE

Navigation in Vaadin.

Drag and drop items from one Grid to another