Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K&S CHAPTER 3

 
akash azal
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2. Given:
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
} }
When // doStuff is reached, how many objects are eligible for GC?
A. 0
B. 1
C. 3
D.compilation fails
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you think the answer is ?
 
akash azal
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
B c1 is eligible for GC
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
akash azal wrote:B c1 is eligible for GC


That would be correct
 
akash azal
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but in K&S answer is c
 
Sunny Mattas
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but in K&S answer is c

hi
There is a mistake with this question in K&S.
Book says second object garbage collected is


But this will not garbage collected because value of short is under 127 which means it is in cache memory.
If value is changed to


then two objects will be garbage collected.

Regards
Sunny Mattas
SCJP5


 
akash azal
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for clearing doubt
 
Aakash Goel
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But this will not garbage collected because value of short is under 127 which means it is in cache memory.


What is the logic behind this?

I am completely unaware of the logic used here. Can somebody explain?
 
Byju Joy
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the logic behind this?


I believe it's 10% of logic + 20% of heuristic + 30% of assumption + 40% of confusion behind this.

Statistically speaking, according Pareto's principle, 80% of numeric variables are using only 20% of possible values. 80% of Short variables take values in the range of 0-127 only. So they are 'cached' or 'shared'.

Later when economy improves, and memory cards becomes cheaper this decision could be revised to cache all Short values.
 
Henry Wong
author
Marshal
Pie
Posts: 21212
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aakash Goel wrote:
What is the logic behind this?

I am completely unaware of the logic used here. Can somebody explain?


The "cache" being referred to here, is the cached in the Short class -- basically, the class will pre-instantiate a certain number of Short objects in a range... Later, when the valueOf() method is called, and the value is within the range, it will return the object from the cache, instead of creating a new one. And since autoboxing uses the valueOf() method, it uses the cache.

In relation to this question, the short is within the range, hence, it can't be GCed, because the object returned is still in the cache. In the errata, the short value is changed so that it is not in the cache.

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 21212
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Byju Joy wrote:
Later when economy improves, and memory cards becomes cheaper this decision could be revised to cache all Short values.


Interestingly, this can be done at any time, without the need to change the specification. The specification defines what range must be cached. It doesn't say what should be done with values outside of the range. Hence, a version of the JVM can be released, which caches everything, and it will still be compliant with the Java specification.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic