This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes islands of isolation(Garbage Collection) Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "islands of isolation(Garbage Collection)" Watch "islands of isolation(Garbage Collection)" New topic
Author

islands of isolation(Garbage Collection)

Divya Gehlot
Ranch Hand

Joined: Sep 10, 2006
Posts: 238
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


SCJP1.5(81%)<br />SCDJWS(94%)<br />next mission SCEA(but need to wait or that)
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
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.


cmbhatt
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Adding to previous post:
JVM has special treatment to handle such a situation called Islandic objects.
yogesh srinivasan
Ranch Hand

Joined: Jun 08, 2007
Posts: 55
is Islandic objects are elligible for garbage collection
Tamer Badr
Ranch Hand

Joined: Feb 18, 2004
Posts: 48
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

Joined: Jun 08, 2007
Posts: 55
that a nice reply but thats too much for me to understand
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8712
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?


Eliminate fossil fuel subsidies. (If you're not on the edge, you're taking up too much room.)
Divya Gehlot
Ranch Hand

Joined: Sep 10, 2006
Posts: 238
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

Joined: Feb 28, 2007
Posts: 1707

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 ]
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1493
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 .


Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8712
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?
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 973

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


http://www.lifesbizzare.blogspot.com || OCJP:81%
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 973

- 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

Joined: Aug 01, 2009
Posts: 973

- 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??
Ankit Garg
Saloon Keeper

Joined: Aug 03, 2008
Posts: 9189
    
    2

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 973

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?



Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
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.




SCJP 6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: islands of isolation(Garbage Collection)
 
Similar Threads
garbage collectiion
Isolating a reference
WA #1.....word association
Dumb question on the wrapper classes