• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how many objects are eligible for GC?

 
Rahul mir
Ranch Hand
Posts: 38
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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 ...
 
Prajakta Acharya
Ranch Hand
Posts: 138
Java Spring
  • Mark post as helpful
  • send pies
  • 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 Java Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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.
 
Kisor Biswal
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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.
 
Henry Wong
author
Marshal
Pie
Posts: 21023
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • 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!
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic