简体   繁体   English

什么时候应该在java中使用finalize()方法?

[英]When should be we use finalize() method in java?

When should we really use finalize() method in java? 我们什么时候应该在java中使用finalize()方法?

If we want to close connection in finalize() method then it's better to use the code below as waiting for GC to call finalize() method and then release the connection does not make sense 如果我们想在finalize()方法中关闭连接,那么最好使用下面的代码等待GC调用finalize()方法然后释放连接没有意义

try{
// Connection creation
}finally{
//close connection
}

So the question is does finalize() method has any relevance today? 所以问题是finalize()方法今天有没有相关性?

It is indeed preferable to release resources by calling a method explicitly. 确实最好通过显式调用方法来释放资源。 Finalizers are not necessarily called promptly or even at all. 终结者不一定要及时调用,甚至根本不调用。 And they add a performance penalty. 而且他们增加了性能损失。

However, finalizers may still be used as a safety net to release resources, in case the client has forgotten to dispose explicitly. 但是,如果客户忘记明确处置,终结者仍可用作释放资源的安全网。

From the topic "Avoid finalizers" in Joshua Bloch's "Effective Java," 2nd ed.: 来自Joshua Bloch的“Effective Java”中的“避免终结者”主题,第2版:

[D]on't use finalizers except as a safety net or to terminate noncritical native resources. [D]不使用终结器,除非作为安全网或终止非关键的本地资源。 In those rare instances where you do use a finalizer, remember to invoke super.finalize. 在您使用终结器的极少数情况下,请记住调用super.finalize。 If you use a finalizer as a safety net, remember to log the invalid usage from the finalizer. 如果您使用终结器作为安全网,请记住记录终结器中的无效用法。

The only usecase I can think of for using finalize() is: 我可以想到使用finalize()的唯一用例是:

Suppose you have a static resource (member) in your class and want to do some cleanup, finalization or logging on that resource at the time of class being unloaded then you need to override finalize() method and do all that. 假设您的类中有一个静态资源(成员),并且想要在卸载类时对该资源执行一些清理,完成或登录,那么您需要覆盖finalize()方法并执行所有操作。

The short answer is never. 简短的回答永远不会。 Finalize() is full of subtle issues, and greatly slows garbage collections. Finalize()充满了微妙的问题,并且大大减慢了垃圾收集。

The longer answer is that maybe, maybe, during development, you want to check whether the important connection, file, socket or whatever was closed, and, if not, log a warning so that developers can investigate and properly fix the problem. 更长的答案是,也许,在开发期间,您可能想要检查重要的连接,文件,套接字或其他任何内容是否已关闭,如果没有,请记录警告,以便开发人员可以调查并正确解决问题。

finalize() does not only release resource like socket, but also manage the memory. finalize()不仅释放像socket这样的资源,还管理内存。 Theoretically speaking, you do not need to call finalize() explicitly in your code. 从理论上讲,您不需要在代码中显式调用finalize() It is performed at proper time by the VM. 它由VM在适当的时间执行。

Practically finalize() shouldnt be used especially shouldnt be used to clean up resources. 实际上finalize()不应该被使用,特别是不应该用于清理资源。 If a resource is acquired it is better from the performance perspective to explicitly release the resource rather than wait for JVM to call finalize() . 如果获取资源,从性能角度来看,最好显式释放资源,而不是等待JVM调用finalize() You cannot foresee when JVM will call finalize() and if you are done with the resource you would be holding it unnecessarily for longer. 您无法预见JVM何时会调用finalize(),如果您完成了资源,那么您将不必要地持续更长时间。 I have seen somebody nullifying variables inside the finalize method, which is another bad practice as it slows down the program by extending GC cycles, with completely unnecessary lines of codes. 我已经看到有人在finalize方法中使变量无效,这是另一个不好的做法,因为它通过扩展GC循环来减慢程序,完全不必要的代码行。 GC is designed to handle very well dead objects clean up process. GC旨在处理非常好的死对象清理过程。

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

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