File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question about GC problem in k&b's book for 1.5 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question about GC problem in k&b Watch "question about GC problem in k&b New topic
Author

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

Anna Chu
Ranch Hand

Joined: May 18, 2006
Posts: 30
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

Joined: Apr 25, 2005
Posts: 809
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


Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Anna Chu
Ranch Hand

Joined: May 18, 2006
Posts: 30
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

Joined: Apr 25, 2005
Posts: 809
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

Joined: Aug 02, 2006
Posts: 152
Remember when you pass a reference to a method it actually creates a copy of the reference and the original reference keeps unchanged.


SCJP 5.0 95%<br /> <br />The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question about GC problem in k&b's book for 1.5