aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes garbage collection code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "garbage collection code" Watch "garbage collection code" New topic
Author

garbage collection code

Raj Sing
Greenhorn

Joined: Apr 23, 2003
Posts: 3
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 ]
Lawrence Chettiar
Ranch Hand

Joined: Apr 10, 2003
Posts: 62
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.


SCJP 1.4 (86%)<br />SCMAD Beta (77%)<br />SCEA (part I:89%,partII:93%)<br />MCAD (cleared 70-315 & 70-320 [Score 980])
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
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 ]
Badri Sarma
Ranch Hand

Joined: Apr 01, 2003
Posts: 72
So, what is correct answer according to SCJP1.2 exam


Thanks<br />Badri
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
This has been discussed somewhere here before. For the SCJP2 it would be 10 objects are eligible for GC.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Here we go, 'old on tight!
[ April 24, 2003: Message edited by: Barry Gaunt ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
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.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: garbage collection code