简体   繁体   English

将对象引用设置为null或调用finalize()方法?

[英]Set object reference to null or call the finalize() method?

As a java beginner I wanted to ask about if I should set an object reference to null, or call the finalize method in a large project? 作为一个java初学者,我想询问是否应该将对象引用设置为null,或者在大型项目中调用finalize方法?

For example, 例如,

while( obj... some code here){
 //code stuff
}
obj = null; // or obj.finalize();

Since im done with the obj and there are no more references to it what should i do with it? 由于我完成了obj并且没有更多的引用它应该怎么做呢? set it to null or call finalize();? 将其设置为null或调用finalize();?

I read the java doc but this paragraph The finalize method is never invoked more than once by a Java virtual machine for any given object. 我阅读了java doc但是本段The finalize method is never invoked more than once by a Java virtual machine for any given object. confused me. 使我困惑。 Does that mean that even if i use it, it won't do anything at all unless GC decides to do so? 这是否意味着即使我使用它,它也不会做任何事情,除非GC决定这样做? Then, will my decision to set the obj to null help any? 那么,我将obj设置为null的决定是否有帮助?

This all assuming its a large project and the scope has yet to end in order for GC to deallocate it itself. 这一切都假设它是一个大型项目,并且范围尚未结束,以便GC自行解除分配。 Thanks. 谢谢。

Neither. 都不是。 If obj is no longer used further in the code (and nothing else references the same object as obj does), the object will no longer be reachable and will become a candidate for Garbage Collection. 如果代码中不再使用obj (并且没有其他内容引用与obj相同的对象),则该对象将不再可访问并将成为垃圾收集的候选对象。

Setting obj to null will not achieve anything (YMMV depending on implementation of garbage collectors). obj设置为null将无法实现任何功能(YMMV取决于垃圾收集器的实现)。

As for calling finalize() , don't. 至于调用finalize() ,不要。 Let the Garbage Collector take care of that. 让垃圾收集器处理这个问题。 But note that finalize is a method like any other. 但请注意, finalize是一种与其他方法一样的方法。 What the text you quoted is saying is that the GC will not call it more than once, regardless of the amount of times your code may have invoked it. 你引用的文字是说GC不会多次调用它,无论你的代码调用它的次数多少。

NEVER invoke an object's finalize() method manually. 永远不要手动调用对象的finalize()方法。 It is for the VM's use only. 它仅供VM使用。 The VM will invoke it at an appropriate time (as part of the garbage collection process). VM将在适当的时间调用它(作为垃圾收集过程的一部分)。

You may set a reference variable to null to possibly make an object eligible for garbage collection sooner than it otherwise would be, but doing so does not cause it to be GC'd immediately. 您可以将引用变量设置为null以使对象可能比其他情况更快地使垃圾收集符合条件,但这样做不会导致它立即进行GC。 (Note that you use the = operator for assignments; == is a relational operator for testing the equality of its operands.) (注意,您使用=运算符进行赋值; ==是一个关系运算符,用于测试其操作数的相等性。)

If your object maintains state that you must ensure is cleaned up on demand, then implement a different method for that. 如果您的对象保持必须确保按需清理的状态,则为此实现不同的方法。 close() is a popular name for such methods. close()是此类方法的流行名称。 It is good practice to avoid implementing finalize() if at all possible. 如果可能的话,最好避免实现finalize() The GC will automatically take care of most resources that you might think you want to clean up manually in finalize() . GC将自动处理您可能认为要在finalize()手动清理的大多数资源。

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

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