• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Objects - GC

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question is - how many objects are eligible for garbage collection at //doStuff?



My understanding says that there should be 4 objects (c1, c1.story, c2, c2.story). But the answer is 2. Please explain why is it so?

Thanks.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Hi Guptajee,
You should always see at how many times "new" is being used at first hand.
Inside the main method you created 2 objects; dont forget Short story =5; in the class definition, it is for every object, Short is wrapper not primitive. Inside the go method there wont be affect of making bs =null to the original main reference. It would have local impact at you know copy of the reference is passed to the method so if any internal modification is made to the object as "changing Person's name or age" using passed reference, it would affect the calling method but setting the reference to null in the called method wont affect the calling method object reference; so dont worry about that.

When you set the c1= null; Two objects become eleigible for garbage collection; the first the object referenced by c1 and the object referenced by the story referecen that is encapsulated in the object. So two objects would be eligible for garbage collection.


Hope this helps,

Thanks and Regards,
cmbhatt
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lovleen
This issue is already discussed. Please check the link
https://coderanch.com/t/262041/java-programmer-SCJP/certification/Garbage-Collection-any-one-explain

regards
Rajeswari
 
Lovleen Gupta
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Chandra & Rajeswari..
It is clear now..!
 
Put the moon back where you found it! We need it for tides and poetry and stuff. Like this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic