• 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
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

After which line obj become eligible fo GC?

 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Alone {
6. Alone r;
7. public static void main(String [] args) {
8. Alone r1 = new Alone();
9. Alone r2 = new Alone();
10. Alone r3 = new Alone();
11. r1.r = r2; r2.r = r3; r3.r = r1;
12. r3 = null;
13. r2 = null;
14. r1 = null;
15. //do stuff that may make r1 refer to an object of Object class or it's 16. // subclasses
17. }
18. }

After which line does the first object become eligible for garbage collection?

A) Compilation Fails
B) Could not be determined
C) After line 14
D) After line 13
E) After line 12

Why is the answer C?
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This thread.
You can search javaranch for "Islands of Isolated" topic.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All r1, r2, r3 objects are in an island of links. So if r2 is alive then anything linked to it cannot be gc-ed, which mean r3 cannot be gc-ed, but since r3 has a link to r1, that makes r1 not gc-able. So for an island of links, only if all of them are null-out, you cannot gc any of them because they are all linked in a closed chain to each other.
 
Firas Zuriekat
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So for an island of links, only if all of them are null-out, you cannot gc any of them because they are all linked in a closed chain to each other.



Right after line 14, we have r1=r2=r3=null;

So we have an island of isolation (eligible for GC) RIGHT after line 14.

But in line 15, one of them could be made to point to an object. So why isn't the answer "Could not be determined"?

After all, GC depends on what line 15 does, right?
 
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No.

The question asks when will the first object be eligible for garbage collection. And, after line 14 all the three objects loose their references. And, these objects cannot be referenced again. Whatever, you try to do with the references after line 14 you cannot recover the objects eligible for collection, unless and until you use the finalize(), which is not the case here.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic