• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

GC eligibility

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!!
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 99
Mac Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Lucia Short
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much, Neha!
 
Neha Bhattad
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Al Mamun. That link really helped me understand immutability of wrapper objects.
 
Lucia Short
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Al Mamun.
Now I will never forget this.
 
Abdullah Mamun
Ranch Hand
Posts: 99
Mac Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Neha and Lucia

This is my pleasure
[ August 20, 2007: Message edited by: Al Mamun ]
 
Hey, check out my mega multi devastator cannon. It's wicked. It makes this tiny ad look weak:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic