Garbage Collection
Chapter
4
Garbage Collection One of the biggest challenge before application developer is allocation and deallocation of memory resources. If it was not properly done, leads to memory leakage and result are unpredictable. Java has built-in facility to re-claim unwanted memory; this is known as garbage collection. Garbage collector plays its role if there is any memory deficiency. Usually an object no longer referenced, for example when you assign with null, it could be ready for garbage collection. In java all objects are created in heap memory, whereas primitive type and object reference are place in stack memory. Object class has defined finalize() method that is called by the garbage collector on an object when garbage collection determines that there are no more references to the object. The below is program example. class MyGC{ public static void main(String args[]){ MyObject mo1 = new MyObject(); mo1=null; System.gc(); } } class MyObject{ public MyObject(){ }
System.out.println("Object created");
protected void finalize(){ System.out.println("Garbage collector in action");
} }
tURBOPLUS