首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

小弟我写的GC的示例

2012-09-05 
我写的GC的示例?public class Test {//@author sunlishan 2012-6-14String namepublic Test(String s){th

我写的GC的示例

?

public class Test {

//@author sunlishan 2012-6-14

String name;

public Test(String s){

this.name =s;

}

@Override

protected void finalize() throws Throwable {

System.out.println("GC "+this.name);

}

public void seeGC(){

Test a = new Test("a");//这句是帮助GC的,<引用a> 不再引用<对象a>,对象t可以被GC了

a=null;

}

public static void main(String[] args) throws Throwable{

Test t = new Test("t");

t.seeGC();

t=null;

System.gc();//JVM GC ?输出的"GC t","GC a"先后顺序不定

Thread.sleep(10000);//GC是单起一个线程来做的,让当前线程停一下,先GC线程执行完。如果没有这句,不一定能输出 "GC t","GC a"

}

}

?

//别外,finalize方法在调几次就执行几次,不存在只执行一次之说

?

?

?

测试环境:

?

java version "1.6.0_30"

Java(TM) SE Runtime Environment (build 1.6.0_30-b12)

Java HotSpot(TM) 64-Bit Server VM (build 20.5-b03, mixed mode)

?

CPU:inter core 2 Duo E8200

热点排行