• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Garbage Collection question from K&B

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fedry, regarding your signature...:
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java rocks!
 
Fedry Kemilau
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lol
Yeah... Java rocks!!
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic