GeeCON Prague 2014*
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


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage collection" Watch "Garbage collection" New topic
Author

Garbage collection

maggie karve
Ranch Hand

Joined: Dec 29, 2009
Posts: 186

answer is 2..i get 3....???
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
yup the answer is 2.

the objects referred by s1.a1.a2 and s1.a1.a2.a2 will be eligible for gc.

which other did you get???


SCJP 1.6 96%
Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

As per my understanding Neha is right
rushikesh sawant
Ranch Hand

Joined: Dec 22, 2009
Posts: 65
this may help you.
http://www.coderanch.com/t/477867/Programmer-Certification-SCJP/certification/Garbage-Collection


SCJP 5.0 100%
saima kanwal
Ranch Hand

Joined: Oct 05, 2009
Posts: 148
can anyone please explain how and what two objects are eligible for gc? I am really confused in this GC topic . I have seen the link referred above and have understood that example , but this one is really difficult. Anyone please explain.


SCJP 6.0 (86%) SCWCD 5 (94%)
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14194
    
  20

maggie joseph, please quote your sources - tell us from which book, mock exam or other source you copied this question. (Note that quoting your sources is required on JavaRanch).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
maggie karve
Ranch Hand

Joined: Dec 29, 2009
Posts: 186
i was considering the object referenced by s1.a1.a2.a2.a1..
According to me...

s1 is referring to an object..
.then s1.a1 will be further extension like
s1------->s1.a1
then s1=s1.a2,so they are referring to same object
then s1 or s1.a2---------->s1.a1.a2------------->s1.a1.a2.a2---------->s1.a1.a2.a2.a1;
Again due to
s1.a2.a1.a2=null;;
Since s1.a1=s1..i will refer this as s1.a1.a2 ok?
so s1.a1.a2 will be referring to null.......and hence further s1.a1.a2.a2 's object will not be referring to anything...so are "these" objects eleigible for Gc???
what about the object made by s1.a1.a2.a2.a1 since it is no longer pointing to its first object ..it is pointing to latest s1.a1's object.....???
thanks...

Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
s1---------------> object A and it contains two reference variable a1 and a2 pointing to null at the moment
s1.a1---------------> another object A that again contains two reference variable a1 and a2 pointing to null at the moment
s1.a2 = s1 ---------> means s1.a2 points to object refered by s1.
s1.a1.a2-------------> yet another object A that contains two reference variable a1 and a2 pointing to null at the moment.
s1.a1.a2.a2------------> yet another object A that contains two reference variable a1 and a2 pointing to null at the moment.
s1.a1.a2.a2.a1=s1.a1-------> means both of them point to same object which was s1.a1 was pointing.
s1.a2.a1.a2=null----------> means object refered by s1.a1.a2 becomes null (since s1 and s1,a2 refers to same object there a1 reference variable is same)
so now object refered by s1.a1.a2 has no live reference and hence object refered by s1.a1.a2.a2 also loses any live references.
and hence these 2 objects become eligible for gc.


I hope this helps.
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
saima kanwal wrote:can anyone please explain how and what two objects are eligible for gc? I am really confused in this GC topic . I have seen the link referred above and have understood that example , but this one is really difficult. Anyone please explain.


the best way to solve gc question is drawing a diagram.
saima kanwal
Ranch Hand

Joined: Oct 05, 2009
Posts: 148
Thanks neha for your superb explanation!!
rushikesh sawant
Ranch Hand

Joined: Dec 22, 2009
Posts: 65
nice explanation by neha, it must have helped.
seomul evans
Greenhorn

Joined: Jan 07, 2010
Posts: 2
Thank you neha for clarification


SEO Services | Internet Marketing | Internet Marketing Blog | SEO
 
GeeCON Prague 2014
 
subject: Garbage collection