• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GC: Islands of Isolation

 
Royston Monteiro
Ranch Hand
Posts: 35
Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi folks,

In the above code, I was wondering..If i2 is set to null, doesnt all the instance variables of i2(and hence i2.n) also get set to null?
Why does K&B(pg 249, 250) say that i2.n still exists and "points" to i3?
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes that is correct. Just because you set a reference of an object to null does not mean the object's member references will be set to null.
 
Royston Monteiro
Ranch Hand
Posts: 35
Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about this:


In this case, after setting c1 = null, the c1.story reference should still be on the stack(like in the previous example), referring to the Short object.
But the answer says that at this stage, even the Short object is eligible for garbage collection
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is correct as well. You must however notice that there is not island of isolation here

Reference -> Short

After setting the reference to null there is not way to reach the Short. So the JVM GCs it. In case of the island however the JVM thinks the objects can be reached since they point to each other in a circle. The JVM does not figure that these objects dont have an external reference through which they can be reached, even though they can reach each other through their internal references.

Does that make it clear ? Or have I managed to confuse you more
 
Royston Monteiro
Ranch Hand
Posts: 35
Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh ok. Now I get it.

All clear John, thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic