• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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?
 
Marshal
Posts: 28260
95
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: 13089
67
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.
 
I do some of my very best work in water. Like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic