调用了System.gc()可finalize()怎么还无法执行

dcbeyond 2006-06-15 03:50:55
附代码如下
public class Tank {
Tank(){
full=true;
System.out.println("Tank is created.");
}
public void get(){
System.out.println("get() is called...");
full=false;
}
public void put(){
System.out.println("put() is called...");
full=true;
}
public void finalize(){
System.out.println("hello");
if(full)
{
System.out.println("Error:The tank is full.");
}
else
{
System.out.println("Tank is cleaned.");
}
}
/**
* @param args
*/
public static void main(String[] args) {
Tank t=new Tank();
t.get();
t.put();
//?此处如果不对t.finalize()进行显式调用,那么finalize()不会执行?
System.runFinalization();
t.finalize();
System.gc();
}
private boolean full=false;
}
...全文
99 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
dcbeyond 2006-06-15
  • 打赏
  • 举报
回复
哦,这样JVM就知道t已经不是某一对象的引用,然后垃圾回收器就会在System.gc()前调用fianlize()方法,是这样的吧。
ttaallkk1 2006-06-15
  • 打赏
  • 举报
回复
调用GC并不一定立即触发

最好

在gc之前加

t = null;
这样能加快触发的速度

不过还是不一定立即触发
usherlight 2006-06-15
  • 打赏
  • 举报
回复
调用GC并不一定立即触发
UnAgain 2006-06-15
  • 打赏
  • 举报
回复
在gc之前加

t = null;

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧