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 Confused with GC related K&B 1.5 question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Confused with GC related K&B 1.5 question" Watch "Confused with GC related K&B 1.5 question" New topic
Author

Confused with GC related K&B 1.5 question

Marcin Skrzek
Ranch Hand

Joined: Oct 19, 2005
Posts: 38
There is question in K&B 1.5 Study Guide which answer I can`t understand:

Let`s take the code snippet:


When // doStuff is reached, how many objects are eligible for GC?

Answer is: 2 objects while i`m sure it is: 1 object.

Why?
5<127, so both c1.story and c2.story references point to the same object (of Short class) - i mean in the code above there is only one Short object created. So, when c1 becomes null there is only single object eligable for garbage collection (first of 2 CardBoard objects).

Can anybody tell I`m right or not?


SCJP 5, SCWCD 1.4, SCBCD 5
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
You are right.
Check this thread.
Marcin Skrzek
Ranch Hand

Joined: Oct 19, 2005
Posts: 38
Thanks.
I`ve found errata and now all is clear for me - "Short story = 200" makes really big difference in this case.
Steven Gao Song
Ranch Hand

Joined: Oct 02, 2006
Posts: 78
I think that c1 and c2 are referring to different object.

c1 ---> new CardBoard();
c2 ---> new CardBoard();

Can you tell me why c1 and c2 are referring to the same object?
According to my opinion, only the object referred by c1 can be released.
the one referred by c2 cannot be released. So, the answer is 1 object.
Marcin Skrzek
Ranch Hand

Joined: Oct 19, 2005
Posts: 38
I didn`t say that c1 and c2 point to the same object but c1.story and c2.story:

c1.story --> new Short(5) <-- c2.story

Check K&B SCJP 1.5 Study Guide - page 235/236 for the reason of that.
Anvi Dixit
Ranch Hand

Joined: Aug 28, 2006
Posts: 45
Hi All,

one thing is clear that c1 is eligible for the GC.
but which is the other object eligible for GC if the answer is '2 objects'.

Also if we make Short story = 5 , this is the second correct answer
or if we make Short story = 200 , is the second correct answer.

here story is a variable of type Short and not the object so in any case story is not correct answer eligible for GC if it is..

I hope the doubt is clear..
Please Help me out in this
thanks,
Anvi

Pankaj Patel
Ranch Hand

Joined: Jul 13, 2006
Posts: 73
You are Right. There is only one object eligible to GC and that is c1.
Anvi Dixit
Ranch Hand

Joined: Aug 28, 2006
Posts: 45
So it means that for the above code when Short story = 5 , only one object c1 is eligible for GC .
Please Ranchers correct me if i am wrong

Also please explain the below statement by example if possible ;
When Short story = 5, you end up with all the objects referencing the
pooled instance of the Short Object.
But when its made as Short story = 200, there will be no pooled instance
created, as the value is out of range(-128 to 127).
So new Short objects are created for each instance of the class.


Thanks in advance
Anvi
Sireesha Mullapudi
Ranch Hand

Joined: Jun 26, 2006
Posts: 74
When Short story = 5, you end up with all the objects referencing the
pooled instance of the Short Object.
But when its made as Short story = 200, there will be no pooled instance
created, as the value is out of range(-128 to 127).
So new Short objects are created for each instance of the class.


Is this applies in scjp1.4?
I think for scjp1.4,2 objects eligible for GC.one is c1 and one is short object that is associated with C1.

Could you explain?
Sai Surya
Ranch Hand

Joined: Feb 08, 2006
Posts: 457

So,

If Short story = 200 then for each instance new Short object will be created. Then Three objects are eligible for GC.

<code>
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;
// do Stuff
} }
</code>

1. C2
2. Short story inside C2
3. C1

Please anyone correct me if I am wrong,

- Surya


Sai Surya, SCJP 5.0, SCWCD 5.0, IBM 833 834
http://sai-surya-talk.blogspot.com, I believe in Murphy's law.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confused with GC related K&B 1.5 question
 
Similar Threads
Garbage collector
Garbage Collection Question
How many objects are eligible for GC?
Ques from Self Test in Chap3/K&B 5 ,page 266
Garbage Collection