Java Garbage Collection

In Java, unreferenced objects are treated like garbage. The process of reclaiming the unused memory during runtime automatically is known as the Java Garbage Collection.In other words, the Java Garbage Collection is used to destroy the reference less or unused objects. In this section, we are going to discuss what is garbage collection in Java.

What is Java Garbage Collection?

  • Java garbage collection is a feature used in Java that releases the runtime unused memory by destroying the unreferenced objects.
  • To achieve the same in C, one has to use the free() function, and the delete() function is used in C++.
  • But in Java, the garbage collector runs automatically while the program is in execution.
  • The garbage collector resides inside the JVM (Java Virtual Machine).

However, in Java, the garbage collection is done automatically. Hence, Java has better memory management as compared to C and C++.

Why Java Garbage Collection is required?

In languages like C++, it is the responsibility of the programmer to create as well destruct the objects. Many times, it is observed that the programmer created the objects but forgot to destruct the objects. Because of this negligence, at the specific point of time, there will be a shortage of memory for the creation of more objects, and this may lead to abnormal termination of the program because of OutOfMemoryErrors. Hence, to get rid of such errors, Garbage Collection is introduced in Java, that works automatically and does the destruction of unreachable or reference less objects.

The heap memory has two types of objects as follows:

  1. Live object:An object that is live in heap memory and referencing to any objectis known as live object.
  2. Dead object:An object that is no longer assigned anywhere is known as dead object.

How objects are dereferenced?

We can use the following ways to make an unused object valid for garbage collection:

  1. By making a reference null

Consider the example, a class named Employee is declared having an object named emp.

The second line of code makes the object emp be dereferenced.

  • Assigning an old reference to a new reference:

          Consider the example, a class named Employee is declared having two objects named emp1 and emp2.

In the above code snippet,the last statement reassigned the object emp2 to emp1. It means thatthe abject emp1 can be removed from the memory as it does not have any new reference.

  • With the use of anonymous objects

Consider the example, a class named ‘Employee’ is declared having an object named emp.

There is no name to this object therefore it is valid for garbage collection.

How does the Java Garbage Collector work?

The Java garbage collector runs inside the JVM (Java Virtual Machine). Each JVM has its own version of the garbage collector. The garbage collector must satisfy the standard JVM specifications of working with the object stored in the memory heap, identifying the unreachable objects, destroying them, and compacting the memory once again.

Oracle provides a JVM named HotSpot. It has a powerful and full-fledged garbage collection option. It follows the same procedure as followed by other garbage collectors.It identifies the unreferenced objects and marks those objects for garbage collection.After that, it removes them and at last performs defragmentation on the heap memory. So that remaining objects are compacted in a contiguous memory space at the start of the memory heap.

Java Garbage Collection

The memory heap is divided into three sections:

  1. Young Generation:

The newly created objects are of the young generation. It is divided into two parts i.e. Eden and Survivor Space. In Eden, the newly generated objects are stored whilethe survivor spaces S0 and S1 have those objects that are gone through the first garbage collection cycle. The garbage collection performed in this generation is a minor garbage collection event.

  • Old Generation:

The objects that are no longer used in the program are shifted to the old generation from the young generation. These objects’ removal is called a major garbage collection event.

  • Permanent Generation:

Metadata that consists of different classes and methods is considered as a permanent generation. Removing the unused classes results in permanent garbage collection.

Advantages of Java Garbage Collection

  1. As we know programming languages like C and C++ require handling memory management manually, it becomes hectic. But Java does perform this functionality automatically with the help of JVM.  So, the programmer need not to take care of the memorymanagement.
  2. If the program contains dereferenced memory objects it may lead to memory exhaustion over a longer period of time.
  3. Garbage collection reduces certain bugs like dangling pointer bugs, memory leaks, and double-free bugs.

Disadvantages of Java Garbage Collection

  1. The garbage collector stops the execution of an entire program in order to search and collect the garbage objects. It may cause longer pauses in the case of large programs and sometimes the user may notice the same.
  2. We never know when and how long the garbage collector will run to perform its task. It might impact the performance of the software.

In this section, we have discussed about Java garbage collection and its working. In the coming section we discuss about Java gc().

Pin It on Pinterest

Share This