Friday, 16 January 2015

Garbage Collection in java


Garbage Collection:

In java, garbage means unreferenced objects.
  • It makes java memory efficient because garbage collector removes the unreferenced objects from heap memory.
  • It is automatically done by the garbage collector so we don't need to make extra efforts.

How can an object be unreferenced?

There are many ways:
  • By nulling the reference
  • By assigning a reference to another
  • By annonymous object etc.

1) By nulling a reference:

  1. Employee e=new Employee();  
  2. e=null;  

2) By assigning a reference to another:

  1. Employee e1=new Employee();  
  2. Employee e2=new Employee();  
  3.  
  4. e1=e2;//now the first object referred by e1 is available for garbage collection  

3) By annonymous object:

  1. new Employee();

finalize() method:

The finalize() method is invoked each time before the object is garbage collected. This method can be used to perform cleanup processing. This method is defined in System class as:
  1. protected void finalize(){}

The Garbage collector of JVM collects only those objects that are created by new keyword.

gc() method:

The gc() method is used to invoke the garbage collector to perform cleanup processing. The gc() is found in System and Runtime classes.
  1. public static void gc(){}

Garbage collection is performed by a daemon thread called Garbage Collector(GC). This thread calls the finalize() method before object is garbage collected.

  1. class Simple{  
  2. public void finalize(){System.out.println("object is garbage collected");}
  3. public static void main(String args[]){  
  4.  Simple s1=new Simple();  
  5.  Simple s2=new Simple();  
  6.  s1=null;  
  7.  s2=null;  
  8.  System.gc();  
  9. }  
  10. }  
Output:object is garbage collected
      object is garbage collected

Note: Neither finalization nor garbage collection is guaranteed



In Java destruction of object from memory is done automatically by the JVM. When there is no reference to an object, then that object is assumed to be no longer needed and the memory occupied by the object are released. This technique is called Garbage Collection. This is accomplished by the JVM

Garbage collection runs in a low priority thread. It may kick in when memory is too low. . It's not possible to force garbage collection. Invoking

System.gc may start garbage collection process.

Note:
An object is only eligible for garbage collection, if the only references to the object are from other objects that are also eligible for garbage collection. That is, an object can become eligible for garbage collection even if there are references pointing to the object, as long as the objects with the references are also eligible for garbage collection.


finalize method
All objects have a finalize method. It is inherited from the Object class.
is used to release system resources other than memory

finalize is called only once for an object. If any exception is thrown in finalize, the object is still eligible for garbage collection (at the discretion of gc)
protected void finalize() throws Throwable { }


finalize can be called explicitly, but it does not garbage collect the object.

finalize can be overloaded, but only the method with original finalize signature will be called by gc.
System.runFinalization can be used to run the finalizers (which have not been executed before) for the objects eligible for garbage collection

Can the Garbage Collection be forced explicitly?
No, the Garbage Collection can not be forced explicitly. We may request JVM for garbage collection  by calling System.gc() method. But This does not guarantee that JVM will perform the garbage collection.

Some Important Points to Remember

  • finalize() method is defined in java.lang.Object class, therefore it is available to all the classes.
  • finalize() method is declare as protected inside Object class.
  • finalize()method gets called only once by GC threads.

public class Test
{
   public static void main(String[] args)
   {
       Test t = new Test();
       t=null;
       System.gc();
   }
   public void finalize()
   {
       System.out.println("Garbage Collected");
   }
}

No comments:

Post a Comment