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:

Features

Some features of the Garbage Collector are:
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