aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes object eligible for GC 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 "object eligible for GC" Watch "object eligible for GC" New topic
Author

object eligible for GC

weiquan yuan
Greenhorn

Joined: Jun 13, 2006
Posts: 5
here is the one question in Self test in chapter 3 of the book SCJP 310-065

why the answer is only 2 objects are eligible for GC? I think c1 and c3 are both eligible for GC, so plus short instance variable, there should be 4 objects are eligible for GC.
=========================================================================

public class CardBoard {
Short story = 200;
CardBoard go(CardBoard cb){
cb.story = 100;
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;
System.out.println(" c1 \n" + c1 + " c2 \n" + c2 + " " + c2 + " c3 \n" +c3);
}
}
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Hello weiquan, welcome to javaranch.

I think c1 and c3 are both eligible for GC


By this, I'm getting that you are thinking c2 will be eligible for GC. But it will be not. When you set cb to null in go method, that doesn't effect c2. Also c3 was null from the beginning. So there is no way of it being eligible for GC. I feel that 3 objects will be eligible for GC, c1, c1.story and c2.story. Let's see what others think.

Also please Use Code Tags when you post a source code...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Why should c3 be eligible for GC? It has a reference to c2.

So c1 including it's Short instance variable are both eligible for GC, hence the answer is 2


JDBCSupport - An easy to use, light-weight JDBC framework -
shivendra tripathi
Ranch Hand

Joined: Aug 26, 2008
Posts: 263
Only two ClipBoard objects have been created. First one has no reference but second is still being reffered by C2. So only one ClipBoard and total two objects available for garbage collection.


SCJP 1.5(97%) My Blog
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

I guess you have to remember that its pass by copy. Only the way to get to the object is pass and not the actual reference. So what ever changes that are made in the method is done for that method reference only that is cb. All c3 refers to null as cb = null is returned to c3. SO when c1 = null then its cardboard and Short object are eligible for GC. Well the main thread completes then yes 4 objects are eligible.
From Ankit: I feel that 3 objects will be eligible for GC, c1, c1.story and c2.story


How can c2.story be eligible for gc if the object carrying its reference is still reachable from c2?


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Nitish Bangera wrote:
From Ankit: I feel that 3 objects will be eligible for GC, c1, c1.story and c2.story


How can c2.story be eligible for gc if the object carrying its reference is still reachable from c2?


The value of c2.story is changes in the go method. Since it is of type Short which is immutable, so the original object which story referred to (which had the value 200) will be eligible for GC...
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

hmmm if you see that then its true but then it will not be c2.story coz it has been reassigned to a new object. Well i checked with the original question in the book and guess what there is no cb.story in the go method so the appropriate answer there will be 2.


(Lol bad habit of eating up words)
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Nitish Bangera wrote:but then it will not be c2.story coz it has reassigned to a new object


True, I must have said the original object to which c2.story referred (which contained value 200)...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: object eligible for GC