

Java技术
2005: 03 04 05 06 07 08
09 10 11 12
2006: 01 02
Asp.net
2005: 07 08 09 10 11 12
2006: 01 02

下面这段代码可以用来测试GC的工作情况,finalize方法是关键
public class GCTest {
public static void main(String[] args) throws InterruptedException {
//runGC();
for(int i=0;i<100;i++){
newObj();
}
System.out.println("一共产生的对象个数:"+A.i);
System.out.println("垃圾回收的对象个数:"+A.gc);
}
static void newObj(){
for(int i=0;i<1000;i++){
Object o=new A();
}
}
static void runGC(){
Runtime.getRuntime().gc();
}
}
class A{
public static int i=0;
public static int gc=0;
int ii=i++;
public String toString(){
return "Object"+ii;
}
public void finalize(){
//System.out.println("GC:"+this.toString());
gc++;
}
}