This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage Collection" Watch "Garbage Collection" New topic
Author

Garbage Collection

Srinivas Katta
Ranch Hand

Joined: Feb 01, 2007
Posts: 76
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

Joined: Aug 16, 2005
Posts: 14074
    
  16

Please QuoteYourSources - tell us where you copied this question from.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

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


[Thumbnail for Untitled.jpg]


SCJP 6 [86%] June 30th, 2010
OCPWCD [84%] March 26th, 2013
If you find any post useful, click the "plus one" sign on the right
Srinivas Katta
Ranch Hand

Joined: Feb 01, 2007
Posts: 76
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

Joined: Feb 01, 2007
Posts: 76
Hi Prasad,

How can I upload the image that is modified in My System (The Image you have sketched)
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

while writing the post
there is option for the attachments
just under the text area
Srinivas Katta
Ranch Hand

Joined: Feb 01, 2007
Posts: 76
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


[Thumbnail for Garbage.jpg]

Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

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

Joined: Mar 07, 2010
Posts: 438

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


[Thumbnail for Untitled1.jpg]

Srinivas Katta
Ranch Hand

Joined: Feb 01, 2007
Posts: 76
Hi Prasad,

Thanks a lot for taking time to explain in detail.

Thanks
Srinivas


Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

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

Joined: Feb 01, 2007
Posts: 76
Yes prasad, Happy Holidays
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

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


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Rodrigo Tassini
Greenhorn

Joined: Dec 29, 2008
Posts: 21
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

Joined: Mar 07, 2010
Posts: 438

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage Collection
 
Similar Threads
Garbage collection question from Exam lab
How to make the map of object to be garbage collected
exam lab gc question
How many Objects are elligible for Gabbage Collection at Line 17?
Garbage collection