Island of Isolation in Java with Example


1. Objective

In this Java Island of Isolation tutorial, we are going to learn about what is an island of isolation in Java Programming language, this is a subpart to Java garbage collection. Moreover, we will discuss how to make an object eligible for Garbage collection.

So, let us start the Island of Isolation in Java.

Island of Isolation in Java

Introduction to the Island of Isolation in Java

2. The Island of Isolation in Java

In Java, object destruction is taken care by the Garbage Collector module and the objects which don’t have any references to them are qualified for garbage gathering. Java garbage collector is a specialist, proficient to distinguish this sort of objects.

The Island of Isolation in Java:

  • When object 1 references Object 2 and Object 2 references Object 1, but neither Object 1 nor Object 2 is referenced by some other object than that is an island of confinement.
  • Essentially, an island of isolation is a gathering of items that reference each other however they are not referenced by any dynamic object in the application. Entirely, even a solitary unreferenced object is an island of isolation as well.

Follow this link, to know more about Java Garbage Collection 

3. Example of Java Island of Isolation

public class Test
{
    Test i;
    public staticvoid main(String[] args) 
    {
        Test t1 = new Test();
        Test t2 = new Test();
        t1.i = t2;
        t2.i = t1;
        t1 = null;
        t2 = null;
        System.gc();
    }
    @Override
    protected void finalize() throws Throwable
    {
        System.out.println("Finalize method called");
    }
}

Explanation –

Before destructing an object, Garbage Collector calls the finalize method for maximum one number of times.

Let’s revise GC Algorithm in Java

The reason finalize method called two times in above case since two items are qualified for garbage collection. This is on the grounds that we don’t have any outside references to t1 and t2 questions in the wake of executing t2=null.

The sum total of what we have is just internal references (which is in instance variable I of class Test) to them of each other. It is extremely unlikely we can call occurrence variable of the two articles. In this way, none of the items can be called once more.

  • t2i = t1
Island of Isolation in Java

Java Island of Isolation – t2i = t1

Related Topic- Decision Making in Java Programming 

  • t1 = null
Island of Isolation in Java

Java Island of Isolation – t1 = null

  • t2 = null
Island of Isolation in Java

Java Island of Isolation – t2 = null

Now, that both objects are eligible for garbage collection, we call it the Island of Isolation in Java.

This was all the Island of Isolation in Java Tutorial. Hope you like our explanation.

4. Conclusion

Hence, in this tutorial, we learned what is Island of isolation in Java, how it is used in Garbage collection for Java. In conclusion, we got the answer of how to make an object eligible for Garbage collection. Furthermore, if you have any query feel free to ask in the comment section.

Related Topic- Java Regular Expression / Java Regex

For reference