您可以在这里快速查找:


 
您的位置: 编程学习 > java教程 > 200508
文章分类

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

VB编程
2006: 02

Asp编程
2005: 11 12
2006: 01 02

C++/VC
2005: 10 11 12
2006: 01 02

Delphi
2005: 12
2006: 01 02

其它

 本文章适合所有读者

GC测试

treeroot

下面这段代码可以用来测试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++;
  }
}