• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Garbage Collection Question

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the K&B SCJP study guide book there is a question related to garbage collection. I have a doubt or you can say i have a question regarding the textbook answer so i need an explanation.


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

Question: When //doStuff is reached, how many objects are eligible for Garbage Collection ?
My answer is 3. The objects i think eleigible are c1, the Short object associated with c1 and c3.

But according to the book only 2 objects are eligible: c1 and the Short object associated with it.

So i need an explanation that 'Why is c3 not eligible for gc since it references a null object that it obtained from the method go'.

Thanks
Abhi
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Abhi Bhutani:


So i need an explanation that 'Why is c3 not eligible for gc since it references a null object that it obtained from the method go'.



c3 is not eligible for garbage collection because it does not refer to any instance of an object.

c3 is just a reference to null, so nothing can be garbage collected.
[ August 26, 2008: Message edited by: Mario Minutolo ]
 
author
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, please check the errata for the K&B book. As written, the Short object also can't be GC'ed because it is autoboxed, and has a value which is cached.

In the new version, the Short reference is assigned a value of 200.

Henry
 
Abhi Bhutani
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All clear now. Thanks
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic