Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GC eligibility

 
Lucia Short
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there:

I run on a question in Self Test that I answered right, but it seems that for different reason in the book.

The question is -

**********************************************************
Given:
class CardBoard {
Short story = 5;
CardBoard go (CardBoard cb) {
cb = null;
return cb;
}
public static void main() {
CardBoard c1 = new CardBoard();
CardBoard c2 = new CardBoard();
CardBoard c3 = c1.go(c2);
c1 = null;
// do Stuff
}}

When // doStuff is reached, how many objects are eligible for GC?
********************************************

The answer is 2. Could you please explain me why?

Thanks a lot!!!
 
Neha Bhattad
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lucia,

When c1 is set to null its object becomes available for the GC. Also, since c1 contains a wrapper object story, that object will also be eligible. So in all there are 2 objects for GC.

As far as c2 goes:-
It is passed to the method go but it is the cb reference that is set to null. So the c2 object & reference still exist.

Hope this helps!
 
Abdullah Mamun
Ranch Hand
Posts: 99
Eclipse IDE Mac
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Neha Bhattad:

...since c1 contains a wrapper object story, that object will also be eligible...

Little misconception remains in this statement regarding Wrapper objects...
Only one object will be eligible for garbage collection. For detail look here
[ August 20, 2007: Message edited by: Al Mamun ]
 
Lucia Short
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much, Neha!
 
Neha Bhattad
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Al Mamun. That link really helped me understand immutability of wrapper objects.
 
Lucia Short
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Al Mamun.
Now I will never forget this.
 
Abdullah Mamun
Ranch Hand
Posts: 99
Eclipse IDE Mac
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Neha and Lucia

This is my pleasure
[ August 20, 2007: Message edited by: Al Mamun ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic