简体   繁体   English

finalize()没有被调用

[英]finalize() not getting called

Why is finalize() not being called here. 为什么没有在这里调用finalize() The code compiled and ran successfully but there wasn't any output. 代码编译并成功运行,但没有任何输出。

package temp;

public class Temp {

    int i;

    Temp(int j) {
        i = j;
    }

    public void finalize() {
        if (i == 10) {
            System.out.println("Finalize called.");
        }
    }

    public static void main(String[] args) {
        Temp obj = new Temp(10);
        System.gc();
    }

}

Your call to System.gc(); 您对System.gc();致电System.gc(); makes no difference, since your Temp instance has a reference ( obj ) so it's not eligible for garbage collection. 没有区别,因为您的Temp实例有一个引用( obj ),因此它不符合垃圾回收的条件。

Even if it was eligible for garbage collection, calling System.gc(); 即使它有资格进行垃圾收集, System.gc();调用System.gc(); doesn't necessarily collect all the objects that have no reference to them immediately. 不一定会立即收集所有没有引用它们的对象。

It so happen that im reading Effective Java 我正在阅读有效的Java

ITEM 7: AVOID FINALIZERS 第7项:避免决赛者

Finalizers are unpredictable, often dangerous, and generally unnecessary. 终结器是不可预测的,通常是危险的,并且通常是不必要的。 -Effective Java (page 50) - 有效的Java(第50页)

another one from pdf. 另一个来自pdf。

Don't be seduced by the methods System.gc and System.runFinalization . 不要被System.gc和System.runFinalization方法所诱惑。 They may increase the odds of finalizers ge tting executed, but they don't guaran- tee it. 它们可能会增加执行终结器的几率,但它们并不能保证它。 The only methods that claim to guarantee finalization are System.runFi- nalizersOnExit and its evil twin, Runtime.runFinalizersOnExit . 声称保证最终确定的唯一方法是System.runFi-nalizersOnExit及其邪恶的双胞胎Runtime.runFinalizersOnExit。 These methods are fatally flawed and have been deprecated [ThreadStop]. 这些方法存在致命缺陷,已被弃用[ThreadStop]。

based on this using System.gc will only increase the odds of finalizers getting executed and importantly using it will not guarantee that it will run the Garbage Collection, it is only suggesting to the jvm. 基于此使用System.gc只会增加终结器执行的几率,重要的是使用它不能保证它会运行垃圾收集,它只是建议 jvm。

another one. 另一个。

Not only does the language specification provide no guarantee that finalizers will get executed promptly; 语言规范不仅不能保证终结器能够及时执行; it provides no guarantee that they'll get executed at CHAPTER 2 CREATING AND DESTROYING OBJECTS 28 all. 它并不能保证它们会在第2章创建和破坏对象28中被执行。 It is entirely possible, even likely, that a program terminates without executing finalizers on some objects that are no longer reachable 完全可能,甚至可能,程序终止而不对某些不再可访问的对象执行终结器

add obj = null; add obj = null; to make reference null, then your finalize method will be called. 要使引用为null,则将调用finalize方法。 Thsi is again not a guranteed behaviour, for me 1-2 times i was able to call it out of 5 times. Thsi再次不是一种保证行为,对我来说,1-2次,我可以用5次来称呼它。

 public static void main(String[] args) {
        Temp obj = new Temp(10);
        obj  = null;
        System.gc();
    }

Output 产量

hi10
Finalize called.

while creating an object the constructor is called but not the finalise() method so you need to refer the function from instance obj and here System.gc(); 在创建对象时,构造函数被调用但不是finalize()方法,所以你需要从实例obj和这里引用函数System.gc(); has not made any difference or called the method finalize(); 没有任何区别或称为方法finalize();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM