• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

K&B SCJP6 Chapter 3- Q11 a GC question

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23959
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

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
 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Man , now i understand it all
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 6
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic