Island of Isolation in Java with Example

Get Job-ready: Java Course with 45+ 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

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

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

courses

DataFlair Team

DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.

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 *