wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes objects eligible for GC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "objects eligible for GC" Watch "objects eligible for GC" New topic
Author

objects eligible for GC

abi hari
Greenhorn

Joined: Jan 23, 2007
Posts: 6
Hi,

I see this question in the self-test of K&B.

class cardBoard{
Short story = 5;
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
}
}

The question is, when //doStuff is reached how many objects are eligible for garbage collection?
a.0
b.1
c.2
d.compilation fails
e.it is not possible to know
f.runtime exception

In this case, won't we take c1, c3, cb as objects that can be garbage collected. The answer says c.2 is correct and the explanation says c1 and the associated Short wrapper object are eligible for GC.

I don't understand how to identify objects for GC. Thanks to let me know the same.

Best Regards,
Abirami
Xyz Abc
Greenhorn

Joined: Aug 24, 2007
Posts: 27
From what I can remember, an object is eligible for garbage collection only after all of its references are set to null. Notice the word "eligible", does
[ December 01, 2007: Message edited by: Xyz Abc ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Amy Cha:
From what I can remember, an object is eligible for garbage collection only after all of its references are set to null.

...or are unreachable by the running code.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Here is the code with comments. Note that:
  • Each instance of cardBoard HAS-A Short.
  • The method go simply returns a null reference. It does not affect the object used as an argument. (So calling c1.go(c2) does not do anything to c1 or c2.)
  • ahmed yehia
    Ranch Hand

    Joined: Apr 22, 2006
    Posts: 424
    This example has been corrected in the errata, and Short story = 200; not 5. So that each CardBoard object 'c1' 'c2' would contain different 'story' object instead of refering to only one object in the pool in case it were 5.
    And as far as I know real exam questions about GC wont involve pool objects.
    Xyz Abc
    Greenhorn

    Joined: Aug 24, 2007
    Posts: 27
    I just got more confused by your answer. What is 'story' in this case. Is it Short story=5? And why isn't object 3 garbage collected. Isn't it set to null now?

    [ October 05, 2007: Message edited by: Amy Cha ]
    [ October 05, 2007: Message edited by: Amy Cha ]
    Xyz Abc
    Greenhorn

    Joined: Aug 24, 2007
    Posts: 27
    i bet i need to read the chapter again :roll:
    ahmed yehia
    Ranch Hand

    Joined: Apr 22, 2006
    Posts: 424

    cardBoard c3 = c1.go(c2); //c3 set to null

    Here 'c3' is Not an object, just reference variable.
    Remember to create an object you have to use 'new' to instantiate it.
    Xyz Abc
    Greenhorn

    Joined: Aug 24, 2007
    Posts: 27
    cardBoard c3 = c1.go(c2); //c3 set to null


    Here 'c3' is Not an object, just reference variable.
    Remember to create an object you have to use 'new' to instantiate it.


    I was wondering now since you said c3 is not an object which is right, it's just a reference variable which we are setting to null. However why is Short story=5 garbage collected? Is it because it isn't used anywhere in the program and is termed unreachable?
    [ December 01, 2007: Message edited by: Xyz Abc ]
    ahmed yehia
    Ranch Hand

    Joined: Apr 22, 2006
    Posts: 424

    why is Short story=5 garbage collected? Is it because it isn't used anywhere in the program and is termed unreachable?

    This is because its unrefrenced from anywhere, so it become eligible with its enclosing object.
    However its not always the case, an enclosing object could become eligible while it has an uneligile object.
    marc weber
    Sheriff

    Joined: Aug 31, 2004
    Posts: 11343

    Originally posted by Ahmed Yehia:
    This example has been corrected in the errata, and Short story = 200; not 5. So that each CardBoard object 'c1' 'c2' would contain different 'story' object instead of refering to only one object in the pool in case it were 5.
    And as far as I know real exam questions about GC wont involve pool objects.

    Good point! I missed that.

    With respect to questions about c3, note that c3 is never set to reference any object. Therefore, setting it to null does not make any object eligible for garbage collection.
    Septimiu Pelau
    Greenhorn

    Joined: Feb 16, 2007
    Posts: 8
    some more clarifications.

    class cardBoard {
    Short story = 5; //Short wrapper object in each CardBoard instance
    CardBoard go(CardBoard cb) {
    cb = null; //sets local "cb" reference to null. Note that cb is a copy of reference c2. Setting cb to null does not imply that c2 is also set to null. However you could modify the object 2 using cb reference as both references (cb and c2) refer the same object.
    return cb; //returns null reference
    }

    public static void main(String[] args) {
    CardBoard c1 = new CardBoard(); //object 1 referenced by c1
    CardBoard c2 = new CardBoard(); //object 2 referenced by c2
    CardBoard c3 = c1.go(c2); //here the argument is C2 reference but a copy of it is being passed to go() which returns null.
    c1 = null; //c1 set to null, so object 1 and its Short are eligible.
    // do stuff
    object 2 is still being referred by c2. c3 is not an object, just an object reference therefore is not eligible.
    }
    }


    _ ____ _ <br />Regards,<br />Septimiu
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: objects eligible for GC
     
    Similar Threads
    Garbage collector
    Doubt about garbage collection
    Garbage collector
    SCJP for Java 6, Garbage Collection question
    Garbage Collection