This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

object eligible for GC

 
weiquan yuan
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
shivendra tripathi
Ranch Hand
Posts: 263
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 537
Eclipse IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic