This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I am preparing for scjp(1.4). When I was taking up a mock test I came across the following question on garbage collection. Question: How many objects would be eligible for garbage collection at line number 9: Code:
My answer to the above question was "B". But the mock exam told that my answer was wrong and it said "C" was the correct answer. The reason that the mock exam states for this is that the last String object still has a reference and hence it will not be eligible for garbage collection. Hence the answer 9. But what I thought is that since the reference "s" looses its scope at line 9, i thought all the 10 objects are eligible for garbage collection. Can some one tell me if I am right or wrong in this. [ Jess fixed the [ code ] tags ] [ April 23, 2003: Message edited by: Jessica Sant ]
Yes you are right the correct answer is B i.e 10 objects are eligible for garbage collection. There seems to be a mistake in the mock exam. Which mock exam was it, you can send a mail to the author and he/she will update the exam accordingly.
But what I thought is that since the reference "s" looses its scope at line 9, i thought all the 10 objects are eligible for garbage collection.
In your example, the variable s is out of scope at line 9. At that point, the object referred to by the variable s can no longer be accessed by the program. However, a reference to the object still exists on the thread stack until the method main returns. The object is in the �invisible� state. Invisible objects cannot be collected. I learned this from section A.3.3 of this article The Truth About Garbage Collection It's an appendix to this book Java Platform Performance Therefore, I would say the correct answer is C. 9 [ April 23, 2003: Message edited by: Marlene Miller ]
Thank you Barry for the link. It is very helpful. An efficient implementation of the JVM is unlikely to zero the reference when it goes out of scope. The object referenced by the local variable s continues to be strongly referenced, at least until the method returns. An object enters an unreachable state when no more strong references to it exist. When an object is unreachable, it is a candidate for collection. It is important to note that not just any strong reference will hold an object in memory. These must be references that chain from a garbage collection root. GC roots are a special class of variable that includes temporary variables on the stack (of any thread) and static variables (from any class). From this I conclude that when the example is executed by some Java virtual machine implementations, the last String object is not eligible for garbage collection at line 9.
Joined: Aug 03, 2002
I do not disagree with you, Marlene, about the implementation details, but from the SCJP2 point of view the answer will be the "wrong" one. The reference variable goes out of lexical scope and the object is considered to be eligible for GC. It's an example of too much JVM knowlege is a dangerous thing (that is two points are lost)