• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!