• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

eligible for GC

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
neeraj nandwana
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
neeraj nandwana
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 91
Notepad
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Mehdi Ben Larbi
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes it helps,thanks but C2 is also null after the go method.
Aren't we in the case 'Islands of Isolation' ?
 
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about c2 then, isnt assigning null to c2 mean the object it was pointing to previously, eligible for GC?
 
Tareq Ateik
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah it's much better,thanks a lot.
 
Dieter Quickfend
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic