Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes 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 "Garbage Collection" Watch "Garbage Collection" New topic
Author

Garbage Collection

Mckenzie John
Greenhorn

Joined: Sep 21, 2012
Posts: 16
Hi,

Can some one explain me the below code:

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;

}
}

Also i have a clarification needed regarding my understanding of Garbage collection - can i say that an object which has no link to a reference variable in the stack be considered eligible for GC, even though there is an instance within another object pointing to the same(This will be on the heap)?
Please explain me.

Thanks
Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

Mckenzie John wrote:Hi,

Can some one explain me the below code:

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;

}
}

What don't you understand in it mention that.

Also i have a clarification needed regarding my understanding of Garbage collection - can i say that an object which has no link to a reference variable in the stack be considered eligible for GC, even though there is an instance within another object pointing to the same(This will be on the heap)?
Please explain me.

Thanks

Yes, it will be eligible.


OCA7
Mckenzie John
Greenhorn

Joined: Sep 21, 2012
Posts: 16
Which of the objects will be eligible for garbage collection at the end of this program and why. i just want a very brief explanation step by step which can help me understand the concept
Kevin Florish
Ranch Hand

Joined: Jan 06, 2009
Posts: 175
Objects become eligible for GC when their scope ends and there are no live references to them or they become unreachable.

In your code above at the end you are setting a1,b1 and b2 to null.
so:

after the assignments and null settings

object a1 State: b1 = null, b2 = null and the a1 object has been set to null - so a1 is eligible for GC as there are no live references

object b1 has been set to null and is no longer referenced in a1 - so a is eligible for GC as there are no live references

object a2 is still referenced - so not eligible for GC

Although b2 is set to null it still has a live reference in a2 state and so is reachable - so not eligible for GC

I think this is correct, not 100% on it though.

The following slideshow from my website might help with understanding The Heap


Java5 Tutorials | Java6 Tutorials | HTML/CSS Tutorials | JavaScript and jQuery Tutorials
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 741
I think static Beta b1 is not eligible for GC because static reference belongs to a class, not any object instance.

Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 741
Kevin Florish
Ranch Hand

Joined: Jan 06, 2009
Posts: 175
Himai Minh wrote:I think static Beta b1 is not eligible for GC because static reference belongs to a class, not any object instance.


Don't reference variables by their nature refer to objects, how can we have a static reference to an object?

Isn't the static initializer block just putting some memory addresses on the stack before any instances are created and then using these?

I would really love a definitive answer on this one.

GC questions like these always give me a headache
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 741
Hi Kevin,
Maybe I don't say it right.
Static reference means those reference refering to static (class level) object.

Static initializer block initialize those static reference which refers to class level objects. Static intializer block is run before any contructor that create object instance.

In GC issues, if an object is class level, it can be refered by ClassName.ObjectReference. So, if the instances of the ClassName is GCed, the ClassName.ObjectReference is not GCed.
 
Don't get me started about those stupid light bulbs.
 
subject: Garbage Collection