This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes eligible for GC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "eligible for GC" Watch "eligible for GC" New topic
Author

eligible for GC

neeraj nandwana
Greenhorn

Joined: Sep 17, 2007
Posts: 6


and the Answer is: Only one CardBoard object (c1) is eligible, but it has an associated Short wrapper object that is also eligible

why "c3" not eligible for GC?
Object "c3" is also have the null value.
srinivas sridaragaddi
Ranch Hand

Joined: Jul 24, 2007
Posts: 225
hi,

Object c2 is passed as a parameter through go method and assigned a null value. In turn c3 is also assigned a null value but it is still having a reference through object c2 only. so c3 is not eligible for GC. only c1 which is assigned a null in main method is eligible for GC.

Hope this will help you.



SCJP 5.0<br /> <br />"Skills are started with learning and mastered with improvement. Nothing is hereditary except death" BUDDHA...
neeraj nandwana
Greenhorn

Joined: Sep 17, 2007
Posts: 6
hi srinivas
thanks for your help. But i am still confuse.
because c3 hold the null value, and i think It is not mater through which reference it got the value only mater the value, it hold at run time.
Kota li
Greenhorn

Joined: Sep 05, 2007
Posts: 20
Originally posted by neeraj nandwana:


and the Answer is: Only one CardBoard object (c1) is eligible, but it has an associated Short wrapper object that is also eligible

why "c3" not eligible for GC?
Object "c3" is also have the null value.
Kota li
Greenhorn

Joined: Sep 05, 2007
Posts: 20
Originally posted by neeraj nandwana:


and the Answer is: Only one CardBoard object (c1) is eligible, but it has an associated Short wrapper object that is also eligible

why "c3" not eligible for GC?
Object "c3" is also have the null value.


Whyit it is not eligible means after assignment c3 c1 is null. i.e that is problem
Kota li
Greenhorn

Joined: Sep 05, 2007
Posts: 20
Originally posted by neeraj nandwana:
hi srinivas
thanks for your help. But i am still confuse.
because c3 hold the null value, and i think It is not mater through which reference it got the value only mater the value, it hold at run time.


after assignment c3 is.
and c1 is null
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424
This example is taken from K&B and is corrected in the errata:

In this case 2 Objects are eligible for GC(1 CardBoard(c1), 1 Short).

if Short was less than or equal 127 then c2 would also contain it hence not eligible for GC.
Ajay Chandravadiya
Greenhorn

Joined: Aug 06, 2007
Posts: 27
Original problem seems to be like this...

C3 = C1.go(C2); // which returns null
Hence, C3 was NEVER assigned to a New Object!

so Total Objects created were C1, C2 , Short C1.short , C2.short.

When we said C1= null, we removed C1 (and C1.short ) references.

But C1.short = 5 = C2.short (as -128 to 127 wrapper objects refer to a unique object of that value)

Hence, Total Objects un-referred= C1 only.

bingo


SCJP Tiger Hunter 91%.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

there is a difference between OBJECTS and REFERENCES. c1, c2 and c3 are references. they can and do exists independantly of objects. if you write:

Cardboard c1;

you have created a reference with no associated object. So, in the sample code provided, you create the two references c1 and c2, and create an associated object for each. The line:

CardBoard c3 = c1.go(c2);

created a new reference, but no object. only two objects have been created.

you then set c1 to null. c1 is not eligible for gc, but the object it USED to point to is. "c3" is NOT an object, and is not eligible for gc.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
neeraj nandwana
Greenhorn

Joined: Sep 17, 2007
Posts: 6
Ok that�s the problem
C3 = C1.go(C2); // which returns null
Hence, C3 was NEVER assigned to a New Object!

Thanks for your help
Mehdi Ben Larbi
Ranch Hand

Joined: Aug 17, 2010
Posts: 70
I am having a LOT of trouble with GC,i don't understand why the C2 isn't eligible for GC after the go method ?
Thanks.


SCJP 93 % ,preparing for SCWCD
sumit kothalikar
Ranch Hand

Joined: Apr 15, 2010
Posts: 91

the go() method returns what cb is pointing to ,but here cb is pointing to null.
and in the main method c3 points to what go() method returns

now her c3 is pointing to null

c3 ----> null

reference variable pointing towards null does not mean it is eligible for garbage collection

here c3 is not pointing to any object no object is created so there will no object eligible for garbage collection.

i hope this helps


Thanks & Regards
Sumit Kothalikar
Mehdi Ben Larbi
Ranch Hand

Joined: Aug 17, 2010
Posts: 70
Yes it helps,thanks but C2 is also null after the go method.
Aren't we in the case 'Islands of Isolation' ?
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 503
    
    4

c2 is not null after the go method. c2 the reference variable is copied as a parameter into the go method, and that parameter is set to null, but c2 still points to its assigned object.


Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

Dieter Quickfend wrote:c2 is not null after the go method. c2 the reference variable is copied as a parameter into the go method, and that parameter is set to null, but c2 still points to its assigned object.

c2 is not copied. By default java passes objects by-reference and not by-value. So, I too feel, c2=null and c1=null => objects referenced by c1 & c2 must be eligible for GC.


OCPJP 6
Tareq Ateik
Ranch Hand

Joined: Jan 16, 2010
Posts: 36
ahmed yehia wrote:This example is taken from K&B and is corrected in the errata:

In this case 2 Objects are eligible for GC(1 CardBoard(c1), 1 Short).

if Short was less than or equal 127 then c2 would also contain it hence not eligible for GC.


i just want to clarify something, only one object is eligible for GC, and its just the CardBoard c1 , NOT the Short object, as the other c2 holds the same value of the short.
i know that sounds weird, but that happens to save memory.

also Short object can hold 2^16 which means more than 65000, i think you meant Byte object.


OCJA6, OCJP6
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

What about c2 then, isnt assigning null to c2 mean the object it was pointing to previously, eligible for GC?
Tareq Ateik
Ranch Hand

Joined: Jan 16, 2010
Posts: 36
Vinoth Kumar Kannan wrote:What about c2 then, isnt assigning null to c2 mean the object it was pointing to previously, eligible for GC?


this question was answered before if you look up, but ill say it again.

when the c1.go(c2) was executed, what happens actully in that method is the c2 value was copied to cb of the go method.
in this moment, two references point to that object, c2 & cb,
then the method took cb and destroyed it, but we still have c2!!

its something like this:



you just have to think like the java compiler, the methods take "COPY" of the parameter, not the parameter itself
i hope its clear now
Mehdi Ben Larbi
Ranch Hand

Joined: Aug 17, 2010
Posts: 70
Yeah it's much better,thanks a lot.
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 503
    
    4

Vinoth Kumar Kannan wrote:
Dieter Quickfend wrote:c2 is not null after the go method. c2 the reference variable is copied as a parameter into the go method, and that parameter is set to null, but c2 still points to its assigned object.

c2 is not copied. By default java passes objects by-reference and not by-value. So, I too feel, c2=null and c1=null => objects referenced by c1 & c2 must be eligible for GC.

No, Java passes variables by value, ALWAYS. If the variable is a primitive, it passes the primitive's value, if the variable is a reference, it passes the reference value, which is the reference to the object in memory.

So you could say it passes Objects by reference - it never passes the object itself.. But it passes variables by value - the value of a primitive, the value or, if you will, memory address, of the reference variable.
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

Oh..great! I was assuming myself that primitives were passed by-value and Objects were passed by-reference. Thanks for pointing it out Dieter and Tariq.
I happened to come across this link - http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html which explains this much better. Thought it might be helpful to someone..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: eligible for GC
 
Similar Threads
GC question
Garbage Collection Doubt
K&B ch-3 Ques-2
how many objects are eligible for GC
Garbage Collection