Island of Isolation in Java with Example

Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java

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

The Island of Isolation in Java

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

It refers to the object that is not reachable by any thread. Thus, placed in isolation without interaction with any process of an application. Then, the garbage collector notices those objects, and they pass the criteria for removal of those objects. If the objects contain references to each other, then only they are eligible for the garbage collector.

When Object 1 references Object 2 and Object 2 references Object 1, but neither Object 1 nor Object 2 is referenced by any 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? 

Example –

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 destroying an object, the Garbage Collector calls the finalize method for maximum one number of times.

Let’s revise the GC Algorithm in Java

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

The total of what we have is just internal references (which are in instance variable I of class Test) to them of each other. It is extremely unlikely we can call the 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.

Hope you like our explanation.

Conclusion

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

Related Topic: Java Regular Expression / Java Regex

For reference

If you are Happy with DataFlair, do not forget to make us happy with your positive feedback 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 *