• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

island of isolation

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi , I was going through this example in k&B related to island of isolations.
public class Island {
Island i;
public static void main(String [] args) {
Island i2 = new Island();
Island i3 = new Island();
Island i4 = new Island();
i2.i = i3; // i2 refers to i3
i3.i = i4; // i3 refers to i4
i4.i = i2; // i4 refers to i2
i2 = null;
i3 = null;
i4 = null;
// do complicated, memory intensive stuff
}
}
The explanation given in the book is:
When the code reaches // do complicated, the three Island objects (previously known as i2,i3, and i4) have instance
variables so that they refer to each other, but their links to the outside world (i2, i3, and i4) have been nulled. These
three objects are eligible for garbage collection.

I think this means that we are simply setting the reference variables i2,i3,i4 to null which refer to 3 objects created by following piece of codes:
Island i2 = new Island();
Island i3 = new Island();
Island i4 = new Island();
Thus making the 3 objects eligible for garbage collection.
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They are not eligible for GC unfortunately. Search the forum, this topic has been discussed before. The island is by itself not reachable but the references point to each other and the JVM thinks that these references can be reached since they point to each other circularly
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to K&B, Chapter 3, answer for question number 10

"objects can live in "islands of isolation" and be GC elligible. "

From that my interpretation is that they will indeed be elligible.

I wonder if the Language Spec discusses this at all?
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by John Meyers:
They are not eligible for GC unfortunately. Search the forum, this topic has been discussed before. The island is by itself not reachable but the references point to each other and the JVM thinks that these references can be reached since they point to each other circularly



As I had understood it, the "island" is eligible for garbage collection, despite the circular references, because none of the objects are reachable.
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If answer to the above question is 3 i.e. only 3 objects namely i1, i2 & i3 are eligible for garbage collection. Than my question is what will happen to Object i inside each of these i1, i2 & i3 ???

Will they be also not Garbage collected along with i1,i2 & i3 ???
 
Greenhorn
Posts: 17
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Saurabh,

The instance variable "i" inside class Island is not an object, rather it is just an object reference.

Therefore only objects i1,i2 and i3 are eligible for garbage collection.
[ June 18, 2008: Message edited by: Akash Mohapatra ]
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by John Meyers:
They are not eligible for GC unfortunately. Search the forum, this topic has been discussed before. The island is by itself not reachable but the references point to each other and the JVM thinks that these references can be reached since they point to each other circularly

I thought the garbage collector uses on a mark-and-sweep algorithm which ignores such circular references.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Add a finalize() method to the Island class like this:You will see "finalize" printed 3 times before the "memory-intensive" stuff, showing that 3 objects are garbage collected at that point. So the garbage collector can obviously ignore circular references.
 
Deepak Bala
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Campbell Ritchie:
Add a finalize() method to the Island class like this:You will see "finalize" printed 3 times before the "memory-intensive" stuff, showing that 3 objects are garbage collected at that point. So the garbage collector can obviously ignore circular references.



My bad. They are indeed eligible for GC. Thanks for pointing it out
 
reply
    Bookmark Topic Watch Topic
  • New Topic