aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes how many objects are eligible for GC? 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 "how many objects are eligible for GC? " Watch "how many objects are eligible for GC? " New topic
Author

how many objects are eligible for GC?

Rahul mir
Ranch Hand

Joined: Oct 31, 2012
Posts: 38

This question is from K&B SCJP book page no. 269.

When // doStuff is reached, how many objects are eligible for GC?



from my point of view there is only 1 object is eligible for GC, c1. But the explanation given in Book i am not getting.
Books explanation : Only one CardBoard object (c1) is eligible, but it has an associated Short wrapper object that is also eligible..
Please someone explain me ...
Prajakta Acharya
Ranch Hand

Joined: Nov 08, 2012
Posts: 138

I guess its 3 because c2,c3 and c1 point to null after call to go method.


Regards,
Prajakta
Rahul mir
Ranch Hand

Joined: Oct 31, 2012
Posts: 38

Prajakta Acharya wrote:I guess its 3 because c2,c3 and c1 point to null after call to go method.


c3 is not referring any Object, after go() method call it holds null value, and c2 reference is not loosing there object so c2 is not eligible for GC. c1 = null, it is ok .. it will go for GC.
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1823
    
    7

I'd say that this question is complicated unnecessarily by the Short cache, and that the object reference by c1's story member variable is in fact not eligable for GC.
Questions about GC shouldn't use Strings, or primitive wrapper classes.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Kisor Biswal
Greenhorn

Joined: Mar 10, 2011
Posts: 3
Rahul mir wrote:This question is from K&B SCJP book page no. 269.

When // doStuff is reached, how many objects are eligible for GC?



from my point of view there is only 1 object is eligible for GC, c1. But the explanation given in Book i am not getting.
Books explanation : Only one CardBoard object (c1) is eligible, but it has an associated Short wrapper object that is also eligible..
Please someone explain me ...


When we call new CardBoard() 2 objects are created. 1st the new CardBoard object and 2nd the instance variable in it. i.e Short.
CardBoard c3 = c1.go(c2); at this line c3 is assigned with null, but there is no change to c2.

c1 = null; at this line we lose reference to the object created at CardBoard c1 = new CardBoard(); so this will be garbage collected along with it's instance variables.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18988
    
  40

Rahul mir wrote:
from my point of view there is only 1 object is eligible for GC, c1. But the explanation given in Book i am not getting.
Books explanation : Only one CardBoard object (c1) is eligible, but it has an associated Short wrapper object that is also eligible..
Please someone explain me ...


You have an very old copy of the book. This question was fixed a long time ago.

In the fixed version, the value being assigned to the Short reference is large enough to be outside of the range of the cache used by autoboxing.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
Posts: 256
This question ought to be made into a sticky thread, I've lost count of the number of times its has been asked here!
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
Hey Jelle,

A big part of why GC is on the exam is to get candidates familiar with when objects get created, and those times when you might be making objects without realizing that you're making objects.


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
 
wood burning stoves
 
subject: how many objects are eligible for GC?