Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

objects eligible for GC

 
abi hari
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 424
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 27
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 27
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    i bet i need to read the chapter again :roll:
     
    ahmed yehia
    Ranch Hand
    Posts: 424
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    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
    Posts: 27
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 424
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    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
    Posts: 11343
    Java Mac Safari
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 8
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
    }
    }
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic