aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes problem in garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "problem in garbage collection" Watch "problem in garbage collection" New topic
Author

problem in garbage collection

rooneywayne kumar
Greenhorn

Joined: Jul 08, 2009
Posts: 1
hi all,
can anyone tel me how?

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;

//do stuff
}
}
results in 1 object for garbage collection by the time //do stuff is reached

Jacob Sonia
Ranch Hand

Joined: Jun 28, 2009
Posts: 174
Hi, The best way to solve GC i feel till now is by drawing diagrams

see
we have this picture

b1 -----> new Beta()
a1.b1 ------->
a1.b2 -------->

implying all three point to the same object

b2 ------> new Beta()
a2.b2 -------->



a1 --------> new alpha()



a2 -------> new alpha()


after setting thigs to null it becomes like this


b1 ---X--> new Beta()
a1.b1 ------->
a1.b2 -------->

implying all three point to the same object

b2 ----X--> new Beta()
a2.b2 -------->



a1 ----X----> new alpha()



a2 -------> new alpha()


so we see a1 is not pointing to new alpha() and there is no other reference pointing to it. So it is eligible for GC.

Hope you got things.
Mo Jay
Ranch Hand

Joined: Feb 16, 2009
Posts: 83

Ok, the object that is eligible for garbage collection here is the object referred to it by a1 because there is NO reference to it from the stack or from within the heap.

Note that there is ONLY one object in the heap now that has an outside reference in the stack and that's the object referred to by a2, then this object in turn refers to 2 objects in the heap through a2.b1 and a2.b2: one that had b1 reference and second had b2 reference before they were nulled. Even though there is no explicit statement in the code that says a2.b1=b1 BUT because b1 (in a2.b1) is static means that there is one copy of it and it is pointing to what a1.b1 is pointing to (which is the object with b1 pointing to). So we have a1.b1 and a2.b1 point to the same object was pointed to by b1.

It will help you a little bit to see this if you draw some graphs with the heap and the objects created in them with arrows pointing from the references.

I hope this clarifies your question.

Cheers!!!
Jacob Sonia
Ranch Hand

Joined: Jun 28, 2009
Posts: 174
But i have one big doubt here, when i do a1 = null isn't it that a1.b1 and a1.b2 should also be nulled. How can i reference them from a1 which is null
Mo Jay
Ranch Hand

Joined: Feb 16, 2009
Posts: 83
In this case you cannot reference them through a1 because a1 is null already, in this case you need to use some API methods like intern() to get access to them directly in the heap or may be through a2 if it is possible.

Note, a1 reference in stack and a1.b1/a1.b2 are totally 2 different things, a1 in (a1.b1) is the instance of object alpha created in the heap with b1 as a copy of the instance variable. they are different from a1 reference which is in the stack and pointing to the object in the heap.

Cheers!!!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14342
    
  22

Welcome to JavaRanch, "rooneywayne kumar".

Please check your private messages for an administrative matter from JavaRanch.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Vaishali Sutaria
Greenhorn

Joined: Jun 16, 2009
Posts: 6
Hi,
I am Vaishali here.
I see that object referred by a1 is eligible for GC. But it has b1(static) and b2.
So, here when we say only 1 object is eligible for GC, is it like a1.b2 is for gced?
My doubt is if a1 is gced , why not b2(beta object it has as Instance) as well?
Excuse me if I am mistaken for some facts which I still need to know.
Thanks.
Mo Jay
Ranch Hand

Joined: Feb 16, 2009
Posts: 83
o, here when we say only 1 object is eligible for GC, is it like a1.b2 is for gced?
My doubt is if a1 is gced , why not b2(beta object it has as Instance) as well?


I am really not sure what you mean exactly, but I just want to say that when we say 1 object is eligible for GC we mean the entire object including any instance variables and data in it. In this case when we said a1 is GCed that includes a1.b2 because it is part of it as you stated above.

Cheers!!!
Vaishali Sutaria
Greenhorn

Joined: Jun 16, 2009
Posts: 6
Hi There,
Thanks for that.
Actually, I was also referring the following Link on other example of GC. Here 2 objects(Cardboard object and its Short Wrapper Instance) are considered for GC.
Though Short Object is Instance of Cardboard object, We say two objects are eligible for GC.

http://www.coderanch.com/t/449628/Programmer-Certification-SCJP/certification/garbage-collection#2001151

And in the example discussed here, it is not like that.
Regards,
Vaishali.
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
Mo Jay wrote:I am really not sure what you mean exactly, but I just want to say that when we say 1 object is eligible for GC we mean the entire object including any instance variables and data in it. In this case when we said a1 is GCed that includes a1.b2 because it is part of it as you stated above.

I don't agree to this......

If an object is available for GC, and it has an instance variable which is another object on the heap, then we say 2 objects are available for garbage collection.

Can someone confirm this........


SCJP 6
How To Ask Questions On Java Ranch - How To Answer Questions On Java Ranch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem in garbage collection