Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Anna Chu
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Anna Chu
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Javier Sanchez Cerrillo
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember when you pass a reference to a method it actually creates a copy of the reference and the original reference keeps unchanged.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic