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

Islands of isolation

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. class Eco {
2. public static void main(String[] args) {
3. Eco e1 = new Eco();
4. Eco e2 = new Eco();
5. Eco e3 = new Eco();
6. e3.e = e2;
7. e1.e = e3;
8. e2 = null;
9. e3 = null;
10. e2.e = el;
11. e1 = null;
12. }
13. Eco e;
14. }


Can anyone explain me the islands of isolation topic, I found it very confusing?

 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
as per polya, if you draw a picture, it becomes clearer.



at this point, you have three references (e1, e2 and e3), each pointing to a unique object in the heap.



e1.e = e3;

e2 = null;
e3 = null;

e2.e = el;

e1 = null;

so at this point, e1, e2, and e3 all point to nothing. obj1 has a reference to obj3. that has a reference to obj2, which has a reference to obj1. Every object is referred to by SOMETHING, so they're not eligible for GC... However, you have no way to GET to them. They're all alone, each referring to each other in a little island in the heap. They're isolated from the rest of the code/references. Therefore, since you can't GET to them, even though they all HAVE a reference, they CAN be GC'd.
 
Ranch Hand
Posts: 331
Python Ruby Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here all the three objects are referencing to each other through their 'e' variable.However as you have nullified them.Now, you cannot access them in any manner, but still the three objects are lying somewhere on your computer's ram.
These are called 'islands' as they are isolated from the user (you) but still have got references(amongst themselves).
This is quite tricky and you should expect questions that ask about how many objects are eligible for garbage collection(as of java 5 exam) at a given line of code.
Hope this helps
 
babul bansal
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks buddies.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

e2 = null;
e3 = null;

code:


e1 -> obj1---
e2 obj2 |
^ |
| |
e3 obj3<--


e2.e = el;



how can we say this as e2 = null and how can we access e2.e it is same as saying null.e=e1;
 
Sheriff
Posts: 9708
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
varinder mahajan is right. Statement (1) will throw a NullPointerException.

1. class Eco {
2. public static void main(String[] args) {
3. Eco e1 = new Eco();
4. Eco e2 = new Eco();
5. Eco e3 = new Eco();
6. e3.e = e2;
7. e1.e = e3;
8. e2 = null;
9. e3 = null;
10. e2.e = el; //(1)
11. e1 = null;
12. }
13. Eco e;
14. }

also at (1) e2.e = el; must be e2.e = e1;
I think the whole statement (1) is wrong....Please babul bansal check your code...
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by babul bansal:
1. class Eco {
2. public static void main(String[] args) {
3. Eco e1 = new Eco();
4. Eco e2 = new Eco();
5. Eco e3 = new Eco();
6. e3.e = e2;
7. e1.e = e3;
8. e2 = null;
9. e3 = null;
10. e2.e = el;
11. e1 = null;
12. }
13. Eco e;
14. }

Can anyone explain me the islands of isolation topic, I found it very confusing?



I think this code trying to confuse you about Island Of Isolation, but the "real" problem is the line #10, which throws NullPointerException. You will face some questions like this in the exam.
But anyway, it's good to be good at Island of Isolation.
 
babul bansal
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I check the code, its a question given in the Kathy Sierra book under Self Test-12th question.

I am again posting the question with options and answer.
Given:

1. class Eco {
2. public static void main(String[] args) {
3. Eco e1 = new Eco();
4. Eco e2 = new Eco();
5. Eco e3 = new Eco();
6. e3.e = e2;
7. e1.e = e3;
8. e2 = null;
9. e3 = null;
10. e2.e = el;
11. e1 = null;
12. }
13. Eco e;
14. }

At what point is only a single object eligible for GC?

a)After line 8 runs.

b)After line 9 runs.

c)After line 10 runs.

d)After line 11 runs.

e)Compilation fails.

f)Never in this program.

g)An exception is thrown at runtime

ANSWER:
G is correct. An error at line 10 causes a NullPointerException to be thrown because e2 was set to null in line 8, If line 10 was moved between lines 7 and 8, then F would be correct, because until the last reference is nulled none of the objects is eligible, and once the last reference is nulled, all three are eligible.

 
Ranch Hand
Posts: 62
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all... this entire discussion lead me to have good knowledge on island of isolation..
 
Did you ever grow anything in the garden of your mind? - Fred Rogers. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic