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.
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:
- passing null reference
- assign the reference of one to another.
- using an anonymous object
1. Bypassing null reference
The following is an example of passing a null reference:
- class GarbageEx {
- public static void main(String args[]) {
- GarbageEx ge1 = new GarbageEx();
- ge1 = null;
- }
- }
Output
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):
- class GarbageEx {
- public static void main(String args[]) {
- GarbageEx ge1 = new GarbageEx();
- GarbageEx ge1 = new GarbageEx();
- ge1 = null;
- ge1 = null;
- }
- }
3. Using an anonymous object
The following is an example of using an anonymous object:
new GarbageEx();
Some advantages
Some advantages of that are:
- It allocates objects on the heap effectively.
- It provides a way to make applications that have no free memory.
- Clear the memory taken by the object that has not been used for a long time and free the memory for future allocation.
- 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.
- public class GarbageCollectionEx {
- public static void main(String[] args) {
-
- Runtime runtm = Runtime.getRuntime();
- long m1, m2;
- Integer[] someint = new Integer[1630];
- System.out.println("Overall memory is: " + runtm.totalMemory());
- m1 = runtm.freeMemory();
- System.out.println("free memory is: " + m1);
- runtm.gc();
- m2 = runtm.freeMemory();
- System.out.println("Free memory after garbage collection: " + m2);
- System.out.println("----------------------------------------------------");
- System.out.println("After inserting some variables in the memory........");
- for (int k = 0; k < 1630; k++)
- someint[k] = k * 999999;
- m1 = runtm.freeMemory();
- System.out.println("Intitial free memory is: " + m1);
- runtm.gc();
- m2 = runtm.freeMemory();
- System.out.println("Free memory after garbage collection: " + m2);
- System.out.println("----------------------------------------------------");
- System.out.println("After UNLOADING variables from the memory........");
- for (int k = 0; k < 1630; k++)
- someint[k] = null;
- m1 = runtm.freeMemory();
- System.out.println("Intitial free memory is: " + m1);
- runtm.gc();
- m2 = runtm.freeMemory();
- System.out.println("Free memory after garbage collection: " + m2);
- }
- }
Output