File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage Collection" Watch "Garbage Collection" New topic

Garbage Collection

Tarun Kumar

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

Joined: Sep 28, 2004
Posts: 20517

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.


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.)

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.

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: 15081

Please use code tags when you post source code.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Tarun Kumar

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:
subject: Garbage Collection
It's not a secret anymore!