Java Garbage Collection
In java, garbage means unreferenced objects.
Garbage Collection is process of reclaiming the runtime unused memory automatically. In
other words, it is a way to destroy the unused objects.
To do so, we were using free() function in C language and delete() in C++. But, in java it is
performed automatically. So, java provides better memory management.
Advantage of Garbage Collection
o It makes java memory efficient because garbage collector removes the
unreferenced objects from heap memory.
o It is automatically done by the garbage collector(a part of JVM) so we don't need
to make extra efforts.
How can an object be unreferenced?
There are many ways:
o By nulling the reference
o By assigning a reference to another
o 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. e1=e2;//now the first object referred by e1 is available for garbage collection
3) By annonymous object:
1. new Employee();
The Algorithm
The garbage collection algorithm performs the following two basic tasks:
It first checks whether the objects is eligible or not to be garbage collected.
Once the object is identified, the garbage collector reclaims the heap space used by
the object and makes the space available back to the program.
Detection of the eligible object is done by defining a set of roots and tracing the reachability
from the roots. An object is said to be reachable if there exists a path of reference from the
roots using which the executing program can access the object. These roots once defined
are accessible to the executing program. An object which is reachable from any of these
roots is considered to be "live." Objects that are not reachable by the defined roots are
considered to be garbage, as they cannot put any impact on the execution of the program
further. In addition to this, an object which is referred by a live object is also considered as
reachable. These reachable objects are accessible by the executing program; hence, these
objects should remain on the heap as long as they are required. An object which is not
reachable can be garbage collected since there is no way a program can access it.
So the available System.gc() method can be used to request garbage collection. And the
System.runFinalization() method is used to run finalizers for all the eligible objects for GC.
Listing 1: The sample describes the syntax for calling garbage collection and finalizer
static void gc() // Requests GC to run
static void runFinalization() // Requests finalizers to run
Listing 2: Sample program performs garbage collection. The free memory before and after
garbage collection is returned and displayed.
import java.util.*;
class GarbageCollectionDemo
{
public static void main(String s[]) throws Exception
{
// Get runtime environment
Runtime rt = Runtime.getRuntime();
System.out.println("Free memory before GarbageCollection =+rt.freeMemory());
// Apply garbage collection
rt.gc();
System.out.println("Free memory after Garbage Collection = +rt.freeMemory());
}
}
The following diagram describes the garbage collection model in java.
Types of Garbage Collector
Since J2SE 5.0, we have following types of Garbage collectors. Programmers can normally
choose any of these to reclaim the unused memory using the JVM Parameters. These are
mentioned as under:
The table below describes different types of garbage collectors
Garbage
JVM Option Parameter Other Options
Collector Type
Serial Collector XX:+UseSerialGC
Young Generation GC are done in
Throughput
XX:+UseParallelGC parallel threads Tenured Generation GC
Collector
are done in Serial threads
Parallel Old Some Old Generation GC can be done in
Generation XX:+UseParallelOldGC parallel threads, which enhances the
Collector performance
Concurrent Low Xincgc or Used to collect the tenured generation
Pause Collector XX:+UseConcMarkSweepGC Cocnurrently.
Incremental Low
XX:+UseTrainGC Not supported after JSE 1.4.2
Pause Collector