aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes After which line obj become eligible fo GC? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "After which line obj become eligible fo GC?" Watch "After which line obj become eligible fo GC?" New topic
Author

After which line obj become eligible fo GC?

Firas Zuriekat
Ranch Hand

Joined: May 09, 2006
Posts: 144
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?
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
This thread.
You can search javaranch for "Islands of Isolated" topic.
William CJ
Ranch Hand

Joined: Feb 28, 2006
Posts: 34
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

Joined: May 09, 2006
Posts: 144
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?
Aum Tao
Ranch Hand

Joined: Feb 14, 2006
Posts: 210
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.


SCJP 1.4 85%
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: After which line obj become eligible fo GC?