• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage collector

 
rakhee gupta
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be great if any of the guys (Bert, Jesper, Ben, or marc) can verify this???

// Zaheer
 
Kishore Kumar
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes sorry I meant to ask this only that when does the wrapper object Short become eligible for GC?
 
Ninad Kulkarni
Ranch Hand
Posts: 802
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jesper young please help on this
 
kapil kumar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic