java - 终结器对 JVM 的性能影响

标签 java finalizer

根据 this post , 在 .Net 中,

Finalizers are actually even worse than that. Besides that they run late (which is indeed a serious problem for many kinds of resources), they are also less powerful because they can only perform a subset of the operations allowed in a destructor (e.g., a finalizer cannot reliably use other objects, whereas a destructor can), and even when writing in that subset finalizers are extremely difficult to write correctly. And collecting finalizable objects is expensive: Each finalizable object, and the potentially huge graph of objects reachable from it, is promoted to the next GC generation, which makes it more expensive to collect by some large multiple.

这是否也适用于一般的 JVM,尤其适用于 HotSpot?

最佳答案

Here是 2004 年的明确声明:

Objects with finalizers (those that have a non-trivial finalize() method) have significant overhead compared to objects without finalizers, and should be used sparingly. Finalizeable objects are both slower to allocate and slower to collect. At allocation time, the JVM must register any finalizeable objects with the garbage collector, and (at least in the HotSpot JVM implementation) finalizeable objects must follow a slower allocation path than most other objects. Similarly, finalizeable objects are slower to collect, too. It takes at least two garbage collection cycles (in the best case) before a finalizeable object can be reclaimed, and the garbage collector has to do extra work to invoke the finalizer. The result is more time spent allocating and collecting objects and more pressure on the garbage collector, because the memory used by unreachable finalizeable objects is retained longer. Combine that with the fact that finalizers are not guaranteed to run in any predictable timeframe, or even at all, and you can see that there are relatively few situations for which finalization is the right tool to use.

关于java - 终结器对 JVM 的性能影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2954948/

相关文章:

.net - 如何识别GC Finalizer线程?

memory-leaks - Jetty 8 服务器上的内存泄漏

java - 使用 OCR 检测扫描文档是否创建 PDF [pdfbox]

java - Hazelcast 服务器和客户端出现异常

java - 如何生成java实例的完整json字符串

java - +获取当前工作目录的IntelliJ问题

java - 如何在Java中实现对象计数器

.net - 设置字体真的有多重要?

c# - 紧凑的框架 - 离线时 HttpWebRequest 资源泄漏

java - Hadoop Mapreduce 自定义拆分/自定义记录读取器