Could anyone please explain the concept of Island of isolation of Garbage Collection?
4 Answers
Object A references object B. Object B references object A. Neither object A nor object B is referenced by any other object. That's an island of isolation.
Basically, an island of isolation is a group of objects that reference each other but they are not referenced by any active object in the application. Strictly speaking, even a single unreferenced object is an island of isolation too.
Edit from Comment:
class A {
B myB;
}
class B {
A myA;
}
/* later */
A a = new A();
B b = new B();
a.b = b;
b.a = a;

- 5,377
- 5
- 41
- 72

- 118,853
- 40
- 150
- 176
-
This post maybe old. but my question this is question of mine might be kiddie but when you say Object A references object B. Object B references object A. Do you mean SOmething like this A a = B ; B b = a ? – user962206 Jul 08 '12 at 15:43
-
5@user962206 No he means `class A { B myB; } class B { A myA; } /* later */ A a = new A(); B b = new B(); a.b = b; b.a = a;` If you can decipher that... But in all honesty, it's enough to have `class A { B myB; } class B { } B b = new B(); A a = new A(); a.b = b;` – corsiKa Jul 31 '13 at 20:43
-
2Code should have been this: `A a = new A(); B b = new B(); a.myB = b; b.myA = a;` Please correct me if I'm wrong. – Akshay Feb 20 '20 at 06:48
-
But what happens to the isolated objects anyway? Will they even be removed at some point? – Java bee Nov 26 '20 at 12:21
Here is a good explanation of this term. Excerpt:
- "If an object obj1 is garbage collected, but another object obj2 contains a reference to it, then obj2 is also eligible for garbage collection"
- "If object obj2 can access object obj1 that is eligible for garbage collection, then obj2 is also eligible for garbage collection"
This is called "Island of Isolation". An "island of isolation" describes one or more objects have NO references to them from active parts of an application.

- 49,103
- 10
- 104
- 136
The thing to keep in mind is that objects are only collected if they are referenced, either directly or indirectly, from a GC root object (threads, current local variables, static variables etc). If two (or more) objects reference each other, but are not referenced from a root, then they are eligible for garbage collection.

- 4,268
- 1
- 27
- 17
In fact, if you understand the concept of Mark and Sweep of Garbage Collection, you'll understand better Island of Isolation too:
- The algorithm starts from the GC roots: main thread, local variables in the main method, static variables of the main class.
- The algorithm traverses all object references, starting with the GC roots, and marks every object found as alive.
- All of the heap memory that is not occupied by marked objects is reclaimed. It is simply marked as free, essentially swept free of unused objects.
- If two or more objects are referencing each other but they are not referenced by objects linked with any root, they are in the Island of Isolation, and are swept too.

- 87
- 10