Forums Register Login

I Am Not Tired of Garbage Collection Yet

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
He puts the "turd" in "saturday". Speaking of which, have you smelled this 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 617 times.
Similar Threads
Garbage collector
Strings and GC
Garbage
Garbage collection
Are String literals ever eligible for garbage collection?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 01:24:51.