• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object for eligible for GC question

 
howie jao
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • 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
 
Jacob Sonia
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • 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
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • 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.
 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
  • 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
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • 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
  • 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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic