wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage Collection Problem" Watch "Garbage Collection Problem" New topic
Author

Garbage Collection Problem

Jan tenpas
Greenhorn

Joined: Nov 14, 2010
Posts: 5
I'm working in the McGraw Hill SCJP study guide for Java 6. They showed that the code below has only one object eligible for garbage collection. Can anyone explain which one and why?

Thanks in advance!!!



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

Answer: 1
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3573
    
  14

This would be the instance initially assigned to a1.

The instance assigned to b1 still has a static reference to it (a1.b1 = b1), the instance assigned to b2 is still referenced by the instance in a2, and the instance in a2 is referenced by, well... a2.

Technically though, in the code as you have posted it, line 16 is not part of any method so the amount of objects (eligible or not) is undefined.

At the very least, after the main method has run, 3 of the objects are eligible for garbage collection (referenced by a1, a2 and b2) because they only have local references to them in the main method. b1 is referenced statically.
Jan tenpas
Greenhorn

Joined: Nov 14, 2010
Posts: 5
Good catch, I did put an extra blank line in the code at line 16.

The response from the books is as follows:

"It should be clear that there is still a reference to the object referred to by a2, and that there is still a reference to the object referred to by a2.b2. What might be less clear is that you can still access the other Beta object through the static variable a2.b1--because it's static."

I guess what I'm not getting is why the b1 object in Tester is not eligible for garbage collection. Since a1 is set to null and b1 is set to null, then what is still referencing b1?

Thanks a bunch, it's starting to click!
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3573
    
  14

Sorry for nitpicking, but even without the extra space, line 16 would still be outside of any method. What the guide should probably have said was 'at line 14'.

Anyway, back to your question. The instance is referenced by the class Alpha itself. The fact that they assign b1 to a1.b1, actually means they assign it to Alpha.b1 (since Alpha.b1 is static).

They qualify the reference with a1 (instead of Alpha), just to throw you off.

I am a firm believer that static members should *always* be qualified with the class name, instead of a reference name, and that the language should make the latter case illegal. Sadly, it's not.
Jan tenpas
Greenhorn

Joined: Nov 14, 2010
Posts: 5
Oh nice, I think I have it now. Would the following addition to the code would break the reference to the b1 object and free it up for GC? (

1. class Beta {}
2. class Alpha {
3. static Beta b1;
4. Beta b2;
5. }
6. public class Tester {
7. public static void main(String [] args){
8. Beta b1 = new Beta(); Beta b2 = new Beta();
9. Alpha a1 = new Alpha(); Alpha a2 = new Alpha();
10. a1.b1 = b1;
11. a1.b2 = b1;
12. a2.b1 = b2; //CHANGE STATIC REFERENCE TO THE b2 OBJECT. WILL THIS FREE UP b1 FOR GC?!?
13. a2.b2 = b2;
14. a1 = null; b1 = null; b2 = null;
15. //do stuff
16. }
17. }
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Jan please Use Code Tags when you post a source code. That way your code looks formatted. Unformatted code is hard to read. You can add code tags by wrapping your code in [code] [/code] tags. You can edit your message using button and then add code tags to it...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Kushan Athukorala
Ranch Hand

Joined: Aug 09, 2010
Posts: 33
Hi Jan,

This is an arguable question raised in our forum in 2008.

http://www.coderanch.com/t/417925/java-programmer-SCJP/certification/SCJP-Chapter-GC

However I believe only the answer given by Henry Wong in the above link.

The reason is that the references are not eligible for GC, only Objects get GCed.

Thanks,
Kushan

Kushan Athukorala
OCPJP 6.0 [86%]
vani venkat
Ranch Hand

Joined: Nov 21, 2006
Posts: 142
while doing GC questions, please try to draw diagrams when ever a new object is created. i mark them as circles.
add arrows to them when ever references are assigned. removes arrows when they are assigned null. this gives a clear picture of how many objects at the end are available with zero references.

SCJP 1.4, SCWCD 1.5
Nagendra Shasthri
Ranch Hand

Joined: Nov 12, 2010
Posts: 35

Hi Vani,

Can you give some more tips regarding the exam?


With Regards,
Shastry
Harry Tuttle
Greenhorn

Joined: Apr 08, 2010
Posts: 24
^
Jan tenpas wrote:When line 16 is reached, how many objects will be eligible for garbage collection?

Answer: 1


hello jan, i had the same question some monthes ago. the rule you must apply here is that an object is only eligible by the gc when it has no references on it. perhaps to understand this gc problem it helps you to differentiate between stack and heap. the stack is a piece of memory where all references to objects are stored. the heap is a piece of memory where the objects live.

in the given code four objects are instantiated, lets call them A1, A2, B1 and B2. the object B1 is referenced by b1, A1.b1 and A1.b2. so B1 will not be eligible by nulling b1, because the other references are still there. B2 has two references on it, on by b2 and the other by A2.b2. so even by nulling the b2 there is still one reference on it, so this object will not be eligible. the next object is A1 that has only one reference from a1. so by nulling a1 this object will be eligible by the gc. the last object is A2 that has also only one reference from a2. but this reference is not set to null so also this object is not eligible for the gc. so looking once again at the four objects we can see that only 1 object is eligible. so the answer given in the book is right if you need further help, let me know.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3573
    
  14

Harry, sorry for nitpicking (again), but objects may be eligible for collection even if they have references to them. What you mean is *reachable references*, which is an important difference. If two objects reference eachother, but nothing else does, they may still be garbage collected because they are not reachable.
Harry Tuttle
Greenhorn

Joined: Apr 08, 2010
Posts: 24
Stephan van Hulst wrote:Harry, sorry for nitpicking (again), but objects may be eligible for collection even if they have references to them. What you mean is *reachable references*, which is an important difference. If two objects reference eachother, but nothing else does, they may still be garbage collected because they are not reachable.


no problem. this page explain the types of reference objects.
Kurt Zhu
Greenhorn

Joined: Aug 25, 2011
Posts: 8

I think we can just try to adjust ourselves to the way JVM works. We can use the results of the code as what we should adjust ourselves to.

Thanks to @Ankit Garg @James Tharakan @Stephan van Hulst and others.
 
Don't get me started about those stupid light bulbs.
 
subject: Garbage Collection Problem
 
Similar Threads
Eligible for garbage collection
Garbage collection program really confusing me?
Problems with garbage collection
Double K&B Chapter 3- Question 11
Garbage Collection query.