File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage Collection Question" Watch "Garbage Collection Question" New topic
Author

Garbage Collection Question

ManChun Lam
Ranch Hand

Joined: May 04, 2008
Posts: 32
This question is taken from Sierra & Bates SCJP for Java 5, Chapter 3, Self-test Q2



Q: When // do Stuff is reached, how many objects are eligible for GC
A: C. 2

I got the same answer but my explanation is different.

First, the book's version: C is correct. Only one CardBoard object (c1) is eligible, but it has an associated Short wrapper object that is also eligible.

My explanation: c3 == null, c1 == null. Therefore the answer is 2.

Am I missing something here?
Harvinder Thakur
Ranch Hand

Joined: Jun 10, 2008
Posts: 231
My explanation: c3 == null, c1 == null. Therefore the answer is 2.


Well, the above explanation implies that there are 2 Cardboard objects + 2 Short objects in each of the Cardboard objects eligible for GC.
But that is incorrect.

c3 is a reference and was never assigned any object as the go() method returns a null.

So the correct answer is *c1* and the enclosed object *story* are eligible for GC.


thanks
Harvinder
ManChun Lam
Ranch Hand

Joined: May 04, 2008
Posts: 32

c3 is a reference and was never assigned any object as the go() method returns a null.


Thank you very much for this explanation. I completely overlooked this.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18836
    
  40

This question comes up very often. Please search the forums for other discussions about this... There is also an Errata related to this question.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
 
subject: Garbage Collection Question