| Author |
K&B SCJP6 Chapter 3- Q11 a GC question
|
Oren Ben
Greenhorn
Joined: Nov 19, 2008
Posts: 6
|
|
Hi All i hope someone could help me with the following question class Beta {} class Alpha { static Beta b1; Beta b2; } public class Tester { public static void main (String [] args){ Beta b1 = new Beta(); Beta b2 = new Beta(); Alpha a1 = new Alpha(); Alpha a2 = new Alpha(); a1.b1=b1; a1.b2=b1; a2.b2=b2; a1=null; b1=null; b2=null; // line 16 --do stuff } } the Question is : When line 16 is reached . how many objects will be eligible for garbage collection ? The Answer is 1 . =================================================================== i personlly see more than 1 object that is eligible for GC for example : b2 , b1 Why am i wrong ? can someone please explain me the answer for that question?
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16687
|
|
Why am i wrong ? can someone please explain me the answer for that question?
References are not garbage collected -- objects are. The b1 and b2 references are nulled, but are the objects that they use to reference, reachable or not? Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Abhi vijay
Ranch Hand
Joined: Sep 16, 2008
Posts: 509
|
|
In case of the object referenced by b2, it is not eligible for GC, because it has an active reference a2.b2. when a1=null, so 1 object for GC. So when a1=null, will the references to b1,b2 be lost?? i.e. The objects referenced by b1,b2 will also be eligible for GC? In that case, b1 will also be elegible for GC.
|
 |
James Tharakan
Ranch Hand
Joined: Aug 29, 2008
Posts: 580
|
|
Hi, I think the confusion is caused because of the static b1 reference of class Alpha. Basically b1 is a class's property. not object. If a1.b1 is assigned a object then it means a2.b1 is also assigned a object. Check the print statement in the below code. Hope it clears the doubt. I have added a constructor for better understanding. Output:- james So the object that would be GC is ONLY a1's object [ November 19, 2008: Message edited by: James Tharakan ] [ November 19, 2008: Message edited by: James Tharakan ]
|
SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
|
 |
Oren Ben
Greenhorn
Joined: Nov 19, 2008
Posts: 6
|
|
thanks All for your replies thanks James for your static point that i missed and it is really important . but what about the 2 Beta Objects with it's 2 references b1, b2. Beta b1 = new Beta(); Beta b2 = new Beta(); why aren't they called Objects? (lets put the Alpha class issue a side )
|
 |
James Tharakan
Ranch Hand
Joined: Aug 29, 2008
Posts: 580
|
|
@ Oren Ben
Beta b1 = new Beta(); Beta b2 = new Beta(); why aren't they called Objects?
Who said there are not objects.... They are objects.(Created by using new operator). If your question is why they are not deleted its because they are still refered. b1 is refered by a2.b1 b2 is refered by a2.b2
|
 |
Oren Ben
Greenhorn
Joined: Nov 19, 2008
Posts: 6
|
|
Thanks Man , now i understand it all
|
 |
Vaishali Sutaria
Greenhorn
Joined: Jun 16, 2009
Posts: 6
|
|
Hi,
I am Vaishali.
I am following this example.
My doubt is when we say only a1's object is eligible for GC, do we mean Alpha object is gced or a1.b2 is gced?
If we say a1 object as whole is Gced then why the count[Total no of objects eligible for GC] is not 2 as one is Alpha object that has one Beta object as its Instance.
I see two examples from K&B book Q1 and Q10 Chapter 3.
Q1. Here one Cardboard object is eligible for GC which has Short Wrapper Object so count=2.
Q10. Here one Dozen object is eligible for GC which has an array so count=2.
Someone please help. I am badly stuck here.
Sorry if I still need to understand some concepts right.
Thanks a lot.
|
 |
Richard Kuehner
Greenhorn
Joined: Apr 11, 2010
Posts: 3
|
|
Hello:
I realize it's several months later, but did anybody ever answer the question from Vaishali? I am confused about the exact same thing.
I understand that a1 is assigned null, so the object it was attached to is eligible for GC. What confuses me is, what about a1.b2? Isn't that a separate object that would be eligible for GC also, making two objects GC? Apparently a1.b2 is not counted as a second object eligible for GC. Is it because it is still attached to b1? Is it because it is a reference variable, not actually an object? (I'm new at this, and I'm having a hard time getting my brain around the difference between reference variables and objects.)
If anybody could help, I would greatly appreciate it.
Thanks.
|
 |
trivikram rammurthy
Greenhorn
Joined: Feb 03, 2010
Posts: 18
|
|
okay....i think there is a bit of confusion between objects and references here . a1.b2 is not an object and is a reference. you assign it to an object .
a1.b2 = b1 ;//It means that a1.b2 points to an object that is already referenced by b1 . a1.b2 by itself IS NOT AN OBJECT
so when you make a1 = null , it means that you are just removing of the reference and since the object earlier referenced by b1 can still be accessed by a2.b1 it will not be eligible for garbage collection.
NOTE : if they had inserted a code a1.b2 = new Beta () ; instead of "a1.b2 =b1" Then there would have been two objects for GC .
Hope this helps
|
 |
vaibhav teli
Greenhorn
Joined: Apr 14, 2010
Posts: 6
|
|
im guessin using the ClassName.ref format for a static reference in this case Alpha.b1 would clarify things more clearly..
|
OCPJP 6.0- 90% OCPWCD 5.0- 97%
|
 |
Bert Bates
author
Sheriff
Joined: Oct 14, 2002
Posts: 8712
|
|
Hi Guys,
Sun thought (we'll see if Oracle does too) that GC was an important concept, so it's on the exam.
The important concepts are:
- what's an object (and only objects get GCed)
- what's a reference to an object
- a single object can have multiple references
If you have those concepts really clear in your mind, you should be able to draw pictures for these kinds of questions to determine the objects, their various references, and therefore what's eligible for the GC.
Of course in the real world you might be fixing someone else's code, and they might not use something like the 'static' nomenclature mentioned here, so you need to be able to spot references as they appear "in the wild"
hth,
Bert
|
Eliminate fossil fuel subsidies. (If you're not on the edge, you're taking up too much room.)
|
 |
Kurt Zhu
Greenhorn
Joined: Aug 25, 2011
Posts: 8
|
|
I think we can just try to adjust ourselves to the way JVM works. We can use the results of the code as what we should adjust ourselves to.
Thanks to @Ankit Garg @James Tharakan and others.
|
 |
 |
|
|
subject: K&B SCJP6 Chapter 3- Q11 a GC question
|
|
|