• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Garbage collection

 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

answer is 2..i get 3....???
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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???
 
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As per my understanding Neha is right
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this may help you.
https://coderanch.com/t/477867/Programmer-Certification-SCJP/certification/Garbage-Collection
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
maggie karve
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks neha for your superb explanation!!
 
rushikesh sawant
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
nice explanation by neha, it must have helped.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you neha for clarification
 
That is a really big piece of pie for such a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic