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 collector 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 collector" Watch "Garbage collector" New topic
Author

Garbage collector

rakhee gupta
Ranch Hand

Joined: May 01, 2008
Posts: 43
Please see the question.

Given:
class CardBoard {
Short story = 5;
CardBoard go(CardBoard cb) {
cb = null;
return cb;
}
public static void main(String[] args) {
CardBoard c1 = new CardBoard();
CardBoard c2 = new CardBoard();
CardBoard c3 = c1.go(c2);
c1 = null;
// do Stuff
} }
When // doStuff is reached, how many objects are eligible for GC?
A. 0
B. 1
C. 2
D. Compilation fails.
E. It is not possible to know.
F. An exception is thrown at runtime.

Answer is C.
The explanation given in K&B is:
C is correct. Only one CardBoard object (c1) is eligible, but it has an associated Short wrapper object that is
also eligible.

I cant understand which short object it is talking about.

I thought the 2 objects eligible for GC would be:
C1 and C3( as the method CardBoard is returning a null instance variable which is getting assigned to reference C3).
Zaheer Ahmed
Greenhorn

Joined: Feb 06, 2007
Posts: 22
You are right. C1 and C3 are the ones that have been assigned null values and ready for GC.

And if you look closely at the answers you will see that option C says 2. I guess the explanation is wrong and there is a typo in there.

Please have a look a look at the following link for a list of K&B errata.

http://www.coderanch.com/t/257589/java-programmer-SCJP/certification/SCJP-Errata-Updated

// Zaheer


Zaheer Ahmed<br />SCJP 5
Zaheer Ahmed
Greenhorn

Joined: Feb 06, 2007
Posts: 22
It would be great if any of the guys (Bert, Jesper, Ben, or marc) can verify this???

// Zaheer
Kishore Kumar
Ranch Hand

Joined: Oct 15, 2007
Posts: 71
Hi,
Actually C1 object is eligible for Garbage Collection. But, C3 gets assigned null, and also C3 is not referring to any object. So there is no object ,eans, no garbage collection for C3.
Coming to second object which is eligible for garbage collection is the wrapper class object Short which is a member variable is eligible for GC.

I hope i explained clearly.
rakhee gupta
Ranch Hand

Joined: May 01, 2008
Posts: 43
Hi Kishore,
First point is clear but second is still unclear how does the instance variable become available for GC.Is it referring to any object?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

No, you have one of the standard misconceptions. Garbage collection does not operate on variables, it operates on objects. The question you have to answer is, for which objects are there any variables that contain references to them.
rakhee gupta
Ranch Hand

Joined: May 01, 2008
Posts: 43
Yes sorry I meant to ask this only that when does the wrapper object Short become eligible for GC?
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 787

jesper young please help on this


SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
kapil kumar
Greenhorn

Joined: May 22, 2008
Posts: 25

Please see the question.

Given:
class CardBoard {
Short story = 5;
CardBoard go(CardBoard cb) {
cb = null;
return cb;
}
public static void main(String[] args) {
CardBoard c1 = new CardBoard();
CardBoard c2 = new CardBoard();
CardBoard c3 = c1.go(c2);
c1 = null;
// do Stuff
} }
When // doStuff is reached, how many objects are eligible for GC?
A. 0
B. 1
C. 2
D. Compilation fails.
E. It is not possible to know.
F. An exception is thrown at runtime.

Answer is C.
The explanation given in K&B is:
C is correct. Only one CardBoard object (c1) is eligible, but it has an associated Short wrapper object that is
also eligible.

I cant understand which short object it is talking about.

I thought the 2 objects eligible for GC would be:
C1 and C3( as the method CardBoard is returning a null instance variable which is getting assigned to reference C3).




Hi Rakhee.......
In the above code the answer C is correct...
Only the object referenced by c1 is eligible for gc.
But since the object itself is having Short as its instance variable
Short story = 5; also becomes eligible for gc since there are no external references from any live thread...

And one more thing it is the objects that are eligible for garbage collection and not reference variables.......

CardBoard c3 = c1.go(c2); is only assigning null reference to variable
c3 .There is no object that c3 points to.......

So the question of c3 being garbage collected does not arise......

I hope this clears your doubt...


If you can't be the Sun ..<br /> Be a star ..............
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage collector
 
Similar Threads
Garbage Collection
K&B ch-3 Ques-2
K&S CHAPTER 3
SCJP for Java 6, Garbage Collection question
GarbageCollection