aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection 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 "Garbage Collection" Watch "Garbage Collection" New topic
Author

Garbage Collection

Tarun Kumar
Greenhorn

Joined: Apr 30, 2008
Posts: 26
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?

Tarun Kumar
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
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.


All code in my posts, unless a source is explicitly mentioned, is my own.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18139
    
  39


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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
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

Joined: Oct 16, 2008
Posts: 952
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.



SCJP 6
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
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

Joined: Aug 16, 2005
Posts: 13884
    
  10

Please use code tags when you post source code.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Tarun Kumar
Greenhorn

Joined: Apr 30, 2008
Posts: 26
Thanks Ruben and all who post their reply.
And i will take care of code tag .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage Collection
 
Similar Threads
Objects - GC
Garbage collector
Garbage collection
question about GC problem in k&b's book for 1.5
Garbage Collection