• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection ... Need Clarification

 
Sumeet Chakraborty
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all. This question is from K & B Book (Chap -3 , Self Test Q No 11).

I need the clarification for the answer given , because its bit confusing for me.



When "do stuff" is reached , how many objects will be eligible for Garbage Collection ?

The answer is 1 (according to the book and obviously its right )
The answer Explanation is : It should be clear that there is still a reference to the object reffered by a2, and that there is still a reference to to the object referred to by a2.b2. (Its fine up till here)
What might be less clear is that you can still access the other Beta object through the static variable a2.b1 -- because its static. (This is where I am confused)


Te above code never assigns anything to a2.b1. But still how can it refer the Beta Object ???

It may be something very simple logic which I am missing ...for sure. But somehow I am getting confused.
Please help ...


 
avi sinha
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sumeet Chakraborty wrote:
Te above code never assigns anything to a2.b1. But still how can it refer the Beta Object ???

what is bets here in line 17??? if it is a class where is the definition???

if it is beta then the explanation may be:

the point is that static members are the properties of the class not of any particular object.that's why there is only one copy of a static member and
that would last until atleast one object of the class is on the heap and has a valid refernce.

here for clarification just consider each object of class alpha has a static member Beta b1.

but when you write a1.b1=b1 both a1.b1 & a2.b1 now refers to b1.

that's why when a1 is set to null a2.b1 is referring to b1. & object b1 of class Beta is not eligible for gc.

avi
 
avi sinha
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
clearly here object 1 topmost here in the heap diagram can be accessed by a2.b1
object 2 2nd from top here in the heap diagram can be accessed by a2.b2
object 3 below object 2 here in the heap diagram can't be accessed
object 4 at the bottom here in the heap diagram can be accessed by a2


avi
Noname.jpg
heap
[Thumbnail for Noname.jpg]
 
Sumeet Chakraborty
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes .. That is Beta in line 17 and not Bets Sorry for the mistake, I have rectified it .

And thanks Avi for the clarification. Its quite clear for me now. Static variables and Garbage collection are the 2 things which are confusing me a lot
 
Bob Wheeler
Ranch Hand
Posts: 317
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sumeet Chakraborty wrote:

the point is that static members are the properties of the class not of any particular object.that's why there is only one copy of a static member and
that would last until atleast one object of the class is on the heap and has a valid refernce.

here for clarification just consider each object of class alpha has a static member Beta b1.

but when you write a1.b1=b1 both a1.b1 & a2.b1 now refers to b1.

that's why when a1 is set to null a2.b1 is referring to b1. & object b1 of class Beta is not eligible for gc.

avi


Thanks Avi, I was jst checking this Q.

rob
 
Pete Delrue
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does it really say in the book on line 21: a1.b1=b1?

That doesnt make any sense,according to me it should say Alpha.b1=b1.
There is no a1.b1 or a2.b1, isnt that the point of the keyword static?
 
avi sinha
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pete Delrue wrote:
That doesnt make any sense,according to me it should say Alpha.b1=b1.


you can access the static member b1 by alpha.b1 but it can also be accessed by an instance of class alpha
as
alpha a1=new alpha();
a1.b1;
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic