Island of Isolation in Java with Example

Free Java courses with 37 real-time projects - Learn Java

1. Objective

In our last tutorial, we discussed Java Generics. Now, in this Java Island of Isolation tutorial, we are going to learn about what is an island of isolation in Java, 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 with Example

The Island of Isolation in Java with Example

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 in Java 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.

Do you know What is 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

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

The reason finalize method called two times in the 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
Java Island of Isolation

Java Island of Isolation – t2i = t1

Related Topic- Decision Making in Java Programming 

  • t1 = null
Java Island of Isolation - t1 = null

Java Island of Isolation – t1 = null

  • t2 = null
Java Island of Isolation - t2 = null

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 use 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

You give me 15 seconds I promise you best tutorials
Please share your happy experience on Google

follow dataflair on YouTube

1 Response

  1. Poonam says:

    What is the solution of the island of isolation

Leave a Reply

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