• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

islands of isolation

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all;
see code in K&B book:
public class island{
island i;
public static void main(String [] arg)
{
island i2=new island();
island i3=new island();
island i4=new island();
i2.i=i3;
i3.i=i4;
i4.i=i2;
i2=null;
i3=null;
i4=null;
.
.
}
}
My question is "are last 3 statements necessary for three objects to be eligible for GC?
Please explain me in detail "islands of isolation"
thanks,
Rajiv
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rajiv,
Yes, the last three statements are necessary to make 3 objects eligible for Garbage collection.
After execution of the following code :
island i2=new island();
island i3=new island();
island i4=new island();
Three objects will be created. So in the memory will be allocated to these three objects and this memory area will be denoted by name say... A, B, and C.
now ref variable i2 ===> A (ref var i2 points to memory area A and so on)
and ref variable i3 ===> B
and ref variable i4 ===> C
After execution of statments :-
i2.i=i3;
ref var i2 ===> A and i2.i ===> B
i3.i=i4;
ref var i3 ===> B and i3.i ===> C
i4.i=i2;
ref var i4 ===> C and i4.i ===> A

After execution of the last three statements :-
i2=null;
ref var i2 ===> null (i2 no more points to memory area A and B & eligible for GC)
i3=null;
ref var i3 ===> null (i3 no more points to memory area B and C & eligible for GC)
So after execution of the above two statments only memory area B is eligible for Garbage collection as it is not referenced by any variable.
i4=null;
This statement is very important to make the remaining two areas denoted by memory area A and C to be eligible for Garbage collection because
i4 as whole points to memory area C and
i4.i points to memory area A
So after execution of the last statement, memory area A and C will also be freed and all the three will be eligible for garbage collection...
I hope this solves your query. Pls correct me if i am wrong anywhere.
 
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rajiv and Manish,
I'm still working out garbage collection and so forth, so this question intrigued me.
Manish's explantion is good, but misses a few points.


i2=null;
ref var i2 ===> null (i2 no more points to memory area A and B & eligible for GC)
i3=null;
ref var i3 ===> null (i3 no more points to memory area B and C & eligible for GC)


After i2=null is executed, A.i will still reference B.
Likewise, after i3=null B.i ==> C and after i4=null C.i ==> A
Yes, the references i2, i3 and i4 are pointing to null, but the
objects A, B and C still reference each other.
As such, it would seem that they are not eligible for gc, however; they have no external reference (creating an 'island of isolation'), they are unreachable and therefore eligible for gc.
But the question remains: is setting each reference to null neccessary for the three objects to be eligible for gc?
My answer: No. To become eligible for gc, an object needs to become unreachable. How that happens is somewhat irrelevant, what's important is that the object no longer has any external references.
Consider the following modified code:

output:


creating: Island@126b249
creating: Island@182f0db
creating: Island@192d342
creating: Island@6b97fd
finalizing: Island@182f0db
finalizing: Island@192d342
finalizing: Island@6b97fd


from the output, we can surmise that i wasn't eligible for gc at the time of the explicit System.gc call at line 3, but i2, i3 and i4 were. i will be eligible for gc after line 4, but the program doesn't last long enough for the message to print.
It is instructive to note that if m1 is modified to return an Island and returns i2, i3 or i4, and line 2 is altered to i = i.m1();, then only the object initially referenced by i will be gc'd at line 3
Hope that helps,
Ray
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ray Stojonic:
After i2=null is executed, A.i will still reference B.
Likewise, after i3=null B.i ==> C and after i4=null C.i ==> A
Yes, the references i2, i3 and i4 are pointing to null, but the
objects A, B and C still reference each other.
Ray[/QB]


Is it correct to say
'A.i will still reference B.'
since you called A as the object and i2 as the reference
or it is better to say
'i2.i will still reference B.'
even if also this seems to me not complete because i2 is already null.
 
Ray Stojonic
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Leonardo,
It is correct to say A.i will still reference B.
Consider this:
i2 ==> (A(i ==> B))
i2 refers to A, which has reference i, which refers to B
i2.i isn't the Reference i that i2 has, rather, i2.i is the Reference i that the Object A has, which i2 happens to currently refer to.
i2 ==> null | (A(i ==> B))
i2 is set to null, but A still exists and has reference i, which refers to B.

Hope that answers your question.
Ray
 
Rajiv Goyal
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
thanks all for the answers!
rajiv
 
please buy this thing and then I get a fat cut of the action:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic