Friday, December 23, 2016

Garbage collection

Garbage Collection in Java

Definition:
Some object oriented languages requires that we keep track of all the objects we have created and that we destroy them explicitly when they are no longer needed. Managing memory explicitly is tedious and the Java platform allows us to create as many objects as we want (limited, of course, by what our system can handle), and we don't have to worry about destroying them, The Java environment deletes objects when it determines that they are no longer being used. This process is known as garbage collection.

An object is eligible for garbage collection when there are no more references to that object. References that are held in a variable are usually dropped when the variable goes out of scope. Or we can explicitly drop an object reference by setting the variable to the special value null. Remember that a program can have multiple references to the same object; all references to an object must be dropped before the object is eligible for garbage collection.

The Java runtime environment has a garbage collector that periodically frees the memory used by objects that are no longer referenced. The garbage collector does its job automatically when it determines that the time is right.

In java garbage collector is a daemon thread (we will discuss on Multithreading chapter) which runs on background. There is a method System.gc()which we can call to force runtime environment to perform garbage collection. But it does not guarantee that the program will run immediately.

Whenever an object is garbage collected, it does not mean that the object data is erased from memory; instead that area is just marked as available for others. Similar like when we format our hard drive data it is actually not erased from the disk; instead it will be marked as available for others.

Let’s take this example:

package com.dev21century;

public class GarbageDemo {

      int x=10;
      int y=20;

      static void show()
      {
            GarbageDemo gDemo2 = new GarbageDemo();
            display();
      }
     
      static void display(){
            GarbageDemo gDemo3 = new GarbageDemo();
            System.out.println("gDemo3.x = " + gDemo3.x);
            System.out.println("gDemo3.y = " + gDemo3.y);
      }

      public static void main(String args[])
      {
            GarbageDemo gDemo1 = new GarbageDemo();
show();
      }
}

Now, in above program, if we will remove display() method then gDemo3 object reference will be removed from stack memory and its corresponding object will be unreachable from heap memory.

There are several conditions where an object may become unreachable, lets study them:

Condition 1:
When an object is created other than main() method. Java’s program termination point is its main() method, when it ends up, all variable memories are freed.

Condition 2:
This is an explicit condition:

package com.dev21century;
public class GarbageDemo {
     
      public static void main(String args[])
      {
            GarbageDemo g1 = new GarbageDemo();//Line6
            GarbageDemo g2 = new GarbageDemo();//Line7
            g1 = g2;
      }
}

In above program as soon as the object reference of g2 is assigned to g1, both g1 and g2 started pointing to the object which was created at line number 7, which means the object created at line number 6 becomes unreachable and eligible for garbage collection.

Condition 3:

package com.dev21century;
public class GarbageDemo {
 int x = 10;
 int y = 20;
 public static void main(String args[])
 {
  GarbageDemo g1 = new GarbageDemo();//Line6
  System.out.println("x = " + g1.x + ", y = " + g1.y);
  g1 = null;
 }
}

As soon as g1 is assigned null, the object becomes unreachable and eligible for garbage collection.

Condition 4:

When we create an anonymous object, as soon as this object is used in the program, the object becomes eligible for garbage collection.





No comments:

Post a Comment