• 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

islands of isolation(Garbage Collection)

 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Can any one explain islands of isolation with a simple example as I read K&B but didnt understand the concept.
Thanks in advance
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Divya,


You know an object is eligible for garbage collection when there is no
active reference to it. Active reference means a reference that you can
avail to access that object.

Islandic objects do have reference but you can't access that.

Like:


You see obj1 and obj2 ref variables are set to null but still objects
are referred by two reference variables obj1.b and obj2.b; You can't access
them. They are islandic. Staying in isolation.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adding to previous post:
JVM has special treatment to handle such a situation called Islandic objects.
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is Islandic objects are elligible for garbage collection
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, island objects are illegible for garbage collection, simply you draw the stack reference to the object, and draw a circle that is the heap, place the objects in the heap, and draw a line which represents the reference from the stack to the object, draw lines between objects in the heap, these are the references from the other objects.
Now when we say that a reference = null, delete the reference to that object from the stack, after all references are deleted, there will be just references from other objects in the heap, think about it, are any of these objects can be accessed?
The only way to access an object is from it's reference, no it has no reference in the stack, so they are not reachable, even they still have reference, but theses reference are from objects in the heap, which are in turn not accessible, so they all are useles, and just memory consuming.
 
yogesh srinivasan
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that a nice reply but thats too much for me to understand
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Divya,

It often helps if you start off with telling us what you do understand, and where things start to get confusing for you. For instance, are you clear on how objects and their references relate, and when objects normally become eligible for the GC?
 
Divya Gehlot
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bert,
Thanks for advicing me. I was reading the book and got confused with what is written in the book so posted my doubt in forum. Still I am confused what I understood is like when an object is refrencing to nowhere it is eligible for grabage collection or if its value is null.If I am wrong please correct me.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Still I am confused what I understood is like when an object is referencing to nowhere it is eligible for garbage collection or if its value is null.If I am wrong please correct me.



A ref variable that lives on the stack (under condition, on the heap also),
refers to the object that lives on the heap. Setting object ref variable to
null means, making an object unusable because there is no reference to refer
to that object. That also mean, making that object eligible for garbage
collection.

Island of isolation is the condition when there is one or more references
that refer to the object on the heap but you can't access the object because
there is no active reference.

Example I have shown in my very previous post.
Now we would like to know what you got.


Thanks,
[ July 12, 2007: Message edited by: Chandra Bhatt ]
 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Divya Gehlot:
Hi Bert,
what I understood is like when an object is refrencing to nowhere it is eligible for grabage collection or if its value is null.If I am wrong please correct me.



These points shall help.

1. An object does not refer anything.

2. An object is like some memory stuff. (Say like a cotton ball)

3. An object is created with new operator and constructor call.
So when you say



memory is allocated, the object is created. But it is eligible for garbage collection because nothing is pointing to it.

Your question may be what do I mean by "nothing is pointing to it". I mean reference. Yes, object and reference are two different things.


It creates a reference. But it is not pointing to any object. There is no object here. Only reference.

4. A reference is a variable that points to memory location (or better put the object).


Now, garbage collection.... garbage collection is having objects without any reference variable pointing to them.


Hope you will enjoy this thread .
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Divya -

It's certainly okay to ask the question you asked - I was just trying to help you think it through yourself so that you'd understand it better!

Before you can really understand islands of isolation here are a few questions you should probably be able to answer (you could write a few lines of annotated code to show your answers):

1 - can you show an example of an object that is NOT eligible for GC?
2 - can you show an example of an object that IS eligible for GC
3 - can you show an example of one object (sort of), referring to another object?
 
Ranch Hand
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1 - can you show an example of an object that is NOT eligible for GC?




ans 1. class App
{
public static App display()
{
App a= new App();
return a;
}
class Exe
{
public static void main(String args[])
{
App a =new App()
a=a.display();// here its not eligibe for gc
}
}

am i right Mr Bert Gates
 
Vishal Hegde
Ranch Hand
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
- can you show an example of an object that IS eligible for GC
class App
{

}
class A
{
public static void main(String args[])
{
App a=new App();
a=null;
}
}
 
Vishal Hegde
Ranch Hand
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
- can you show an example of one object (sort of), referring to another object?

class Obj1
{

}
class Obj2 extends Obj1
{
}
class Obj3 extends obj2
{
public static void main(String args[])
{
Obj1 o1=new Obj1();
Obj2 o2=new Obj2();
Obj3 03= new Obj3();
o2=o3;

}
}

Am i right??? Mr Bert??
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vishal Hegde wrote:am i right Mr Bert Gates


Bert Gates?? As in Bill Gates

Your first code is wrong as per the statement. The object created at line App a =new App() becomes eligible for Garbage Collection at the next line.

Second code is correct.

Third code is not right in accordance to what you are trying to achieve. The statement says "one object, referring to another object", but in your code, two references are pointing to the same object. Look at the following code

Here the reference variable a points to an instance of class A. That instance of class A has a reference variable named b which points to an object of class B. Thus an Object of class A is referring to an object of another class...
 
Vishal Hegde
Ranch Hand
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:

Third code is not right in accordance to what you are trying to achieve. The statement says "one object, referring to another object", but in your code, two references are pointing to the same object. Look at the following code.



how come to references pointing to same object???

i created to object of the same class but they are totally different from each other???
also the reference variables are different?



 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vishal Hegde wrote:- can you show an example of one object (sort of), referring to another object?

class Obj1
{

}
class Obj2 extends Obj1
{
}
class Obj3 extends obj2
{
public static void main(String args[])
{
Obj1 o1=new Obj1();
Obj2 o2=new Obj2();
Obj3 03= new Obj3();
o2=o3;

}
}

Am i right??? Mr Bert??





See remember the difference between reference variable and object.

Object is stored in heap at a particular address, and reference variable keep that particular address with it to point to that object.
so for your above lines:



o1, o2, o3 are reference variables stored in stack with methods
and
new Obj1(), new Obj2() and new Obj3() are actual objects leaving in the heap.


means


than


o1's value = 100 so, o1------------------> 100 address in heap that is new Obj1()
o2's value = 200 so, o2------------------> 200 address in heap that is new Obj2()
o3's value = 300 so, o3-------------------->300 address in heap that is new Obj3()



after

o2=o3
as o3's has 300
o2=300 so o2's new value = 300



that means

o2 ,o3 both-----------------------> 300 address in heap that is new Obj3()



Means o2, o3 references are now pointing to same obj that is obj2.
And nobody is pointing to obj2 at 200 address.
so obj2 is eligible for garbage collection.



 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic