如果finalize方法没有完成怎么办?

时间:2014-12-18 08:05:44

标签: java garbage-collection

当覆盖任何对象的finalize()方法时,当没有引用指向它时,GC不会立即对其进行垃圾收集。它将对象放入队列中,然后调用其finalize方法。 如果finalize方法有一些不定式循环会发生什么。 问题是究竟发生了什么?我需要一些有经验的解释。

感谢。

我试了一下,程序就退出了

这是该计划:

public class Main {

    static int created;
    static int finalized;

    public Main() {
    }

    public static void main(String[] strings) {
        for (int i = 0; ; i++) {
            Main obj = new Main();
            obj = null;
        }

    }

    protected void finalize() {
        while(true){
            //do something
        }
    }


}

这是这些选项-verbose:gc -Xmx64m -XX:+PrintGCDetails

的结果
[GC [PSYoungGen: 16448K->2656K(19136K)] 16448K->16024K(62848K), 0.0729540 secs] [Times: user=0.11 sys=0.00, real=0.08 secs] 
[GC [PSYoungGen: 19104K->2672K(19136K)] 32472K->32544K(62848K), 0.0457800 secs] [Times: user=0.09 sys=0.01, real=0.04 secs] 
[Full GC [PSYoungGen: 2672K->0K(19136K)] [PSOldGen: 29872K->32530K(43712K)] 32544K->32530K(62848K) [PSPermGen: 2748K->2748K(21248K)], 0.0894110 secs] [Times: user=0.09 sys=0.00, real=0.09 secs] 
[Full GC [PSYoungGen: 16448K->5266K(19136K)] [PSOldGen: 32530K->43711K(43712K)] 48978K->48978K(62848K) [PSPermGen: 2748K->2748K(21248K)], 0.1144110 secs] [Times: user=0.11 sys=0.00, real=0.11 secs] 
[Full GC [PSYoungGen: 16448K->16448K(19136K)] [PSOldGen: 43711K->43711K(43712K)] 60159K->60159K(62848K) [PSPermGen: 2748K->2748K(21248K)], 0.1191420 secs] [Times: user=0.12 sys=0.00, real=0.12 secs] 
[Full GC [PSYoungGen: 16448K->16307K(19136K)] [PSOldGen: 43711K->43712K(43712K)] 60159K->60019K(62848K) [PSPermGen: 2748K->2741K(21248K)], 0.1612240 secs] [Times: user=0.16 sys=0.00, real=0.17 secs] 
[Full GC [PSYoungGen: 16448K->16448K(19136K)] [PSOldGen: 43712K->43712K(43712K)] 60160K->60160K(62848K) [PSPermGen: 2741K->2741K(21248K)], 0.1109170 secs] [Times: user=0.11 sys=0.00, real=0.10 secs] 
[Full GC [PSYoungGen: 16448K->16447K(19136K)] [PSOldGen: 43712K->43712K(43712K)] 60160K->60159K(62848K) [PSPermGen: 2741K->2741K(21248K)], 0.1105400 secs] [Times: user=0.11 sys=0.00, real=0.12 secs] 
[Full GC [PSYoungGen: 16447K->16447K(19136K)] [PSOldGen: 43712K->43712K(43712K)] 60159K->60159K(62848K) [PSPermGen: 2741K->2741K(21248K)], 0.1080170 secs] [Times: user=0.11 sys=0.00, real=0.10 secs] 
[Full GC [PSYoungGen: 16447K->16447K(19136K)] [PSOldGen: 43712K->43712K(43712K)] 60159K->60159K(62848K) [PSPermGen: 2741K->2741K(21248K)], 0.1068950 secs] [Times: user=0.11 sys=0.00, real=0.11 secs] 
[Full GC [PSYoungGen: 16447K->16447K(19136K)] [PSOldGen: 43712K->43712K(43712K)] 60159K->60159K(62848K) [PSPermGen: 2741K->2741K(21248K)], 0.1059930 secs] [Times: user=0.10 sys=0.00, real=0.11 secs] 
[Full GC [PSYoungGen: 16447K->16447K(19136K)] [PSOldGen: 43712K->43712K(43712K)] 60159K->60159K(62848K) [PSPermGen: 2741K->2741K(21248K)], 0.1063360 secs] [Times: user=0.11 sys=0.01, real=0.10 secs] 
[Full GC [PSYoungGen: 16448K->16448K(19136K)] [PSOldGen: 43712K->43712K(43712K)] 60160K->60160K(62848K) [PSPermGen: 2741K->2741K(21248K)], 0.1056820 secs] [Times: user=0.10 sys=0.00, real=0.11 secs] 
[Full GC [PSYoungGen: 16448K->16448K(19136K)] [PSOldGen: 43712K->43712K(43712K)] 60160K->60160K(62848K) [PSPermGen: 2741K->2741K(21248K)], 0.1062960 secs] [Times: user=0.11 sys=0.00, real=0.11 secs] 
[Full GC [PSYoungGen: 16448K->16448K(19136K)] [PSOldGen: 43712K->43712K(43712K)] 60160K->60160K(62848K) [PSPermGen: 2741K->2741K(21248K)], 0.1063870 secs] [Times: user=0.10 sys=0.00, real=0.10 secs] 
[Full GCDisconnected from the target VM, address: '127.0.0.1:45038', transport: 'socket'
 [PSYoungGen: 16448K->16446K(19136K)] [PSOldGen: 43712K->43712K(43712K)] 60160K->60158K(62848K) [PSPermGen: 2741K->2741K(21248K)], 0.1241690 secs] [Times: user=0.13 sys=0.00, real=0.13 secs] 
Heap
 PSYoungGen      total 19136K, used 16448K [0x00000000feab0000, 0x0000000100000000, 0x0000000100000000)
  eden space 16448K, 100% used [0x00000000feab0000,0x00000000ffac0000,0x00000000ffac0000)
  from space 2688K, 0% used [0x00000000ffd60000,0x00000000ffd60000,0x0000000100000000)
  to   space 2688K, 0% used [0x00000000ffac0000,0x00000000ffac0000,0x00000000ffd60000)
 PSOldGen        total 43712K, used 43712K [0x00000000fc000000, 0x00000000feab0000, 0x00000000feab0000)
  object space 43712K, 100% used [0x00000000fc000000,0x00000000feab0000,0x00000000feab0000)
 PSPermGen       total 21248K, used 2749K [0x00000000f6e00000, 0x00000000f82c0000, 0x00000000fc000000)
  object space 21248K, 12% used [0x00000000f6e00000,0x00000000f70af4d8,0x00000000f82c0000)

0 个答案:

没有答案