• 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

objects eligible for gc

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why isn't c3 eligible for garbage collection in the code below? Doesn't it equate to null after c1.go(c2) returns?

The question below was found in the book: SCJP Sun Certified Programmer for Java 6 Study Guide (Exam 310-065), the first self test question of Chapter 3.

QUESTION:- When // doStuff is reached, how many objects are eligible for GC?

 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What exactly would be GC'd if c3 *were* eligible?
 
Edward Lawrence
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If c3 *were* eligible, I believe that nothing would be guaranteed for GC. However, c1 with its associated Short wrapper and c3 with its associated Short wrapper would be eligible. There would be 4 objects eligible for garbage collection. I am probably totally wrong. Can anyone provide a little insight?
 
Sheriff
Posts: 26962
84
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's the insight you need: Garbage collection applies to objects, not to variables. So asking whether a variable can be garbage collected is meaningless. Look at the objects.
 
Edward Lawrence
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was thinking on the line of your insight until I read the following answer from the book:-

" Only one CardBoard object (c1) is eligible, but it has an associated Short wrapper object that is also eligible." The Short is a wrapper object.
 
lowercase baba
Posts: 13021
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
c3 isn't an object, so it's eligibility for GC is always 'no', whether it points to null or something else.
 
Edward Lawrence
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Fred. That is the fact/concept I have been missing.
 
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