Tuesday, August 5, 2008

Basics of Garbage Collection in Java

In a short, garbage collection is nothing but cleaning up the memory. So the questions that raises in your mind are...
  • What does it clean?
  • Why does it clean?
  • How does it clean?
  • Why should I know this?
Now we will try to anwser all your questions below....

In Java all the dynamic objects that you create are allocated in the heap. Now if I am in a method main() and I create some objects say m1,m2,m3 in the main method and I will call the method First(). Let us assume this First method creates 2 objects f1 and f2. Now the heap and stack looks some thing like below...



Now every method has its own stack space where it creates all static objects and rest of the dynamic objects are created in heap and a pointer is maintained to the root set.

Root set is the one which keep track of all the the first level objects. Now The method's dynamic variable say f1 points to the root set address. The adress in the root set points to the heap adress where actualy the memory is allocated by the jvm to that object. From the above picture you can also there is an object f3 which is created by f1. The object in the root set are called as first level objects.

Now the actual story beguns... When we finish the work in the method First() and return back to Main(). All the reference to the root set are removed and eventualy the reference link from the root set to the objects in the heap also will be removed. Now the first question here is how will clean the heap as we have left out with 3 objects that are no more required. Now some one has to clean them out! This is where JVMs Garbage collecter will come in to picture... This is the wonder full feature that C or C++ doen't have. In the case of C we have to clean up the memory, technically we have to deallocate the memory that we have created, if not memory will be leaked. (I will talk about memory leak clearly next). Anyway nowdays we are getting some efficient memory allocater in the market where if you use them, they will take care of this garbage collection. Ok let us come back to java..!!

Finaly in Java, the memory cleanup or GC is no more a developers headack. So many people say there is no memory leak in java as GC will take care of all the cleanup automaticaly. Defenatly this is not true... Let us see in my furture blog in more detail...

Now the question araises... How the GC will come to know what to cleanup or exactly is there any algorithim which is used by GC. The answer is yes, Let us have deeper look in my later posts. For now just think, That the main rule of Garbae collecter (From now I will just call as GC) is: "Remove all unreferenced objects".

So let us consider the example again that I mentioned above. So like the First() method, many more method are called and the return back and hence the heap usage grows as heap is left out with many unused objects. Now at some point of time the heap will get full or reaches some threshold, then this GC thread will become active and then starts cleaning up the heap. This is how it work... It starts with each and every object in the root set and try to reach the objects in the heap and marks all the objects that it can reach. In this case we can reach only m1 and m2. Then it parses from the begining of the heap to the end to see all unmarked objects, if it finds any it will remove them. Thats it !!! very simple...

But it is not as simple as said above, it has an big algo which I will explain later. This was just to give an idea how basically GC works.

So final Question: Why should I know this when JVM does all automatically?

The answer is Yes you have to know what it does. The main reason I always say is Please make sure all GC is working efficintly. What I meant was, make sure it is not affecting your response time or using more resources. All the can be done by JVM tuning or GC tuning or Heap tuning.

Hope this gave a basic picture of gabage collector.

Don;t Forget to write coments please... If concepts are not clear then send a mail directly to me.

No comments: