日期:2014-05-20  浏览次数:20695 次

請問一下程序中的System.gc()代錶什么?????
class   MyObject{
Test   main;   //记录Test对象,在finalize中时用于恢复可达性
public   MyObject(Test   t)
{  
main=t;   //保存Test   对象
}
protected   void   finalize()
{
main.ref=this;//   恢复本对象,让本对象可达
System.out.println( "This   is   finalize ");//用于测试finalize只运行一次
}
}

class   Test   {
MyObject   ref;
public   static   void   main(String[]   args)   {
Test   test=new   Test();
test.ref=new   MyObject(test);
test.ref=null;   //MyObject对象为不可达对象,finalize将被调用
System.gc();   //?????????????????
if   (test.ref!=null)   System.out.println( "My   Object还活着 ");  
}
}
當使用gc()2時,輸齣:
This   is   finalize
My   Object还活着

當不使用gc()時,沒有任何輸齣


------解决方案--------------------
gc=garbge collection
------解决方案--------------------
尽管调用了GC垃圾回收,但是JVM不一定会即使清除.
------解决方案--------------------
回收没有用而占用内存的对象
------解决方案--------------------
申请系统进行垃圾回收,但是未必会做
------解决方案--------------------
恩 劝其回收
可以接帖了