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 garbage collection 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 "garbage collection" Watch "garbage collection" New topic
Author

garbage collection

Raymond Chamberglain
Greenhorn

Joined: Jun 14, 2009
Posts: 8
I'm confused with the answer to this question. This question is question one from chapter 3's self test section. It asks that when '// do stuff' is reached, how many objects are eligible for GC?



I know c2 is not null because it is referencing to a CardBoard object. c3 is referencing to the cb reference variable (Is this correct?). c1 is null. But how does the Short wrapper object becomes eligible for GC? The answer says "it has an associated Short wrapper object that is also eligible." what is 'it'? c1? Please help.. My mind is doing an infinite loop and about to blow up the stack.. :P Thanks.
kaushik vira
Ranch Hand

Joined: Feb 01, 2007
Posts: 102
Hello Raymond,

Your base assumption is wrong here.

know c2 is not null because,it is referencing to a CardBoard object.


if you look in to go method implementation.. when you call c2 become GC eligible.

4 Object is eligible for GC - 2 CardBoard and 2 Short object


Please correct me rancher if I am wrong..


kaushik Vira
-------------------------------------
SCJP, Preparing SCWCD..
Raymond Chamberglain
Greenhorn

Joined: Jun 14, 2009
Posts: 8
The answer is 2, but I don't know where one of them comes from.

From what I understand, there are 2 cardboard objects and 2 Short wrapper objects (created when the two cardboard objects are initialized. Let's call cardboard initialized to c1 is A and the other B.
by the time // dostuff is reached, c1 and c3 is null. since c1 is null, cardboard A has no more references referring it. c2 is still referring to cardboard B, because go() method doesn't use the c2, instead it creates a copy of the c2 variable. However the cb variable in go() method is referring to cardboard B. Regardless, cb is null when // dostuff is reached.

So it goes like this:

c1 -> cardboard A.
c2, cb -> cardboard B.
c3 -> null

When // dostuff is reached, c1 and cb becomes null. But c2 still exist. So cardboard A and its Short wrapper is eligible for GC. Is this the right explanation? Sorry that I kinda go around in circles. I am still trying to sort my thoughts. lol..
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18499
    
  40

When // dostuff is reached, c1 and cb becomes null. But c2 still exist. So cardboard A and its Short wrapper is eligible for GC. Is this the right explanation?


Yes.


And BTW, next time, please mention that K&B is the source. Saying chapter 3, is not specific enough.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Mo Jay
Ranch Hand

Joined: Feb 16, 2009
Posts: 83
Sorry kaushik vira but your answer is totally wrong.

I suggest that you read Raymond Chamberglain explanation as he seems to get the answer correctly.

Cheers!!!
Pooja Prakash
Greenhorn

Joined: Jul 01, 2009
Posts: 9
Hi,
Raymond's explanation is perfectly clear. I had misunderstood the explanation in K&B book but now i've got it all sorted. Thanks Raymond

Cheers,
-Pooja
 
Consider Paul's rocket mass heater.
 
subject: garbage collection
 
Similar Threads
Garbage Collection
question from K&B - pl clarify
K&B ch-3 Ques-2
How many objects are eligible for GC?
eligible for GC