s******e 发帖数: 63 | 1 public class A{
static Vector v = new Vector();
public void test{
String s = new String("Test");
v.put(s);
}
}
will s be garbage collected after running test method? assume garbage
collector runs concurrently. | w*r 发帖数: 2421 | 2 I wish some highhand can correct me if I were wrong, here is my understanding
s will be gone after test finish execution, however "Test" will stay
s is just a reference local to test() method.(assume you mean public
void test(){...}) , therefore this reference will be gone once the method
finish execution and exit its execution stack. s is not collected by
GC in this case. ...
【在 s******e 的大作中提到】 : public class A{ : static Vector v = new Vector(); : public void test{ : String s = new String("Test"); : v.put(s); : } : } : will s be garbage collected after running test method? assume garbage : collector runs concurrently.
| g*****g 发帖数: 34805 | 3 It's not gonna be GCed as the Vecotor keeps an ref to the string.
It will be GCed only when v is GCed, that will be end of progam
as v is static.
Vector, memory pool, resource conn (DB, network) is where you 'll
find memory leak in Java.
【在 w*r 的大作中提到】 : I wish some highhand can correct me if I were wrong, here is my understanding : s will be gone after test finish execution, however "Test" will stay : s is just a reference local to test() method.(assume you mean public : void test(){...}) , therefore this reference will be gone once the method : finish execution and exit its execution stack. s is not collected by : GC in this case. ...
|
|