Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!