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

Confused with GC related K&B 1.5 question

 
Marcin Skrzek
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right.
Check this thread.
 
Marcin Skrzek
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are Right. There is only one object eligible to GC and that is c1.
 
Anvi Dixit
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 463
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic