The curious case of Island of Isolation

Arun Jijo
Javarevisited
Published in
4 min readJun 25, 2019
Island of Isolation

The garbage collector is one of the major primitives in the JAVA world. The tool that clears the unused/unreachable objects from the memory. An object is said to be eligible for Garbage Collection if it holds no references. Contrary to this assertion, there is a state where objects can be Garbage collected even if they hold a reference. Such a scenario is known as the Island of Isolation.

To put it in layman’s terms Island of Isolation is a scenario where some objects hold each other’s references, but none of them can be accessed through the active application as the objects have no external strong references.

The term “Island of Isolation” arises because the objects are isolated to hold only internal references.

Let’s have a good picture by diving into an example.

class  Demo{

String objName;
Demo demo;

public Demo(String objName){
this.objName=objName;
}

public static void main(String... args){

Demo obj1 = new Demo("Object 1");
Demo obj2 = new Demo("Object 2");

// Object obj1 gets a copy of obj2
obj1.demo = obj2;

// Object obj2 gets a copy of obj1
obj2.demo= obj1;

/* Both the objects holds the internal references of each
other at this point*/

// Make the Objects GC eligible by Nullifying the references
obj1 = null;

/* Change the name of the object obj1 via obj2, So that it
is easy to identify when finalize method is invoked*/
obj2.demo.objName = "Object 1 referenced via Object 2";

obj2 = null;

// Request GC to garbage collect the objects
System.gc();
}

@Override
/* Overriding finalize method to check which object is garbage
collected */
protected void finalize() throws Throwable
{
System.out.println(this.objName + " garbage collected
successfully");
}
}

For demonstration, we have a Demo class. As you observe that the Demo class has two instance variables, one is to hold the name of the object and the other to hold the Demo class instance. Two new instances of Demo is created and each of these instances is assigned with a copy of another Demo class instance. i.e. obj1 maintains obj2’s inner reference and obj2 maintains obj1’s inner reference. And we’ve overridden the finalize method in a way that it is easily possible to found out the name of the Garbage collected objects.

Obj1 has been nullified next, in case if you’re wondering obj1 is eligible for garbage collection, reassess it as obj2 holds the internal reference of obj1 and obj1 can still be accessed via obj2.

Finally, obj2 has also been nullified, since there are no more external references, there exists no way to access these object in the application. Both these objects are garbage collected which is evident by noticing that the finalize method has been called twice along with their corresponding object names.

This phenomenon of Garbage collecting the objects, even when an existing reference is present is termed as “Island of isolation”.

Conclusion:

As the article conveys, GC will not only clean non-referenced objects, but also clean objects which have an internal reference but no external reference. If you have any queries, feel free to reach me on Twitter.

Other Java Articles You may like
Java Memory Management
A Comprehensive Introduction to Java Virtual Machine (JVM)
Understanding the Java Virtual Machine: Memory Management
10 Articles Every Programmer Should Read
10 Tools Every Software Developer Should Know
10 Data Structure and Algorithms Courses for Programmers
5 Java and Web Development Frameworks Programmers Should Learn
10 Unit testing and Integration testing tools for Java devs
5 Courses to learn JVM Internals and Performance

Thanks for reading this article so far. If you like these tools then please share with your friends and colleagues on Facebook. If you have any questions or feedback then please drop a note.

--

--

Arun Jijo
Javarevisited

Data engineer at DataKare Solutions who gained expertise at Apache Nifi, Kafka, Spark and passionate in Java.