Java Garbage Collection
· When an object is no longer referred to by any variables , Java automatically reclaims the memory used by that object. This is known as garbage collection.
· When we execute a software application it uses the memory in several different ways like used to create stack, a heap, in Java's case constant pools, and method areas. The heap is that part of memory where Java objects live, and it is the only part of the memory involved in the garbage collection process. So Garbage collection ensures that heap has as much free space as possible.
· When garbage collector runs its main purpose is to find and delete objects that cannot be reached.
· If we think of a Java program as being in a constant cycle of creating a new object when it needs, and then discarding them when they are no longer needed, creating objects,discarding them and so on. When Garbage Collector runs it look for for those discarded objects and deletes them from memory.
· The garbage collector is under the control of the JVM. JVM decides when to run the garbage collector.
· From within our Java program we can ask JVM to run the garbage collector , the JVM will grant the request but there is no guarantees. Normally JVM will run the garbage collector when it senses that memory is running low.
· Object is eligible for garbage collection when no live thread can access it. Means object that can't be reached by any live thread, it will consider that object is eligible for deletion. Reaching an object means having reachable reference variables referring to the object and that reference variable is used by live thread, then that object is considered reachable.
· There are many ways to make an object unreachable and make eligible for garbage collection.
· Nulling a Reference
Example to show how to make object eligible for garbage collection using nulling reference
public class GarbageSample {
public static void main(String[] args) {
// TODO Auto-generated method stub
StringBuffer sb = new StringBuffer("hello");
System.out.println(sb);
//The StringBuffer object not eligible for garbage collection
sb=null;
// Now StringBufferObject eligible for garbage collection.
}
}
· Reassigning a Reference Variable
Examine the following code how object made unreachable.
public class GarbageSample {
public static void main(String[] args) {
// TODO Auto-generated method stub
StringBuffer sb1 = new StringBuffer("hello");
StringBuffer sb2 = new StringBuffer("Java World");
System.out.println(sb1);
//At this point the StringBuffer "hello" not eligible for garbage collection
sb1=sb2;
// Redirects sb1 to refer to the"Java World" object.
// Now the StringBuffer "hello"is eligible for garbage collection.
}
}
· Isolating a Reference
Here object become eligible for garbage collection even if they still have valid references. We call this scenario “islands of isolation”
A simple example to explain what is islands of isolation : Consider a class that has a instance variable that is a reference variable to another instance of the same class. Now imagine two such instance exist and that they refer to each other. If all reference to these two objects are removed , then even through each object still had a valid reference , there will no way for live thread to access either object. When garbage collector runs, it can usually find such island of objects and remove them. Examine the following code
public class Island {
Island i;
public static void main(String args[]){
Island i2= new Island();
Island i3= new Island();
Island i4= new Island();
i2.i= i3; // i2 refers i3
i3.i = i4; //i3 refers i4
i4.i= i2; //i4 refers i2
i2=null;
i3=null;
i4= null;
//Here instance variables of objects refer to each other,
//but we can't access objects from outside world because it have //been nulled.
//some other code
}
}
· Forcing Garbage collection:
Java provide some methods that allow we to request that the JVM perform garbage collection. Garbage collection routines that Java provides are members of Runtime class. Runtime class is Singleton class. The Runtime object provides a mechanism to communicate with VM. To get Runtime object we can use the method
Runtime.getRuntime(). Once we have Runtime object we can invoke the garbage collector using the gc() method. We can call the gc() method on the System class. The simplest way to ask for garbage collection is
System.gc();