Saturday 3 September 2011

Basic Stuff that all java programmer should know

All Objects resides in heap in JAVA.

Animal a;

here we have created a reference of class Animal. 'a' is reference which contains the address of object that it points to. Since currently 'a' is not pointing to any object ,'a' contains the null pointer.


a=new Animal();

now reference 'a' will point to object of animal created on heap.

So When does the object become eligible for garbage collection. Garbage collection is memory management technique used by JVM. Where there is no reference to object, that object becomes eligible for GC. We can request JVM to run GC  .

1.System.gc();
 2.Runtime r = Runtime.getRuntime();     r.gc();

when we make a=null; .the object 'a' is referring will have no reference  . So that object  becomes eligible for GC. Remember We can request JVM to run GC but cannot force.
The questions asked in scjp are like how many object will become eligible for gc given some code. So understanding this topic is very important. If you have any query you can contact to milan.ashara@yahoo.com.


No comments:

Post a Comment