Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

garbage collection

 
Raymond Chamberglain
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm confused with the answer to this question. This question is question one from chapter 3's self test section. It asks that when '// do stuff' is reached, how many objects are eligible for GC?



I know c2 is not null because it is referencing to a CardBoard object. c3 is referencing to the cb reference variable (Is this correct?). c1 is null. But how does the Short wrapper object becomes eligible for GC? The answer says "it has an associated Short wrapper object that is also eligible." what is 'it'? c1? Please help.. My mind is doing an infinite loop and about to blow up the stack.. :P Thanks.
 
kaushik vira
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Raymond,

Your base assumption is wrong here.

know c2 is not null because,it is referencing to a CardBoard object.


if you look in to go method implementation.. when you call c2 become GC eligible.

4 Object is eligible for GC - 2 CardBoard and 2 Short object


Please correct me rancher if I am wrong..
 
Raymond Chamberglain
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer is 2, but I don't know where one of them comes from.

From what I understand, there are 2 cardboard objects and 2 Short wrapper objects (created when the two cardboard objects are initialized. Let's call cardboard initialized to c1 is A and the other B.
by the time // dostuff is reached, c1 and c3 is null. since c1 is null, cardboard A has no more references referring it. c2 is still referring to cardboard B, because go() method doesn't use the c2, instead it creates a copy of the c2 variable. However the cb variable in go() method is referring to cardboard B. Regardless, cb is null when // dostuff is reached.

So it goes like this:

c1 -> cardboard A.
c2, cb -> cardboard B.
c3 -> null

When // dostuff is reached, c1 and cb becomes null. But c2 still exist. So cardboard A and its Short wrapper is eligible for GC. Is this the right explanation? Sorry that I kinda go around in circles. I am still trying to sort my thoughts. lol..
 
Henry Wong
author
Marshal
Pie
Posts: 20894
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When // dostuff is reached, c1 and cb becomes null. But c2 still exist. So cardboard A and its Short wrapper is eligible for GC. Is this the right explanation?


Yes.


And BTW, next time, please mention that K&B is the source. Saying chapter 3, is not specific enough.

Henry
 
Mo Jay
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry kaushik vira but your answer is totally wrong.

I suggest that you read Raymond Chamberglain explanation as he seems to get the answer correctly.

Cheers!!!
 
Pooja Prakash
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Raymond's explanation is perfectly clear. I had misunderstood the explanation in K&B book but now i've got it all sorted. Thanks Raymond

Cheers,
-Pooja
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic