• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Garbage collection question

 
Ranch Hand
Posts: 33
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Let me tell you a story about a man named Jed. He made this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic