Forums Register Login

Garbage Collection Problem

+Pie Number of slices to send: Send
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
1
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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. }
1
+Pie Number of slices to send: Send
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...
+Pie Number of slices to send: Send
Hi Jan,

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

https://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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Hi Vani,

Can you give some more tips regarding the exam?
+Pie Number of slices to send: Send
^

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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send

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.
It is difficult to free fools from the chains they revere - Voltaire. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1814 times.
Similar Threads
Garbage collection program really confusing me?
Eligible for garbage collection
Problems with garbage collection
Garbage Collection query.
Double K&B Chapter 3- Question 11
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 13:34:02.