• 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

Is it an example of island of isolation?

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Please see the following code and the explanation:

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 = e1;
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.

My question is if I remove line number 10 from the code will the answer to this question will be choice A.I mean if there was supposed to be no runtime error after removing line number 10 from the above code then the answer to the question 'At what point is only a single object eligible for GC?' would be A.

Thanks
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I claim that after line 8 is executed, the references e1 and e3 are still alive. In addition e3.e is a reference to e2. Therefore, all three objects
that were created are still alive and cannot be garbage collected.

At least that is the way I see it.

Bob
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
rakhee, when you copy a question from a book or mock exam, we require that you quote your sources. So, please tell us where you copied it from.
 
rakhee gupta
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Sorry i didn't knew this convention.I copied it from K&B scjp certification guide chapter 3.
 
rakhee gupta
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bob,

But after line 8 the reference e3.e is on the heap and not on stack(because e is an instance variable of e3) and e2 has no contact will the external world although it has an active reference e3.e so it is eligible for GC.

Please correct me if my understanding is wrong.
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lets say you comment out line 10 (and that no exception
is thrown at runtime), then after the statement on
line 11 has executed, then one object will be eligible
for GC from the heap, that being the object that was referenced
by e1.

I find that it always helps to depict the scenario
by drawing out the objects that would be on the heap.
It does help! Especially in the exam
Best regards
[ July 16, 2008: Message edited by: Keith Nagle ]
 
rakhee gupta
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not objects referenced by e2 and e3 along with e3 are eligible for GC after line 3 after all their references(e2,e3) also being set to null?
 
Keith Nagle
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rakhee gupta:
Why not objects referenced by e2 and e3 along with e3 are eligible for GC after line 3 after all their references(e2,e3) also being set to null?



Again, forgetting about the NPE,
Objects referenced by e2 and e3 are not eligible for GC as
they are still accessible by any live thread via references
e1.e and e3.e
You have to try and visualize it!
Best regards.
 
snakes are really good at eating slugs. And you wouldn't think it, but so are tiny ads:
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