"Island of isolation" of Garbage Collection
JavaGarbage CollectionJava Problem Overview
Could anyone please explain the concept of Island of isolation of Garbage Collection?
Java Solutions
Solution 1 - Java
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;
Solution 2 - Java
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.
Solution 3 - Java
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.
Solution 4 - Java
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.