• Post Reply Bookmark Topic Watch Topic
  • New Topic

Garbage collection question  RSS feed

 
Ranch Hand
Posts: 33
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chapter - 3 Assignment, Self Test Q#11 from K&B SCJP 1.6



Answer is 1 object will be eligible for GC.
However I can see 2 eligible objects, one is object a1 itself and the other is object a1.b2 which also we cannot refer after setting a1 = null.

Please clarify.
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Vivek Raj wrote:However I can see 2 eligible objects, one is object a1 itself and the other is object a1.b2 which also we cannot refer after setting a1 = null.


a1 and a1.b2 are not objects, they are references to objects. An object is only eligible for garbage collection when there are no reachable references to it.
You're right that when a1 is set to null, any object referred to by a1.b2 might also be eligible for garbage collection. So if it isn't, there must be another reference to the same object - can you see what it is ? Have a look at line 12.
 
Vivek Raj
Ranch Hand
Posts: 33
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got the point.

So if w change line4 to
Beta b2 = new Beta();

then there would be 3 objects for GC.
 
Try 100 things. 2 will work out, but you will never know in advance which 2. This tiny ad might be one:
Thread Boost - a very different sort of advertising
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!