This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic