class StringTest{
public void finalize(){
System.out.print("清理垃圾。");
}
public static void main(String[] args){
new StringTest();
String str1="123";
String str2=new String("123");
str1="2";
new StringTest();
System.gc();
}
}
//这样就有2个了
/*
public static void gc()
Runs the garbage collector.
Calling the gc method suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.
The call System.gc() is effectively equivalent to the call:
Runtime.getRuntime().gc()
protected void finalize()
throws Throwable
Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the finalize method to dispose of system resources or to perform other cleanup.
*/
[[it] 本帖最后由 sunkaidong 于 2008-4-5 16:19 编辑 [/it]]