File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Garbage Collection Concept Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Garbage Collection Concept" Watch "Garbage Collection Concept" New topic
Author

Garbage Collection Concept

Anshu Priyadarshini
Greenhorn

Joined: Nov 12, 2011
Posts: 2
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]
Mike Okri
Ranch Hand

Joined: Jun 22, 2011
Posts: 83
Welcome to JavaRanch
This might help
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Hi and welcome to the JavaRanch,

This question has been asked many times. Search for it in the SCJP/OCPJP forum.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Anshu Priyadarshini
Greenhorn

Joined: Nov 12, 2011
Posts: 2
@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

Joined: Jun 22, 2011
Posts: 83
This question was answered a while back - garbage collection problem
Ben Ooms
Ranch Hand

Joined: May 16, 2011
Posts: 44

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).


 
jQuery in Action, 2nd edition
 
subject: Garbage Collection Concept
 
Similar Threads
Garbage collection program really confusing me?
Problems with garbage collection
K&B SCJP6 Chapter 3- Q11 a GC question
GC question (code)
Garbage Collection query.