您的位置:首页 > 编程语言 > Java开发

JAVA作业,GC的效果

2005-09-12 13:59 399 查看
package demo;

public class DemoFinal {

    private static Runtime runtime = Runtime.getRuntime();

    private static boolean finalized;

    private int buffer[] = new int[10000];

    protected static long printFreeMemory(String prefix) {
        long freeMemory = runtime.freeMemory();
        System.out.print(prefix);
        System.out.println("The free memory is " + freeMemory);        
        return freeMemory;
    }

    public static void main(String args[]) {
        DemoFinal demoFinal = new DemoFinal();
        printFreeMemory("");                
        demoFinal = null;
        long time = System.currentTimeMillis();
        runtime.gc();        
        while (finalized)    ; //waiting for the finalize executed
        System.out.print("The gc Action costs ");
        System.out.print(System.currentTimeMillis()-time);
        System.out.print(" Milliseconds");
          
    }

    protected void finalize() throws Throwable {
        printFreeMemory("After GC,");
        finalized = true;
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息