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 I Am Not Tired of Garbage Collection Yet 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 "I Am Not Tired of Garbage Collection Yet" Watch "I Am Not Tired of Garbage Collection Yet" New topic

I Am Not Tired of Garbage Collection Yet

JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
Which is the earliest line in the following code after which the object created on line 0 will be a candidate for being garbage collected?
public class Question {
static String f() {
String a = "hello";
String b = "bye"; // line 0
String c = b + "!"; // line 1
String d = b; // line 2
b = a; // line 3
d = a; // line 4
return c; // line 5
public static void main( String args[] ) {
String msg = f();
System.out.println( msg ); // line 6
This code fragment is from Khalid's mock exam.
Case A:
If all the strings created in the String pool are not going to be garbage collected, then the answer to the above question should be none, however, the answer given by Khalid is line 4. I really need an expert to tell me the right answer.
Case B:
Suppose Khalid's answer is valid. (It is an assumption!) And we delete line 4. Therefore, d still points to "bye", and "bye" is not hanging. My question is that those strings are eligible for being garbage collected after the method completes? or after the main method exits?
Please help because I really don't know.
Axl Rose

Joined: Dec 18, 2000
Posts: 16
I think the answer should be line 4, I will tell you why:
a points to "hello"
b points to "byte" //line 0
c points to "byte!" //line 1, notice that this is a new string
d points to b, which points to "byte" //line 2
b now points to a, which points to "hello" //line 3
notice that at this stage "byte" is still being pointed at by d
d now points to a, which points to "hello" //line 4
notice now that "byte" is not being pointed at by any other
variable. so it is available for GC
once the Java Virtual Machine (JVM) finished with line 4, and
before it jump to line 5, it will mark "byte" to be eligible for
GC, remember that GC can start AT ANY TIME , no guarantee when
it will start, and this answers your second question , the JVM
does not wait for the method to finish to start its GC, because
if it does that, the method may take valuable memory location
that it does not really need.
GC got nothing to do with when the methods will be terminated.
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
I think my questions were misundertood a little bit. My first question is I was told that the strings (those not created by "new") in the string pool are "not eligible" for garbage collection no matter they are being pointed or not. But this example in Khalid's mock exam says they do become eligible for garbage collection. Who is right?
My second question is in case we change the code fragment a little bit by deleting line 4. At which point those objects are "eligible" for garbage collection. It has nothing to do with "when" the garbage collection will happen (Actually, there is no guarantee about it happens or not). The eligibility occurs after the method f() completes or at the point the main method exits.
I agree. Here's the link:
subject: I Am Not Tired of Garbage Collection Yet
It's not a secret anymore!