Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we switch off finalizers?

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.

like image 462
OldCurmudgeon Avatar asked Jan 06 '14 12:01

OldCurmudgeon


People also ask

What do you mean by finalizer?

finalizer in British English 1. a person, thing or event which finalizes a situation or process. 2. computing. a function in some programming languages which is run upon garbage collection.

What is a finalizer C#?

Finalizers (historically referred to as destructors) are used to perform any necessary final clean-up when a class instance is being collected by the garbage collector. In most cases, you can avoid writing a finalizer by using the System.


1 Answers

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.

like image 131
Stephen C Avatar answered Sep 30 '22 08:09

Stephen C