Garbage Collection

In Java, the programmer need not to care for all those objects which are no longer in use. Garbage collector destroys these objects.Main objective of Garbage Collector is to free heap memory by destroying unreachable objects.Garbage collector is best example of Daemon thread as it is always running in background.

Advantage of Garbage Collection
  • It makes java memory efficient because garbage collector removes the unreferenced objects from heap memory.
  • It is automatically done by the garbage collector(a part of JVM) so we don't need to make extra efforts.
As we have seen that the Garbage collector destroy the object which have no reference in the memory. So now we will the different ways to Unreferenced the object.

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

1) By nulling a reference:
Integer i = new Integer(4);
// the new Integer object is reachable  via the reference in 'i' 
i = null;
// the Integer object is no longer reachable. 

2) By assigning a reference to another:
Employee e1=new Employee();  
Employee e2=new Employee();  
e1=e2;//now the first object referred by e1 is available for garbage collection  

3) Anonymous Object
new Employee();

4)Isolation Island
Object 1 references Object 2 and Object 2 references Object 1. Neither Object 1 nor Object 2 is referenced by any other object. That’s an island of isolation. Basically, an island of isolation is a group of objects that reference each other but they are not referenced by any active object in the application. Strictly speaking, even a single unreferenced object is an island of isolation too.

Test t1 = new Test();
        Test t2 = new Test();
         
        // Object of t1 gets a copy of t2
        t1.i = t2;
     
        // Object of t2 gets a copy of t1
        t2.i = t1;
         
        // Till now no object eligible
        // for garbage collection 
        t1 = null;
        
        //now two objects are eligible for
        // garbage collection 
        t2 = null;
        
        // calling garbage collector
        System.gc();


Ways for requesting JVM to run Garbage Collector

Once we made object eligible for garbage collection, it may not destroy immediately by garbage collector. Whenever JVM runs Garbage Collector program, then only object will be destroyed. But when JVM runs Garbage Collector, we can not expect.We can also request JVM to run Garbage Collector. There are two ways to do it :

Using System.gc() method : System class contain static method gc() for requesting JVM to run Garbage Collector.

Using Runtime.getRuntime().gc() method : Runtime class allows the application to interface with the JVM in which the application is running. Hence by using its gc() method, we can request JVM to run Garbage Collector.

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 Object class as:

protected void finalize(){}


This covers the basic concepts of garbage collection in java . If you any question or want to add anything please comment.

Comments

  1. Very efficiently written information. It will be beneficial to anybody who utilizes it, including me. Keep up the good work. For sure i will check out more posts. This site seems to get a good amount of visitors. junk removal pittsburg ca

    ReplyDelete
  2. When your website or blog goes live for the first time, it is exciting. That is until you realize no one but you and your. junk removal pittsburg ca

    ReplyDelete

Post a Comment

Popular posts from this blog

Deploy standalone Spring MVC Application in Docker Container

Refactor Code : Separate Query from Modifier

HTTP : Must known Protocol (Part 1)