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

Garbage collection

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how many objects are eligible for GC in following code?

1. class X2 {
2. public X2 x;
3. public static void main(String [] args) {
4. X2 x2 = new X2();
5. X2 x3 = new X2();
6. x2.x = x3;
7. x3.x = x2;
8. x2 = new X2();
9. x3 = x2;
10. doComplexStuff();
11. }
12. }
after line 9 runs, how many objects are eligible for garbage collection?
A. 0
B. 1
C. 2
D. 3
E. 4

please explain in details
 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An island of isolation is created after execution of line 9.

The objects created on lines 4 & 5 (referred by x2 and x3), and of course the object x which is an instance member in each of them will be eligible for GC.

The answer should be 2.
[ October 11, 2006: Message edited by: Aniket Patil ]
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
C
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What will be the solution if we make a slight modification to the code and delete line 9.
 
Aniket Patil
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If line 9 is deleted, the object originally referred by x2 on line 4 can still be accessed by a live thread through x3.x. None of the objects should be eligible for GC in such a situation.
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

Can you please explain me in detail how after line 9, 2 objects are eligible for gc?

Regards,
Jothi Shankar Kumar. S
 
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 is called "Island of Isolation". An "island of isolation" describes one or more objects have NO references to them from active parts of an application.
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
If line 9 is deleted,only one object will be eligible for GC.Is it True
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by lakshmi amulya:
Hi,
If line 9 is deleted,only one object will be eligible for GC.Is it True



No... If line 9 is deleted then nothing is eligible for garbage collection.

x2 points to the object created in line 8. x3 points to the object created at line 5. As for the object created at line 4, it is still accessable via x3.x.

Henry
[ October 14, 2006: Message edited by: Henry Wong ]
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

I'm not able to understand this concept. Please could anyone explain me in detail about the island of isolation?

Regards,
Jothi Shankar Kumar. S
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I thik i will again advocate use of diagrams whenever having confusion over GC question.

https://coderanch.com/t/259787/java-programmer-SCJP/certification/Garbage-Collection-think-answer-provided

Now for island of isolation i came up with diagram given below


This is the state of object memory pool up to line 7


Now, two objects allocated in memory pool cannot be reached, as there is no way to reach reference varaible "x", this is like island of isolation
for two objects shown above, although each object having reference but it cannot be refer by program. Hence Garbage collector is smart enough to identify such scenario and claim memory allocated to both objects shown above.

Hope it helps !!
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am confused guys,

After execution of line 9 , no object is available for GC. As you said earlier that x2.x and x3.x cant be refered. B'coz x2, x3 are refering to new Object now. Correct me if I am wrong.
Then, how none is availble for GC when you delete line 9 (x3 = x2). What I understand is now both x2 and x3 is pointing towards new created object. But if we delete it, there still will be one object left ???
 
Aniket Patil
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please read my post or Henry's above.
 
reply
    Bookmark Topic Watch Topic
  • New Topic