This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java and have Dr. Raoul-Gabriel Urma & Richard Warburton on-line!
See this thread for details.
Win a copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java this week in the Agile and Other Processes 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Garbage Collection Concept

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While going through the questions from study guide Kathy Sierra I found the following question, the solution was also there, please explain how its done:
3. class Beta {}
4. class Alpha {
5. static Beta b1;
6. Beta b2;
7. }
8. public class Tester {
9. public static void main(String[] args) {
10. Beta b1 = new Beta(); Beta b2 = new Beta();
11. Alpha a1 = new Alpha(); Alpha a2 = new Alpha();
12. a1.b1 = b1;
13. a1.b2 = b1;
14. a2.b2 = b2;
15. a1 = null ; b1 =null; b2 = null;
16 // do stuff
17 }
18. }


When line 16 is reached how many objects will be eligible for Garbage collection?

answer given is 1 object. Please let me know how this answer is approached.
Thanks in advance..

[Edit - thread title - MB]
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch
This might help
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Anshu Priyadarshini wrote:\
When line 16 is reached how many objects will be eligible for Garbage collection?



What do you think is the answer, and why?

You need to understand that this site is about helping people develop the tools to figure out their own answers, not about simply handing out answers. So if you're having a hard time, we need to hear what you've tried, what you think, and where you're confused, so that we can give you the information you need to complete the puzzle you're facing.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi and welcome to the JavaRanch,

This question has been asked many times. Search for it in the SCJP/OCPJP forum.
 
Anshu Priyadarshini
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Jeff:
While solving the above problem my approach said there should be 2 objects for garbage collection; one referred by a1 and the other by b1, the approach I followed is:
a1: we can directly get from question that a1 would be garbage collected, b1: and since a1 is garabage collected, its instance variables that are referring to b1 object do not have any link to outside world, hence it will also be garbage collected.

Please let me know if I am wrong and also the approach how the answer would come as 1.
I think this explains where I am stuck.
Could you now help.
 
Mike Okri
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question was answered a while back - garbage collection problem
 
Ranch Hand
Posts: 47
Netbeans IDE Eclipse IDE Debian
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only way to get it is to draw!

line by line write down a declared reference and a arrow pointing to a object (the hint in this question is a static object). When code say something like foo=null; remove the arrow between foo and the object.

For example:


So at line 8 you draw:
f -------> Foo

after line 9 :
f -------> Foo
b1 -----> Bar

after line 10:
f -------> Foo<- |
b1 -----> Bar |
b1.f1 ------------|

after line 11:
f -------> Foo<- |
b1 -----> Bar |
b1.f1 ------------|
b2 -----> Bar |
b2.f1-------------|

after line 12:
f -------> Foo<- |
b1 Bar |
b1.f1 |
b2 -----> Bar |
b2.f1-------------|

So if the question was how many object are eligible for GC after line 12 you just have to count the objects without lines. In this example 1 object (Bar).


 
What's gotten into you? Could it be this tiny ad?
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!