javaperformancefinalfinalizer

Java: Do empty finalizers also have performance impact?


I am aware that finalizers in Java have serious performance issues - see this thread for details: Why do finalizers have a "severe performance penalty"?

Now I have a scenario where I want to prohibit subclasses of a certain class from having finalizers. AfaIk, this can be done via adding a final empty finalizer:

    protected final void finalize() throws Throwable {}

Would such an empty finalizer already trigger the known performance issues? Or would the GC detect it as an empty finalizer and treat objects like ordinary non-finalizable objects?

Reason for this is a framework that uses its own, controlled finalization process. Users implementing subclasses of the mentioned class could break this process by adding their own Java finalizers. Additionally it would force them to use the intended finalization process if finalization is needed at all.


Solution

  • No, empty finalizers don't have any performance cost; they get optimized out, so that the Finalizer mechanism can ignore them. This link implies that, and this one explicitly states it.

    Fortunately, when our finalize() method is trivial, that is, it has an empty body, it is ignored by the Finalizer mechanism