为什么垃圾回收是在整个程序运行完才执行啊??
class Person{
private String name ;
private int age ;
public Person(String name,int age){
this.name = name ;
this.age = age ;
}
public String toString(){
return "姓名:" + this.name + " 年龄:" + this.age ;
}
public void finalize() throws Throwable{
System.out.println("垃圾被释放了--->:" + this ) ;
}
}
public class OSDemo2{
public static void main(String args[]){
long startTime = System.currentTimeMillis() ;
System.getProperties().list(System.out) ;
System.out.println("===============================") ;
System.out.println("当前系统信息:" + System.getProperty("os.name")) ;
System.out.println("当前系统信息:" + System.getProperty("os.arch")) ;
System.out.println("当前系统版本:" + System.getProperty("os.version")) ;
System.out.println("当前用户名字:" + System.getProperty("user.name")) ;
System.out.println("当前用户国家:" + System.getProperty("user.country")) ;
System.out.println("===============================") ;
Person per =new Person("J.rain",23) ;
per = null ;
System.gc() ;
long endTime = System.currentTimeMillis() ;
System.out.println("\n总共运行时间:" + (endTime - startTime) + "毫秒") ;
}
} ;
结果:
|
|