• 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

Circular references and garbage collection

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which of the following statements are true?
Select all valid answers
(a) Objects can explicitly be destroyed using the keyword delete
(b) An object will be garbage collected immediately after the last reference to the object is removed.
(c) If object obj1 is accessible from object obj2 and obj2 is accessible from obj1, then obj1 and obj2 are not eligible for garbage collection
(d) Once an object has become eligible for garbage collection, it will remain eligible until it is destroyed
(e) If an object obj1 can access an object obj2 that is eligible for garbage collection, then obj1 is also eligible for garbage collection
Ans: e
I feel that the option 'c' is also correct.
Can anybody explain to me as to why 'c' is a wrong answer? Any comments are also welcome.
Thanks,
Kezia.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They could be eligable if they were the only references to those objects.
[ January 12, 2002: Message edited by: Jose Botella ]
 
Kezia Matthews
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jose,
I didn't get you. Can you please explain or eloborate on it?
Thanks.
Kezia.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kezia

If object obj1 is accessible from object obj2 and obj2 is accessible from obj1, then obj1 and obj2 are not eligible for garbage collection


Imagine the only reference to obj1 is hold by obj2. And that the only reference to ob2 is hold by obj1. That means that they are not reachable from any reachable variable in the program. The g.c. should be able to collect these circularly-referenced objects.
This scenario complies with

However they are eligable for g.c. so c is wrong
 
Kezia Matthews
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In such a scenario, that if either obj1 or obj2 becomes eligible for garbage collection, the other also becomes eligible for garbage collection? Am I right?
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the scenario I described both of them are already eligable for g.c.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic