1

Apperently, the answer here is 2, according to the book I'm reading, but I still don't understand why. It says that after line "// do stuff", 2 objects will be eligible for GC.

Can someone please explain it to me step by step? And is there like a trick to knowing how many objects are eligible for garbage collection after a certain line in the code? Because we were told that those kinds of questions (how many are elligible for gc) will appear on the test.

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

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

And yeah, the answer is "2 objects will be eligble for Garbage Collection after the line "// do stuff"", but I still don't understand why.


In this image, it also says why the answer is 2, but the explanation just made it A LOT more confusing for me.

Image from the SCJP book we're told to read.

It doesn't even explain what happened to the other objects.


EDIT:

So according to you guys, it's C1 and C3 that are eligible for GC, and I can see why because C1 has been set to null and C1.GO(C2) returns null to C3.

But according to the book, it's only C1 that is eligible, and the answer is 2 because of the Short wrapper. I have no idea why that is, and what even is the Short wrapper object.

Robert Malansangan
  • 153
  • 1
  • 1
  • 6

3 Answers3

1

I've run and debugged the code and I can see that c1 and c3 are null (for obvious reasons) and that it's c2 the one that is != null.

That's because objects in Java are passed by value (explanation) and setting c2=null inside a method doesn't cause the original c2 to be set to null.

Community
  • 1
  • 1
Sherekan
  • 536
  • 3
  • 14
  • That's what I've just wanted to type.. :) c2 is not set to null, go()'s `cb` argument is set to null – Rafcik Mar 18 '16 at 12:18
  • But why does it say only c1 is null plus the short wrapper thing? I'm still very confused. – Robert Malansangan Mar 18 '16 at 12:30
  • c1 is null, because in the end null was assigned to c1; c3 is null, because c1.go(c2) returned null to c3; c2 is NOT null, because c2 reference to its object was passed to argument cb in go() function, however c2 wasn't assigned to null itself, but cb in go() function only; – Rafcik Mar 18 '16 at 13:18
0

c2 is still referencing the cardboard object. c1 has been set to null and c3 is referencing a null when we call c1.go(c2) . Therefore they are both (c1 and c3) are no longer referenced in memory and are eligible for garbage collection.

Miles
  • 110
  • 9
0

I think the right answer actually is only one object will be eligible for Garbage Collection after the line "// do stuff".

In that code only 2 objects were created and then c1 and c2 refer to them. After the line "c1 = null;" one object immediately becomes eligible for GC, though second object still accessible via c2 ref.

UPDATED:

I missed one detail - each call new CardBoard() creates two object, so call c1 = null makes eligible CardBoard and his nested Short member for GC

Alexey
  • 1,198
  • 1
  • 15
  • 35
  • I agree with you, i missed that only two CardBoard objects were instantiated, c3 = c1.go(c2) doesn't instantiate any new objects. So in this case c1 == null when //dostuff is reached, and so it will be elegible for garbage collection (that's one object), that implies that Short story = 200 inside c1 will be elegible for garbage collection (that's the second object). – Sherekan Mar 18 '16 at 12:29
  • Actually i'm wrong too. Right explanations are here http://stackoverflow.com/questions/11564137/objects-eligible-for-garbage-collection?rq=1 – Alexey Mar 18 '16 at 12:30
  • @Alexey Wow I didn't know that this cardboard question was already asked. Sorry. I don't know how to check an entire code if it's been asked already. – Robert Malansangan Mar 18 '16 at 12:35
  • 1
    The `Short` member may or may not be eligible for GC. So the correct answer is that we don't know. – biziclop Mar 18 '16 at 12:42