• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Object for eligible for GC question

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI, below is a piece of code from the Sierra book. Can anyone explain or show me the step why only CardBoard Object c1 is eligible ?
Also, c1.go(c2) < --- this is passing the copy of c2 object , therefore cb = null is not affecting the originally c2 ??

class CardBoard{
short story = 200;
CardBoard go(CardBoard cb){
cb = null;
return cb;
}

public static void main(String[] s)
{
CardBoard c1 = new CardBoard();
CardBoard c2 = new CardBoard();
CardBoard c3 = c1.go(c2);
c1 = null;
}

Thanks for the help
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
c1 --- o1
c2 --- o2
c3 --- null

at the end
c1 --- null


so only c1 is eligible for GC, GC is done on objects not references.

Here c3 doesn't initialize any object
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

howie jao wrote :

< --- this is passing the copy of c2 object , therefore cb = null is not affecting the originally c2 ??



Here, you are passing a reference of the object of c2, and in the method, go(), you make that reference to null, but the object is referred by c2, so can't be collected by GC.


Hi, UseCodeTag.
 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Here, you are passing a reference of the object of c2, and in the method, go(), you make that reference to null, but the object is referred by c2, so can't be collected by GC.



Actually I think Jacob is right C3 never is assigned any cardBoard Object and is simply a reference. The C2 passed to c1.go() is a copy only and looses any relationship anyhow once c1 = null. So C1 which was originally assigned a Cardboard Object is set to null in the last line and is legible for GC. C3 has only been a reference pointer, to null , so no object was assigned and thus as areference it is not legible for GC as that is exclusive to Objects on the heap.

Only 2 Cardboard objects were created on the heap, and one was orphaned with a call to null

 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
He, howie jao, asked about object referenced by c2. So......
 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abimaran Kugathasan wrote:He, howie jao, asked about object referenced by c2. So......



I'm not sure what your statement above means but initially, C2 references a new Object on the heap.



When assigning a value to C3 you are using the C1.go() Method and passing a copy of the reference to the object referenced by C2 ( a new CardBoard object);

However the go() method then immediately nullifies the reference, so only the reference C2 remains to the object on the heap and C3 is simply an empty reference so cannot be legible for GC

C1 is then nullified, thus orphaning its object on the heap (no reference now points to the CardBoard Object originally referenced by C1 so that Object becomes legible for garbage collection


There are only ever two CardBoard objects on the heap (not withstanding the class itself) and only two references (C1 & C2) ever point to them. Then C1 is nullified, leaving its object unreferenced.
C3 doesn't point to anything, its nullified by the go() method, regardless of the copy of ref C2 that was passed

 
And inside of my fortune cookie was this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic