Garbage Collector In Java 7 New Concept

In this article we discuss the Garbage Collector in Java 7.

Introduction

 
In this article, we discuss the Garbage Collector in Java 7.
 

What is garbage?

 
Garbage is an object, that is in a dead state, in other words, am object that has no use. Simply put, the object is unreachable. In Java, a technique known as the Garbage Collector processes that type of objects (in other words garbage value) that are in a dead state.
 

Garbage collection

 
Garbage collection is also known as automatic memory management. In Java garbage collection is performed by the Garbage Collector, that manages memory that will definitely never be used again.
 
Garbage collection was invented in the 1950s. It was first invented by John McCarthy in 1958 as part of the implementation of the Lisp language. The Garbage Collector (sometimes known as GC) reclaims garbage or memory taken by an object that has not been used in a program for a long time. 
Now let's see a figure to fully understand garbage collection.
fig-1.jpg
In this figure there are two heaps, the first heap (heap1) contains "(a=3 & b=4)" & the second heap (heap2) contains "(a=1 & b=2)". As you can see in the figure, in heap2 a reference variable is assigned to that; in other words S1, but in heap1 no reference variable is assigned to those objects. So this object becomes garbage collected.
 

How garbage collection is performed

 
Garbage collection occurs when one of the following occurs:
  1. passing null reference
  2. assign the reference of one to another.
  3. using an anonymous object
1. Bypassing null reference
 
The following is an example of passing a null reference:
  1. class GarbageEx {  
  2.  public static void main(String args[]) {  
  3.   GarbageEx ge1 = new GarbageEx();  
  4.   ge1 = null;  
  5.  }  
  6. }   
Output
 
fig-2.jpg
 
2. By assigning the reference of one to another
 
The following is an example of assigning the reference of one to another (the output is the same as the previous example):
  1. class GarbageEx {  
  2.  public static void main(String args[]) {  
  3.   GarbageEx ge1 = new GarbageEx();  
  4.   GarbageEx ge1 = new GarbageEx();  
  5.   ge1 = null;  
  6.   ge1 = null;  
  7.  }  
  8. }  

3. Using an anonymous object

 
The following is an example of using an anonymous object: 
 
new GarbageEx();
 
Some advantages
 
Some advantages of that are:
  1. It allocates objects on the heap effectively.
  2. It provides a way to make applications that have no free memory.
  3. Clear the memory taken by the object that has not been used for a long time and free the memory for future allocation.
  4. It provides memory safety since it ensures that no one object can acquire the content/property of another object.
Now for our topic "Garbage Collector in Java 7".
 
In Java 7 a new Garbage Collector has been introduced. It is called G1, that is the short form of Garbage First.
 

Advantages

 
Garbage First has the following advantages:
  • Garbage first uses parallelism that is widely used in hardware today. The main advantage of Garbage First is that it is designed in such a way as to make use of all available CPUs and utilize the processing power of all CPUs and increase the performance and speed of Garbage Collection.
  • Garbage First achieves high performance and pauses time goals through several techniques.
  • The concurrency feature of Garbage First allows Java threads to minimize the heap operations at stop pauses.
  • The next feature that plays a key role in increasing Garbage Collection is the treating of young objects (newly created) and the old objects (that have lived for some time) differently. Garbage First mainly focuses on young objects since they can be reclaimable when traversing the old objects.
     
    Heap compaction is done to eliminate fragmentation problems.
  • Garbage can be more predictable when compared to CMS.

Features

 
Some features of the Garbage Collector are:
  • A single heap is split into regions of the same size. There is no separation between older and younger regions.
  • Garbage First uses evacuation pauses. Evacuation pauses happen in parallel using all the available processors.
  • Garbage First uses a pause prediction model to meet user-defined pause time targets.
  • Like CMS, Garbage First also periodically performs a concurrent marking phase.
  • Unlike CMS, Garbage First does not perform a concurrent sweeping phase.
Example
 
In this example, we describe how memory management works in garbage collection.
  1. public class GarbageCollectionEx {  
  2.  public static void main(String[] args) {  
  3.   // define our code logic here  
  4.   Runtime runtm = Runtime.getRuntime();  
  5.   long m1, m2;  
  6.   Integer[] someint = new Integer[1630];  
  7.   System.out.println("Overall memory is: " + runtm.totalMemory());  
  8.   m1 = runtm.freeMemory();  
  9.   System.out.println("free memory is: " + m1);  
  10.   runtm.gc();  
  11.   m2 = runtm.freeMemory();  
  12.   System.out.println("Free memory after garbage collection: " + m2);  
  13.   System.out.println("----------------------------------------------------");  
  14.   System.out.println("After inserting some variables in the memory........");  
  15.   for (int k = 0; k < 1630; k++)  
  16.    someint[k] = k * 999999;  
  17.   m1 = runtm.freeMemory();  
  18.   System.out.println("Intitial free memory is: " + m1);  
  19.   runtm.gc();  
  20.   m2 = runtm.freeMemory();  
  21.   System.out.println("Free memory after garbage collection: " + m2);  
  22.   System.out.println("----------------------------------------------------");  
  23.   System.out.println("After UNLOADING variables from the memory........");  
  24.   for (int k = 0; k < 1630; k++)  
  25.    someint[k] = null;  
  26.   m1 = runtm.freeMemory();  
  27.   System.out.println("Intitial free memory is: " + m1);  
  28.   runtm.gc();  
  29.   m2 = runtm.freeMemory();  
  30.   System.out.println("Free memory after garbage collection: " + m2);  
  31.  }  
  32. }  
Output
 
fig-3.jpg