Forums Register Login

question about GC problem in k&b's book for 1.5

+Pie Number of slices to send: Send
The second question in chapter 3:
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;
//dostuff
}
The question is: how many objects are eligible for GC when //dostuff is reached.
The answer is: 2; only object (c1) is eligible and its associated Short wrapper object that is also eligible.

My question is: why c3 and cb are not eligible for GC since both of them are null?
Can some one help me with this? Thanks a lot.
+Pie Number of slices to send: Send
Well the answer for this question is one. See the K&B errata section and modify this question. When you modify your question, then objects eligible for GC will be two.

My question is: why c3 and cb are not eligible for GC since both of them are null?



Right from start to end of your program, is c3 pointing to any object? It is always null. So there is no question about gc of c3 object.

cb is set to null. Fine but you must see that both cb and c2 are pointing to same object, so even cb is set to null, still this will not allow CardBoard object for GC as it is still referenced by c2.

Naseem
+Pie Number of slices to send: Send
 

Originally posted by Naseem Khan:
Well the answer for this question is one. See the K&B errata section and modify this question. When you modify your question, then objects eligible for GC will be two.



Right from start to end of your program, is c3 pointing to any object? It is always null. So there is no question about gc of c3 object.

cb is set to null. Fine but you must see that both cb and c2 are pointing to same object, so even cb is set to null, still this will not allow CardBoard object for GC as it is still referenced by c2.

Naseem




When excute "cb=null" in go method, cb is assigned to null, but c2 will not assign to null. That's why I think cb should be eligible to GC.
+Pie Number of slices to send: Send
 

When excute "cb=null" in go method, cb is assigned to null, but c2 will not assign to null. That's why I think cb should be eligible to GC.



References are not garbage collected. Objects are garbage collected.

cb and c2 both point to same object. Even if you set cb null, object is reachable to you by c2. So cb=null will have no effect.

Naseem
[ August 26, 2006: Message edited by: Naseem Khan ]
+Pie Number of slices to send: Send
Remember when you pass a reference to a method it actually creates a copy of the reference and the original reference keeps unchanged.
I'm so happy! And I wish to make this tiny ad happy too:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 709 times.
Similar Threads
question from K&B - pl clarify
Garbage collection
GC question
GC help
Garbage Collection
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 08:24:46.