• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection

 
Mckenzie John
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 333
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mckenzie John
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Himai Minh
Ranch Hand
Posts: 1255
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1255
6
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Kevin Florish
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1255
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic