Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K&B SCJP6 Chapter 3- Q11 a GC question

 
Oren Ben
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20889
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Oren Ben
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ 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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Man , now i understand it all
 
Vaishali Sutaria
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
im guessin using the ClassName.ref format for a static reference in this case Alpha.b1 would clarify things more clearly..
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Kurt Zhu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic