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;
}
}
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;
}
}