我们可以关闭终结器吗? [英] Can we switch off finalizers?

查看:108
本文介绍了我们可以关闭终结器吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于几乎不能保证什么时候甚至终结者运行,终结者现在几乎被认为是一种气味 - 是否有任何方法说服JVM完全跳过所有终结过程?

我问,因为我们有一个庞大的应用程序,当它移动到一个更新的JVM时(不知道在这个阶段哪一个)被看起来非常像已知的问题与finalisers(抛出异常并因此非常缓慢的GC)。



新增



href =https://stackoverflow.com/q/7644556/823393> java内存泄漏故障排除:最终化?建议在终结器中引发异常时出现主要问题,因为这会降低 $ b

我的问题显示,当内存变得很低并且堆转储分析显示大量 Finalizer objects(ove 10,000,000美元) - 暗示我认为经济放缓可能是他们的错,因为他们拖延了GC。很明显,我可能是错的。



我没有权力要求重构。

解决方案

有什么办法可以说服JVM完全跳过所有的终结过程?但是除非你的大部分对象都有 finalize 方法和/或 finalize 方法特别昂贵,我认为他们不太可能让GC非常慢。我希望问题是别的。



我建议您打开GC日志记录以尝试更好地了解实际发生的情况。



但我也同意,重构代码以摆脱 finalize()方法在长期运行中可能是一件好事。 (很少有情况下使用 finalize 确实是最好的解决方案。)






UPDATE - 您的新证据非常有说服力,虽然不是证明!


我没有权力要求重构。

然后,我建议你将证据放在做这些事的人的脚下: - )。

或者,你可以添加一个异常处理程序犯罪嫌疑人敲定方法来查看他们是否 抛出异常。 (如果他们是,然后改变他们,以避免抛出异常...)

但是,最终结果是,如果最终确定是您的性能问题的真正原因那么最好的(也可能是唯一的)方法就是改变代码。


As there is little guarantee about when and even if finalizers run and finalizers are almost considered a smell nowadays - is there any way to persuade the JVM to completely skip all finalization processes?

I ask because we have a mammoth application which, when moved to a newer JVM (not sure which at this stage) is brought to its knees by what looks very much like the known problems with finalisers (exceptions being thrown and therefore very slow GC).

Added

There is some discussion on Troubleshooting a java memory leak: finalization? where it is suggested that the primary problem arises when exceptions are thrown within finalizers because that slows down the finalization process dramatically.

My issue shows as a dramatic slow-down when memory becomes low and analysis of heap dumps show a large number of Finalizer objects (over 10,000,000) - suggesting to me that the slowdown could be their fault because they are delaying the GC. Obviously I may be wrong.

I do not have the power to demand a refactor.

解决方案

Is there any way to persuade the JVM to completely skip all finalization processes?

In a word No.

But unless a large proportion of your objects have finalize methods and/or the finalize methods are particularly expensive, I think that they are unlikely to make GC "very slow". I expect the problem is something else.

I suggest that you turn on GC logging to try and get a better picture of what is actually happening.

But I also agree, that refactoring the code to get rid of the finalize() methods would probably be a good thing in the long run. (There are very few situations where using finalize is genuinely the best solution.)


UPDATE - your new evidence is pretty convincing, though not a proof!.

I do not have the power to demand a refactor.

Then, I suggest you place the evidence at the feet of the people who do :-).

Alternatively, you could add an exception handler to the suspect finalize methods to see if they are throwing exceptions. (And if they are, then change them to avoid the exceptions being thrown ...)

But the bottom line is that if finalization is the real cause of your performance problems then the best (and probably the only) way to cure them is to change the code.

这篇关于我们可以关闭终结器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆