aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt over Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt over Garbage Collection " Watch "Doubt over Garbage Collection " New topic
Author

Doubt over Garbage Collection

Jeena Jeen
Ranch Hand

Joined: Feb 11, 2009
Posts: 47
Exam Lab for SCJP 5 Question 19

1. class A{
2.
3. A a1;
4. A a2;
5.
6. public void finalize(){
7. System.out.println("-");
8. }
9.
10. public static void main(String args[]){
11. A s1=new A();
12. s1.a1=new A();
13. s1.a2=s1;
14. s1.a1.a2=new A();
15. s1.a1.a2.a2=new A();
16. s1.a1.a2.a2.a1=s1.a1;
17. s1.a2.a1.a2=null;
18. System.gc();
19. }
20.
21. }

How many Objects are eligible for GC after Line 17. As mentioned in the ans I tried to draw graph which is attached with this question.
First of all is this graph correct?


Secondly if it is then am I correct then the object we are making null is shown in yellow color and Objects that are not reachable are shown by red arrow. T two objects eligible for gc are s1.a1.a2.a2 And s1.a1.a2.a2.a2
I know its a very long question but please can anyone tell me is it correct or not?





Albert Kam
Ranch Hand

Joined: Oct 18, 2007
Posts: 58
Hi,

My 2 cents .. i hope others could correct me if i'm mistaken

I think your graph is great !


T two objects eligible for gc are s1.a1.a2.a2 And s1.a1.a2.a2.a2


I think the objects eligible for garbage collection are :
s1.a1.a2.a2 (same with your answer) and ..
s1.a1.a2.
In this case, both of these objects are still connected, but they are consideres an island of unreferenced objects, thus being eligible for garbage collection ..

Cheers,
Albert Kam


Be nice !
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

Jeena your graph was great, but references don't point to other references, they point to objects, so precisely this is the correct memory representation of the question. There will be 2 objects eligible of GC as they are in a island of isolation as mentioned by Albert



[Thumbnail for memory map.jpg]


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Jeena Jeen
Ranch Hand

Joined: Feb 11, 2009
Posts: 47
Thank you guys for the reply. And thanks Ankit for pointing out the mistake in graph.
I understand why the two objects mentioned by Albert are eligible for gc.
I think s1.a1.a2.a2.a2 is not eligible for gc because it was never created as a new Object.
Rod Oliver
Greenhorn

Joined: Mar 15, 2009
Posts: 5
Thank you guys for the reply. And thanks Ankit for pointing out the mistake in graph.
I understand why the two objects mentioned by Albert are eligible for gc.
I think s1.a1.a2.a2.a2 is not eligible for gc because it was never created as a new Object.


I agree. but...

I think...The object referenced by "s1.a1.a2.a2.a1" still remains valid because still referenced by "s1.a1"...so, only one object is elegible for GC.

correct?

Anand A. Parikh
Greenhorn

Joined: May 12, 2009
Posts: 21

As pointed by Albert,
the two objects referenced by reference variables s1.a1.a2 AND s1.a1.a2.a2 are eligible for GC.

Anand
avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 453



SCJP 5.0 SCWCD 5.0
ambika mullasseri
Greenhorn

Joined: Jun 01, 2009
Posts: 28
Hi

I guess i am really confused.

According to me 4 objects are created but none are eligible for gc.please let me know where i am wrong.


[Thumbnail for untitled.GIF]


SCJP 6.0 86%
Ken Truitt
Ranch Hand

Joined: Aug 23, 2007
Posts: 124
In line 15, a brand new object is created. In line 17, the only reference to that object is assigned null.
The only other thing that happens in the meantime is that on line 16 the same object that was just created
gets an assignment of another object, that has a valid reference elsewhere, to one of its fields. So
it seems clear to me that only one object is eligible for gc. Am I wrong?


SCJP 88% | SCWCD 84%
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt over Garbage Collection