This topic has been archived. It cannot be replied.
-
工作学习 / IT技术讨论 / question about java garbage collectorConsider the following code:
Public void method(String s)
{
String a,b;
a= new String( "Hello");
b= new String("Goodbye");
System.out.println(a+b);
a= null;
a=b;
System.out.println(a+b);
}
Where the garbage collector will run the first time?
In line "a=null" or " a=b"?
I think it will run in "a = null" at first, then "a=b".
-guest:Yue;
2001-6-8
{367}
(#94412@0)
-
Let me try.....
-jabber(jabber);
2001-6-8
{558}
(#94613@0)
-
both of them are not correct, the garbage collection will start at the first line of "System.out.println(a+b); " couse "a" and "b" have been invoked
-guest:ditto;
2001-6-10
(#96565@0)
-
disagree with youI think the garbage collector will run after the ending of that method.
though a is set by null ,it doesnot mean a is destroyed but it has a value of null.
-danieldu(天马行空);
2001-6-10
{156}
(#96595@0)
-
why not tryprintf somthing in it's finalize function and before/after the assignment, see which print first.
-blaise(blaise);
2001-6-10
{98}
(#96819@0)
-
You can't tell when the garbage collector will run. You can't even force the garbage collector to run. So the answer should be "don't know"
-guest:;
2001-6-11
(#97621@0)
-
although you cant not farce it run. you DO can recommend garbage collection should run by using System.gc() method but do not guarantee it.
-guest:wager;
2001-6-11
(#97648@0)
-
Cannot say GC will happen .... But a+b will lead to a String available to be GC.
-guest:yaojian;
2001-6-11
(#97654@0)
-
Cannot say GC will happen .... But a+b will lead to a String available to be GC. Actually, even first line :new String("hello") will make "hello" available got GC also.
-guest:yaojian;
2001-6-11
(#97657@0)