关于java垃圾回收器的问题

anjae 2005-01-03 08:55:53
//: Garbage.java
// Demonstration of the garbage
// collector and finalization

class Chair {
static boolean gcrun = false;
static boolean f = false;
static int created = 0;
static int finalized = 0;
int i;
Chair() {
i = ++created;
if(created == 47)
System.out.println("Created 47");
}
protected void finalize() {
if(!gcrun) {
gcrun = true;
System.out.println(
"Beginning to finalize after " +
created + " Chairs have been created");
}
if(i == 47) {
System.out.println(
"Finalizing Chair #47, " +
"Setting flag to stop Chair creation");
f = true;
}
finalized++;
if(finalized >= created)
System.out.println(
"All " + finalized + " finalized");
}
}

public class Garbage {
public static void main(String[] args) {
if(args.length == 0) {
System.err.println("Usage: \n" +
"java Garbage before\n or:\n" +
"java Garbage after");
return;
}
while(!Chair.f) {
new Chair();
new String("To take up space");
}
System.out.println(
"After all Chairs have been created:\n" +
"total created = " + Chair.created +
", total finalized = " + Chair.finalized);
if(args[0].equals("before")) {
System.out.println("gc():");
System.gc();
System.out.println("runFinalization():");
System.runFinalization();
}
System.out.println("bye!");
if(args[0].equals("after"))
System.runFinalizersOnExit(true);
}
} ///:~

请教java回收器怎么调用到
if(i == 47) {
这句代码,然后结束程序的?
...全文
208 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
anjae 2005-01-05
  • 打赏
  • 举报
回复
谢谢各位了!
fireflyqt 2005-01-04
  • 打赏
  • 举报
回复
if (i == 47)
这句为什么不会被程序调用?
你考虑的是不是这一句:
while(!Chair.f) {
new Chair();
new String("To take up space");
}
你认为会一直在这个循环中,所以不会执行其他语句。
这个循环中会不断产生新对象,它的目的就是为了使垃圾回收器能够执行,也就是当所占用的JVM内存达到一定数量的时候。
这时开始回收垃圾,回收之前调用finalize方法,当要回收47号对象时,将Chair.f设置为true,此时便退出循环,不再继续产生新的对象,程序继续向下执行。
不知道说明白没
anjae 2005-01-04
  • 打赏
  • 举报
回复
这是程序执行的结果

F:\JCreator>java Garbage
Usage:
java Garbage before
or:
java Garbage after

F:\JCreator>java Garbage before
Created 47
Beginning to finalize after 5882 Chairs have been created
Exception in thread "main" java.lang.OutOfMemoryError

F:\JCreator>java Garbage before
Created 47
Beginning to finalize after 5897 Chairs have been created
Finalizing Chair #47, Setting flag to stop Chair creation
After all Chairs have been created:
total created = 92648, total finalized = 26771
gc():
runFinalization():
bye!

F:\JCreator>java Garbage after
Created 47
Beginning to finalize after 5887 Chairs have been created
Finalizing Chair #47, Setting flag to stop Chair creation
After all Chairs have been created:
total created = 84469, total finalized = 13994
bye!

F:\JCreator>java Garbage any
Created 47
Beginning to finalize after 5985 Chairs have been created
Exception in thread "main" java.lang.OutOfMemoryError


F:\JCreator>java Garbage any
Created 47
Beginning to finalize after 5879 Chairs have been created
Finalizing Chair #47, Setting flag to stop Chair creation
After all Chairs have been created:
total created = 1764137, total finalized = 1712844
bye!

问题是:if (i == 47)这句不能被程序调用,而程序又确实调用了,那么垃圾回收器通过什么方式调用的呢?为什么会调用这句呢? 程序并没有任何 i=47的语句。(不过程序中确实调用到这句了,因为输出了 Finalizing Chair #47, Setting flag to stop Chair creation)
还是垃圾回收器以其它方式运行,并没有调用这句程序结束的?它的执行又是以什么方式呢?
williamsmile 2005-01-04
  • 打赏
  • 举报
回复
同意萤火虫的看法.
lxpbuaa 2005-01-03
  • 打赏
  • 举报
回复
1、while(!Chair.f) 中不停创建对象时,当这些对象累计占用内存逼近JVM规定内存上限时,垃圾收集器会自动执行。
2、收集垃圾前,对象的finalize会被自动调用。if (i == 47)判断此时是否是第47号chair对象的finalize被调用,换句话说,i在这里是这些被创建的对象的编号。因为i = ++created;且created是static的。因此当第47号对象的finalize被调用时,f被设置为true,while(!Chair.f) 就退出了。
harvardjl 2005-01-03
  • 打赏
  • 举报
回复
关注中。。。。。。
anjae 2005-01-03
  • 打赏
  • 举报
回复
呵呵,楼上说的不错,使TIJ的
疑问就是在这儿,finalize和System.gc()应该不会自行中断代码,要不然后面的代码就不会执行
从代码执行的情况看,if (i == 47)确实是执行了,问题在这儿,程序不可能执行这一句的,因为有个static count, 在等于执行第一次垃圾回收的时候 count 都6000多了,后面i跟着循环不可能回到47,那么,垃圾回收器怎么让 if(i == 47)确实执行的呢?
当然,程序执行也遇到内存不足退出的,后面的代码就没有再执行了
fireflyqt 2005-01-03
  • 打赏
  • 举报
回复
这个好象是TIJ中的代码吧
垃圾回收器回收对象之前会先调用finalize方法的
noscar 2005-01-03
  • 打赏
  • 举报
回复
1。当系统,内存不足是调用
2。当你通过 System.gc()是调用

62,612

社区成员

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

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