This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Objects - GC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Objects - GC" Watch "Objects - GC" New topic
Author

Objects - GC

Lovleen Gupta
Ranch Hand

Joined: Feb 26, 2007
Posts: 63
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.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707


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


cmbhatt
Rajeswari Kumar
Greenhorn

Joined: Mar 23, 2007
Posts: 26
Hi Lovleen
This issue is already discussed. Please check the link
http://www.coderanch.com/t/262041/java-programmer-SCJP/certification/Garbage-Collection-any-one-explain

regards
Rajeswari
Lovleen Gupta
Ranch Hand

Joined: Feb 26, 2007
Posts: 63
Thanks Chandra & Rajeswari..
It is clear now..!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Objects - GC
 
Similar Threads
Garbage Collection clarification:
Garbage collection
GC help
Garbage Collection
Garbage collection