GC of Method Local String and Instance Local String
Posted: Sat Jan 19, 2013 8:50 am
Hi,
I have confusion about following two cases
How many objects will be eligible for GC just after the method returns?
public void compute(Object p)
{
Object a = new Object();
int x = 100;
String str = "abc";
}
When does the String object “ hi ” instantiated on line 2 become eligible for garbage collection?
public class Hello {
String greeting = “hi”;
public static void main(String [] args) {
Hello h = new Hello();
h.greeting = null;
System.gc();
return;
}
}
The answer to first is
"abc" is a string literal which goes to the string pool and is not GCed.
I want to clarify whether the same holds for second also. Will the local string and instance member string both not be GCEed?
I have confusion about following two cases
How many objects will be eligible for GC just after the method returns?
public void compute(Object p)
{
Object a = new Object();
int x = 100;
String str = "abc";
}
When does the String object “ hi ” instantiated on line 2 become eligible for garbage collection?
public class Hello {
String greeting = “hi”;
public static void main(String [] args) {
Hello h = new Hello();
h.greeting = null;
System.gc();
return;
}
}
The answer to first is
"abc" is a string literal which goes to the string pool and is not GCed.
I want to clarify whether the same holds for second also. Will the local string and instance member string both not be GCEed?