Garbage Collection in Java – Requesting JVM to Run Garbage Collector

1. Java GC Collect – Objective

In our last session, we talked about the Wrapper Class in Java. Here, we are going to learn about what is garbage collection in Java, methods used in Java garbage collection, important terms, object eligibility. Moreover, we will discuss how Java force garbage collection on an object, and different ways of requesting JVM to run.

So, let us start Garbage Collection in Java (Java GC Collect).

Garbage Collection in Java - Java GC Collect

Garbage Collection in Java – Java GC Collect

2. What is Garbage Collection in Java?

All the objects which are created during a program which are created dynamically are allocated in the memory heap of the software. A programmer is responsible for both creation and deletion of objects in the program, but the programmer usually ignores the latter, which creates a problem called OutOfMemoryErrors due to insufficient memory which is created because of non-deletion of unwanted objects.

Do you know What is Abstract Class in Java?

In Java, the programmer need not worry about this problem as the garbage collection system always runs in the background, the main aim to free the memory heap by deleting unreachable objects.

3. Important Terms – Garbage Collection in Java

This two Important terms used in Garbage Collection in Java or Java GC Collect.

a. Unreachable Objects

An object is considered as unreachable when it doesn’t contain any reference to it. Also, the objects which fall within the Island of Isolation are considered as unreachable objects.

Integer i = new Integer(4);
// the new Integer object is reachable  via the reference in 'i'
i = null;
// the Integer object is no longer reachable.

b. Eligibility for Garbage Collection

An object considers as eligible for garbage collection in java when it is unreachable, in the image above object 4 is eligible for Java garbage collection.

4. Object Eligibility

Different ways to know Object Eligible for Garbage Collection in Java. An object should made unreachable even Java has a garbage collection.

There are generally four types of ways to make an object eligible for garbage collection in Java –

  • Nullifying the reference variable
  • Island of isolation
  • Re-assigning the reference variable
  • An object created inside a class
Java Quiz

5. Ways for requesting JVM to run Garbage Collector

Here, are the ways for requesting JVM to run Java Garbage Collector.

a. If we make an object eligible for Java Garbage Collection, it may or may not be eligible for JVM to destroy.

Follow this link to know about Java Virtual Machine (JVM)

b. Two ways to request for Garbage collection in Java are –

  • Using System.gc() method
  • Using Runtime.getRuntime().gc() method
public class Test
  {
      public static void main(String[] args) throws InterruptedException
         {
            Test t1 = new Test();
            Test t2 = new Test();
            System.gc();       
            t2 = null;
            Runtime.getRuntime().gc();
        }
@Override
    protected void finalize() throws Throwable
      {
          System.out.println("Garbage collector ");
          System.out.println(" collected garbage object: " + this);
      }
 }

6. Conclusion

Hence, in this Java tutorial, we learned about the garbage collection in Java and how Java Garbage Collection works. In the next tutorial, we will study about Java Garbage collection algorithm.

Read – Linear Data Structures in Java

For reference

Leave a Reply

Your email address will not be published. Required fields are marked *