wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection question from K&B 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 "Garbage Collection question from K&B" Watch "Garbage Collection question from K&B" New topic
Author

Garbage Collection question from K&B

Ansar Shah
Greenhorn

Joined: May 02, 2007
Posts: 29
Hello
From K&B book; Chapter 3, page 266


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
} }

When // doStuff is reached, how many objects are eligible for GC?
A. 0
B. 1
C. 2
D. Compilation fails.
E. It is not possible to know.
F. An exception is thrown at runtime.

Answer:
3 C is correct. Only one CardBoard object (c1) is eligible, but it has an associated Short
wrapper object that is also eligible.
A, B, D, E, and F are incorrect based on the above. (Objective 7.4)


To my understanding all the three objects(c1,c2,c3) are set to null. Then how c1 is eligible for GC. May be I am missing something. Please elaborate.

Thanks
Fedry Kemilau
Ranch Hand

Joined: Aug 06, 2007
Posts: 44
Hi Ansar Shah,

Actually there is a post about this question already.
First object that is eligible for GC is obviously c1; when it was set to null.
The second object is c3 which is assigned to null. (c1.go(c2) return null)

Here, c2 is set to null only in the go() method scope, after the method finished, c2 is back to its previous value. Simply said, c2 is null only while c1.go(c2) is called.

Correct me if I were wrong.

You can check it with this code: (put it after //do stuff)


Check the output!!!

Cheers,

Fedry
[ August 13, 2007: Message edited by: fedry kemilau ]

Solve this code:<br /> <br />10010101100001111011011000010100000111001011011111100011110101111100110100001
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
1. CardBoard c1 = new CardBoard();
>> Two objects are clreaetd.
a)CardBoard
b)Short story(inside CarBoard)

2.CardBoard c2 = new CardBoard();
>> Two objects are clreaetd as above.

3.CardBoard c3 = c1.go(c2);
>> c2=null; and observe go method returning null;
>> No Objects created.
Corrected error:
*** c2 is still poiting to its object

4. c1 = null;
>> c1 also marked eligible for garbage Collection.

Only four objects created and one object marked eligible for GC.
we didn't mark any of Short objects to become eligible for GC
Hence answer is 1 objects only

[ August 13, 2007: Message edited by: Srinivasan thoyyeti ]
[ August 13, 2007: Message edited by: Srinivasan thoyyeti ]

Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14432
    
  23

Fedry, regarding your signature...:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Java rocks!
Fedry Kemilau
Ranch Hand

Joined: Aug 06, 2007
Posts: 44
Lol
Yeah... Java rocks!!
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Originally posted by Ansar Shah:
Answer:
3 C is correct. Only one CardBoard object (c1) is eligible, but it has an associated Short
wrapper object that is also eligible.
A, B, D, E, and F are incorrect based on the above. (Objective 7.4)
No. In this sample there is only one object eligible for garbage collection. For details have a look here.
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Manfred Klug,

Thanks for correction.

Java has call by value only. No call by reference.
I will meditate on this.



[ August 13, 2007: Message edited by: Srinivasan thoyyeti ]
 
 
subject: Garbage Collection question from K&B