• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Islands of object..

 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I got it from K&B 5 study guide and i modified a bit.
class Island
{
Island i;
public static void main(String args[])
{
Island i1=new Island();
Island i2=new Island();
Island i3=new Island();
Island i4=new Island();
i1.i=i2;
i2.i=i3;
i3.i=i1;
i4.i=i3;
i3=null;
i2=null;
i1=null;
}
}

How many objects are eligible for GC?
my guess is 3. since i4 is not null islands of objects are still referring each other and they are not eligible for Gc.so only i1,i2,i3 are eligible for Gc.
Am i got the point?

Please do correct me if i am wrong.

Preparing Scjp 1.5
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
only i1 and i2 are eligible for GC.
i3 is being referenced by i4.i !
 
author
Posts: 23836
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No objects are eligible for GC. From the i4 reference you can get to the i4 object. From the reference in that object, you can get to the object originally referred to by i3. From the reference in that object, you can get to the object originally referred to by i1. And from the reference in that object, you can get to the object originally referred to by i2.

Henry
 
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Henry.

To make a cheap diagram you can still access all objects in the following way:

i4->i4.i->i3->i3.i->i1->i1.i->i2

i2 then references i3 but since we already accessed that from i4 it doesn't matter. All objects can be referenced through i4 so none qualify for GC.
 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes no objects are eligible for GC.

i1----------object1-----------i3.i

i2---------object2-------------i1.i

i3---------object3-------------i4.i
|
|
i2.i


i4---------object4

when i3=null


i1----------object1-----------i3.i

i2---------object2-------------i1.i

object3-------------i4.i
|
|
i2.i


i4---------object4

when i2=null


i1----------object1-----------i3.i

object2-------------i1.i

object3-------------i4.i
|
|
i2.i


i4---------object4

when i1=null

object1-----------i3.i

object2-------------i1.i

object3-------------i4.i
|
|
i2.i


i4---------object4


so no object is eligible for GC.....
[ November 11, 2008: Message edited by: Ganeshkumar cheekati ]
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks to all i am clear now.
 
M Srilatha
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I made a mistake

Thanks to all!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!