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

为什么调用了system.gc();,垃圾回收还是没有执行?
class   Garbage
{
int   index;
static   int   count;
Garbage()
{
count++;
System.out.println( "Object "   +   count   +   "construct ");
setID(count);
}

void   setID(int   id)
{
index   =   id;
}

protected   void   finallze()
{
System.out.println( "object "   +   index   +   "is   reclaimed ");
}

public   static   void   main(String   args[])
{
new   Garbage();
new   Garbage();
new   Garbage();
new   Garbage();
System.gc();
}
}

------解决方案--------------------
finallze()写错了-----> finalize()
------解决方案--------------------
其实Java中没有任何机制保证程序能强制执行垃圾回收,gc()不行,其他任何方法都不行。
调用gc()只能告诉jvm说,我想现在运行垃圾回收,而jvm究竟什么时间进行,那是jvm自己的事,程序无权干涉。
------解决方案--------------------
垃圾回收器通常是作为一个单独的低级别的线程运行,不可预知的情况下对内存堆中已经死亡的或者长时间没有使用的对象进行清楚和回收,程序员不能实时的调用垃圾回收器对某个对象或所有对象进行垃圾回收
------解决方案--------------------
System.gc();的作用如下:

我:Hi,楼主结贴了!

你:但是你结不结贴是你的事情,我没有办法控制你马上结贴。