• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection

 
Srinivas Katta
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Can anybody sketch the diagram for below Garbage Collection program for better understanding. I tried to sme extent but still have some confusions



Thanks for your Help
Thanks
Srinivas
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15495
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please QuoteYourSources - tell us where you copied this question from.
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is the diagram of all the references and the objects
now can you tell me what happens after making the references null?
give it a try and then ask if you still are not able to do it
Untitled.jpg
[Thumbnail for Untitled.jpg]
 
Srinivas Katta
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your explanation,

The Third object refers to null, but none of teh Objects are eligible for GC



Please let me know your Observations
 
Srinivas Katta
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Prasad,

How can I upload the image that is modified in My System (The Image you have sketched)
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
while writing the post
there is option for the attachments
just under the text area
 
Srinivas Katta
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After executing the Code

The Third object refers to null, but none of the Objects are eligible for GC

Please let me know your Observations
Garbage.jpg
[Thumbnail for Garbage.jpg]
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well
haven't you checked the explanation ?
how many objects are eligible for garbage collection?
think a little bit
and what is the answer provided in examlab?
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the diagram I have provided is till the line 14
now see what happens at line 15

here the statement
s1.a2.a1.a2 = null
means following

1. s1.a2 means the reference a2 in the object which is referred by s1
2. in the line 11 we have written s1.a2 = s1; this means that "s1.a2 refers to the object that is referred by s1"
3. Now, this is shown in the figure, notice that first object is referred by s1 and s1.a2 (you should understand this now )
4. now in the line 15
we are making
s1.a2.a1.a2 = null
here we can consider s1.a2 as s1 also because of the statement 2 in explanation

now
s1.a2.a1.a2 referes to null hence the link that is to the third object from second object's a2 variable is null isn't it?

so the third object becomes eligible for GC, now there is link to the fourth object from third object
but there is no external link from which we can reach to the fourth object hence the fourth object is also eligible for GC
now there is link from fourth object to the second object as shown in the figure
and the link gets lost due to the fact that fourth object is eligible for GC
but we can still refer the second object as s1.a1
or also as s1.a2.a1
hence it is not eligible for GC
so the total two objects are eligible for GC

see the diagram for further explanation
Untitled1.jpg
[Thumbnail for Untitled1.jpg]
 
Srinivas Katta
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Prasad,

Thanks a lot for taking time to explain in detail.

Thanks
Srinivas


 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well
busy persons like Ankit Garg can devote so much time then why can't I ?
I am just enjoying holidays
college is yet to start
happy preparation Shriniwas
did you understand it thoroughly now?
 
Srinivas Katta
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes prasad, Happy Holidays
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks similar to this

http://www.coderanch.com/t/500166/java-programmer-SCJP/certification/ExamLab-garbage-collection-rd-test

Almost the same question. Do search for existing solutions before you post. The forum is full of them
 
Rodrigo Tassini
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Prasad Kharkar

I don't found a reference to line 11 in your chart:


Could you explain me this.

Thanks.

 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you did not understand the question fully

look

we are referring to object number 1 using the reference variable s1
now
the object contains two references of type A they are a1 and a2
now

s1 has instance reference variable a2
and the line

s1.a2 = s1;

means that "the reference variable a2 that is of the object referenced by s1 is referring to the object that is referred by reference variable s1"

in other words
the reference variable a2 is referring to the object in which a2 lies and that is also the object referred by reference variable s1 also
hence
s1.a2 and s1 refer to the same object

hope this is clear to you now
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic