• 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
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Scope and GC problem

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


According to this, 2 objects are eligible for GC, c1 and Short inside c1, my point its, c3 does send a reference to go (c2), and set it to null, shouldn't that also be eligible for GC?.

from K&B Page 259

Thanks Henry.



 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Remember that it is objects that are eligible for GC -- not references. So... what object was c3 referring to, that got no longer reachable, when the c3 reference was set to null?

Henry
 
Enrique Arguelles
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry I see now silly me, seems I need a bit more practice.
 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Enrique, first of all could you use the code tags, for your formatting, makes your code easier to interpret. Also check your spelling, on the code your return statement is spelt incorrectly, as it stands it woudn't compile in addition your reference types are incorrect, as they should be CardBoard not Cardboard and your class declaration should be spelt class not Class.

You also need to quote your source, was it a mock exam question for example, if so where from.

Ok so lets have a look, here is your example reproduced to cover the spelling changes:



Here is created 2 new Cardboard objects with the references c1 and c2. Then a new reference is created c3 which is basically assigned a null reference through c1.go(c2) for which you are making a new null reference to c2 and returning it so no new objects are created just null references. Then you set c1 to null so the new object created by c1 is all out there on the heap alone waiting for the garbage collector. So I'd say one object!
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephen Davies wrote:
Here is created 2 new Cardboard objects with the references c1 and c2. Then a new reference is created c3 which is basically assigned a null reference through c1.go(c2) for which you are making a new null reference to c2 and returning it so no new objects are created just null references. Then you set c1 to null so the new object created by c1 is all out there on the heap alone waiting for the garbage collector. So I'd say one object!




but what about the c2 reference? isn't that too being getting referenced to null when it is passed as the argument for the go method (to create the reference for c3)?
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

but what about the c2 reference? isn't that too being getting referenced to null when it is passed as the argument for the go method (to create the reference for c3)?



actually the argument in the go method is not c2 but Copy of the reference of c2.
Since it copy reference, you only make this copy reference to null but you cannot make original version (c2) to be null or refer to another object.
Please see this for better understanding.

Hope this helps.

 
Deepak Giri
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yepp.. i got it! actually it striked my mind earlier too but i was l'il confused about the same! thanks for clearing the doubt!
 
I can't beleive you just said that. Now I need to calm down with this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic