Page 1 of 1

GC of Method Local String and Instance Local String

Posted: Sat Jan 19, 2013 8:50 am
by PMiglani
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?

Re: GC of Method Local String and Instance Local String

Posted: Sat Jan 19, 2013 9:46 am
by admin
Yes, neither of them will be GCed.
HTH,
Paul.