File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes garbage collection 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" Watch "garbage collection" New topic

garbage collection

nitin sharma
Ranch Hand

Joined: Feb 24, 2001
Posts: 290
class Test {
static boolean status;

public static void Process(boolean status) {
if(!status) {
String s="Java";
int i;
String x="Java";
String y=x;

public static void main(String [] args) {
Test t = new Test();

How many objects are eligible for Garbage Collection?
1)Cannot determine.
5)compile-time error occurs.
6)No object is eligible for GC.

Hello jane gristi,
I have read your view's on the garbage collection in one of the previous post.I have taken a question related to garbage collection from site.
In one of your previous post you mentioned that only the object's created on the heap are eligible for the garbage collection.In my opinion either 1 or 6 is correct,the given answer is 2 ,Please throw some light on it.

Zahid, Butt
Ranch Hand

Joined: Feb 16, 2001
Posts: 100
nitin, you said the answer is 2, do you mean option 2 from the list or is it that 2 objects are available for garbage
collection ??
Please make it clear.
Thanks in advance.
nitin sharma
Ranch Hand

Joined: Feb 24, 2001
Posts: 290
i am talking about the second option.The answer given in the mocktest is 1.
nitin sharma
Ranch Hand

Joined: Feb 24, 2001
Posts: 290
the nswer given in the mock test is
2)1.I am sorry for the confusing stuff.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi nitin,
Technically, no object would be available for garbage collection (ans 6). Two objects are created in the String pool, "Java" and "JavaScript". These would not be garbage collected unless the system they running on had optimized the JVM to run the garbage collector on the String pool.
A third object 't' is created in <code>main()</code> however it's reference is never set to 'null'. The program ends, at which point nothing is gc'd.
If we assume the mock exam author treated string literals as objects eligible for gc (which is usually the case), I'm not sure how they arrived at the answer of 'one object being eligible for gc'.
There are two strings created in the String pool. "Java" and "JavaScript". The reference variable 's' originally points to "Java" then is reset to point to a new string "JavaScript". The statement <code>String x = "Java";</code> will not create a second "Java" object; a reference to the existing pool object will be returned. Reference 'y' is then given x's reference, so it will now point to the original string object "Java".
At the end of the method you end up with 'y' holding a reference to "Java" and 's' holding a reference to "JavaScript". If string constants were eligible for gc; both these objects would be available when the method completes.
Most of the mock exams ignore the fact that gc is system dependent and use literal strings for the gc examples. For the purpose of the mocks, the majority assume String literals are collected. However; it is highly unlikely you will get a gc question on the exam that involves String literals!. Sun will not include questions that rely on knowledge of a specific JVM implementation!
Also, don't get yourself in knots over gc ... the majority of people report they only had one gc question on the exam
Hope that helps.
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform

Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
I agree. Here's the link:
subject: garbage collection
It's not a secret anymore!