什么时候回收垃圾
public class RJMould{
StringBuffer sb;
public static void main(String[] args)
{
RJMould rjm = new RJMould();
rjm.kansas();
}
public void kansas(){
sb = new StringBuffer( "Manchester ");//8
StringBuffer sb2 =sb;//9
String Buffer sb3 = new StringBuffer( "OK ");//10
sb = sb3;//11
sb3 = null;//12
sb2 = null; //13
}
}
垃圾回收机制上说当代码无法在被访问的时候,就开始回收,那么在12行的时候就开始了,但是在SCJP考试卷子上它确说是13行,希望高人指点。
------解决方案--------------------例子:
class Test{
String name;
Long id;
public EqualObject(String name, Long id) {
super();
this.name = name;
this.id = id;
}
protected void finalize() throws Throwable {
System.out.println( "finalize( " + name + ", " + id + ") ");
}
}
public class MyTest{
public static void main(String[] args) {
EqualObject a = new Test( "1 ", new Long(1));
a = new Test( "2 ", new Long(2));
a = new Test( "3 ", new Long(3));
a = new Test( "4 ", new Long(4));
System.gc(); //如果没有这句,没有打印
a= null;
a = new Test( "5 ", new Long(5));
a= null;
a = new Test( "6 ", new Long(6));
a= null;
a = new Test( "7 ", new Long(7));
a= null;
System.gc(); //同上
}
}
------解决方案--------------------1.gc很懒
2.gc线程优先级低