• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection

 
Tarun Kumar
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a question in K&B book of chapter-3 Self-Test.


When //dostuff is reached , how many objects are eligible got GC?

As per Book answer it says 2 are eligible.(C1 and its associated Short story object.)
I think C2 and C3 and their associated objects are also eligible as their reference are null at this point.
Can any body explain me how?
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tarun,

There is an important omission in your code:

The reason why there is only 2 objects elligible for GC is that c2 still points to the original object. When you pass c2 to go(), the reference value is copied, so any assignments (including assigning null) to the parameter in the method don't affect the original reference. Java uses value-copy semantics both for reference and primitive variables.
 
Henry Wong
author
Marshal
Pie
Posts: 21518
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

First, please search this forum for this question -- this question comes up often here.

Second, also check the errata. There is a error in that the Short isn't eligible for GC -- autoboxing uses a cache, and a Short with a value of 5 is gotten from the cache.

Henry
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't realize that, but you are right. Where is this errata that you speak of? I could only find an errata for the previous edition (SCJP 5.)

Thanks.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Errata is here :

259....clarify...Q-2: code s/b: Short story = 200;
& 266

This problem is only in SCJP 5 book, in SCJP 6 book it is Short story=200; already, so Short s is not taken from cache as 200>127, it builds on heap now, so for C1 object, it's Short s is also eligible for GC.


 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are right in that it is corrected in the new edition, Punit.

I was aware of that errata for the SCJP 5 Guide. Is there any other thread for SCJP 6? If not, how do we go about reporting errata? Do we contact Bert directly?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15495
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code tags when you post source code.
 
Tarun Kumar
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ruben and all who post their reply.
And i will take care of code tag .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic