• 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

how many objects are eligible for GC?

 
Ranch Hand
Posts: 38
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question is from K&B SCJP book page no. 269.

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



from my point of view there is only 1 object is eligible for GC, c1. But the explanation given in Book i am not getting.
Books explanation : Only one CardBoard object (c1) is eligible, but it has an associated Short wrapper object that is also eligible..
Please someone explain me ...
 
Ranch Hand
Posts: 138
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess its 3 because c2,c3 and c1 point to null after call to go method.
 
Rahul mir
Ranch Hand
Posts: 38
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prajakta Acharya wrote:I guess its 3 because c2,c3 and c1 point to null after call to go method.



c3 is not referring any Object, after go() method call it holds null value, and c2 reference is not loosing there object so c2 is not eligible for GC. c1 = null, it is ok .. it will go for GC.
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd say that this question is complicated unnecessarily by the Short cache, and that the object reference by c1's story member variable is in fact not eligable for GC.
Questions about GC shouldn't use Strings, or primitive wrapper classes.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rahul mir wrote:This question is from K&B SCJP book page no. 269.

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



from my point of view there is only 1 object is eligible for GC, c1. But the explanation given in Book i am not getting.
Books explanation : Only one CardBoard object (c1) is eligible, but it has an associated Short wrapper object that is also eligible..
Please someone explain me ...



When we call new CardBoard() 2 objects are created. 1st the new CardBoard object and 2nd the instance variable in it. i.e Short.
CardBoard c3 = c1.go(c2); at this line c3 is assigned with null, but there is no change to c2.

c1 = null; at this line we lose reference to the object created at CardBoard c1 = new CardBoard(); so this will be garbage collected along with it's instance variables.
 
author
Posts: 23951
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

Rahul mir wrote:
from my point of view there is only 1 object is eligible for GC, c1. But the explanation given in Book i am not getting.
Books explanation : Only one CardBoard object (c1) is eligible, but it has an associated Short wrapper object that is also eligible..
Please someone explain me ...



You have an very old copy of the book. This question was fixed a long time ago.

In the fixed version, the value being assigned to the Short reference is large enough to be outside of the range of the cache used by autoboxing.

Henry
 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question ought to be made into a sticky thread, I've lost count of the number of times its has been asked here!
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Jelle,

A big part of why GC is on the exam is to get candidates familiar with when objects get created, and those times when you might be making objects without realizing that you're making objects.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic