4

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.

Community
  • 1
  • 1
stewori
  • 586
  • 4
  • 12
  • Test it out and share your findings? – Kayaman Jul 30 '14 at 12:53
  • 3
    Performance issues? This is the kind of nano-optimization that you shouldn't concern yourself with unless you've measured your app and have evidence that finalizers are the cause. Your code is far more likely to be the source of performance problems. – duffymo Jul 30 '14 at 12:54
  • You know this is but one way to seriously hamper the efficiency of the garbage collector and you need to be programming through slamming your head on the keyboard to go as far as making this mistake. Do you really want to go this far? – Gimby Jul 30 '14 at 13:27
  • Could you be more specific, why it could hamper the gc efficiency if one prohibits finalizers for certain classes? – stewori Jul 31 '14 at 17:45

1 Answers1

5

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

zb226
  • 9,586
  • 6
  • 49
  • 79
resueman
  • 10,572
  • 10
  • 31
  • 45