Can we switch off finalizers?

别等时光非礼了梦想. 提交于 2019-12-04 17:26:03

问题


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.


回答1:


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.




回答2:


It is possible to suppress finalization on certain objects. It does not require bytecode manipulation as one commenter noted.

The procedure is described here and source code is provided. The class java.lang.ref.Finalizer is responsible for maintaining a list of objects that have not yet been finalized. To suppress finalization of your objects of interest, it suffices to use reflection APIs to acquire a lock on the field lock, iterate over the linked list unfinalized that the Finalizer class maintains, and remove your object from this list.

I've tried this method to safely instantiate custom-serialized objects while bypassing constructor invocation, and avoiding problems when the finalizer would be invoked with normal GC. Before applying this method my JVM would break with hard faults in the finalizer thread; since applying this method the faults do not occur.




回答3:


You cannot switch off finalizer in java, but all you can do is write code that will facilitate GC :-).



来源:https://stackoverflow.com/questions/20950086/can-we-switch-off-finalizers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!