• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

q on garbage collection

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi i have a doubt in a question in the danchisholm mock exam on garbage collection

class I {
private I other;
public void other(I i) {other = i;}
}
class J {
private void m1() {
I i1 = new I(), i2 = new I();
I i3 = new I(), i4 = new I();
i1.other(i3); i2.other(i1);
i3.other(i2); i4.other(i4);
}
public static void main (String[] args) {
new J().m1();
}}

acc to answer given all the 4 are eligible for garbage collection since each refer to ecah other

but shdnt answer be 0 since initial references i1,i2,i3,i4 havent been nulled and all can still be used to access the objects
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question has come up many times. See this thread (and the thread linked to in the first response there).
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Steven,

It's a trick question.

To answer this question, imagine the following case :

Imagine that you have four friends and everybody are alone in an Island.

Your friend #1 holds the hand of your friend #2.
Your friend #2 holds the hand of your friend #3
And finnally your friend# holds the hand of your friend #1.

At this point you can conclude that both are in a circle.

What about you ?

Well, you don't want to stay in this Island together with all the others, so you go out and abandon your friends going to another island.

This is the point :

Althrough all your friends can reach each other you are unable to reach them because you are not on the same Island.

This situation in Java is called as "Island of isolation" and in face of this all of your friends are eligible for garbage collector.
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please replace the folowing peace of my explanation

Your friend #1 holds the hand of your friend #2.
Your friend #2 holds the hand of your friend #3
And finnally your friend# holds the hand of your friend #1.


For this

Your friend #1 holds the hand of your friend #2.
Your friend #2 holds the hand of your friend #3
Your friend #3 holds the hand of your friend #4
And finnally your friend#4 holds the hand of your friend #1.

Sorry
 
steven gerrard
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for ur reply
i knw that but if u look at q carefully i1 ,i2,i3,i4 havent been nulled
that means i1 at the end of method still refers to object. sames the case with i2 i3 and i4 . So dont u think that they can still be called frm outside by ny thread
explanation given in the mock is of Island of isolation but i guess we can still use i1 to refer to object whose pointer points to i3.

dont u think that reason shd be that since i1,i2,i3,i4 are declared in a method and are not returned and hence cannot be accesed and hence r eligible . so even if we declare 4 objects in the method and dont call their other method
they will still be eligible for garbage collection since they r not returned so r inaccesible
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is that new J().m1(); statement is given "on the fly" and is not associated to any reference variable.

So it is on heap with no valid reference for it. Eligible for GC.

For sure it's a very good question.
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

dont u think that reason shd be that since i1,i2,i3,i4 are declared in a method and are not returned and hence cannot be accesed and hence r eligible . so even if we declare 4 objects in the method and dont call their other method
they will still be eligible for garbage collection since they r not returned so r inaccesible



Yes, variables i1,i2,i3,i4 will not longer exist after m1() returns becuase they are local variables.
[ February 09, 2006: Message edited by: Vlado Zajac ]
reply
    Bookmark Topic Watch Topic
  • New Topic