File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt over Garbage Collection

 
Jeena Jeen
Ranch Hand
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ankit Garg
Sheriff
Posts: 9495
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

memory map.jpg
[Thumbnail for memory map.jpg]
 
Jeena Jeen
Ranch Hand
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 453
Google Web Toolkit Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
ambika mullasseri
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
untitled.GIF
[Thumbnail for untitled.GIF]
 
Ken Truitt
Ranch Hand
Posts: 124
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic