• Post Reply Bookmark Topic Watch Topic
  • New Topic

Isolating a Reference  RSS feed

 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Can anyone visualise the above code in heap and explain me which objects are eligible for Garbage Collection.

Thank you
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing you might try to do as a learning exercise is to map it out, see what it looks like, and make a determination.

Each time one is created, draw a reference variable pointing to a circle or something representing the object. If it has a reference inside that got set via a constructor, go ahead and make it point to the object that it is set to. Later, when you get to where the class member references are set to point to each other, just draw the arrows. When a reference is nulled just erase or scribble it out. Then, see what is left and think like a "thread". "Do I have any way that I can get to that object? No? Then it is eligible for GC.

DO a couple of these and it will all make sense.
 
Himalay Majumdar
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


As per your statement. I think i3 and i4 are eligible for GC. Please correct me if I went wrong.

Thanks
Himalay
[ December 10, 2008: Message edited by: Himalay Majumdar ]
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that you are correct. As both i3 and i4 are made null. So, there is no reachable reference to the objects created for i3 and i4. Though i3 internally refers to i4, but as i3 cannot be reached, i4 also can't be. i2 reference is still referring to the object created for i2. So, only i3 and i4 are eligible for GC.
 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,

Actually i3.i is pointing to i4 object, then how i4 object will be eligible for gc?
if any object is having a single reference then it cant be eligible for gc.

please correct me...if i am wrong.

Thanks
Preetha
 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per my Knowledge.....

Object1 ---------i2,i4.i

Object2 ---------i3

Object3----------i4,i3.i

if i3=null then Reference variable i3.i is also become null,so

Object1 ---------i2,i4.i

Object2 ---------

Object3----------i4

similarly if i4=null

Object1 ---------i2

Object2 ---------

Object3----------

finally 2objects are eligible for garbage collection which are i3 and i4...
 
Bob Ruth
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When looking for GC eligibility what you should look for is a reference that any runnable thread can use to reach a specific object. Your observation that i3.i points to i4 is true. But there is no reference pointing to i3 that a thread can use to reach i3 so, if a thread can not reach i3 then it follows that a thread can not reach i4.
 
Rajshekhar Paul
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Preetha
There are two pssible ways to reach the object created for i4 -
1. through the i4 reference variable. but it is made null.
2. through i3 as i3 is referring to i4 internally. But i3 itself is made null, that means by i3 there is not way to reach i4.
So, there is basically no other way to reach the object created for i4 from the outside world of heap. That means no live thread can reach that object now. Thats why the object created for i4 is also eligible for GC.
 
Himalay Majumdar
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I Agree with all, There is no way a runnable thread can reach i3 and hence i4 that makes both of them eligible for GC. i2 is the future KING
[ December 11, 2008: Message edited by: Himalay Majumdar ]
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can "test" it by running the following code:



But remember that you can not force the jvm to gc. You can only ask it.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!