wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes GC eligibility Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "GC eligibility" Watch "GC eligibility" New topic
Author

GC eligibility

Lucia Short
Greenhorn

Joined: Aug 19, 2007
Posts: 13
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

Joined: Aug 20, 2007
Posts: 30
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

Joined: Mar 19, 2007
Posts: 99

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 ]

MooN
Lucia Short
Greenhorn

Joined: Aug 19, 2007
Posts: 13
Thank you very much, Neha!
Neha Bhattad
Ranch Hand

Joined: Aug 20, 2007
Posts: 30
Thanks Al Mamun. That link really helped me understand immutability of wrapper objects.
Lucia Short
Greenhorn

Joined: Aug 19, 2007
Posts: 13
Thanks Al Mamun.
Now I will never forget this.
Abdullah Mamun
Ranch Hand

Joined: Mar 19, 2007
Posts: 99

Hi Neha and Lucia

This is my pleasure
[ August 20, 2007: Message edited by: Al Mamun ]
 
 
subject: GC eligibility