Why do finalizers have a “severe performance penalty”?

前端 未结 6 2011
花落未央
花落未央 2020-12-03 04:44

Effective Java says :

There is a severe performance penalty for using finalizers.

Why is it slower to destroy an object using t

6条回答
  •  忘掉有多难
    2020-12-03 05:18

    Having actually run into one such problem:

    In the Sun HotSpot JVM, finalizers are processed on a thread that is given a fixed, low priority. In a high-load application, it's easy to create finalization-required objects faster than the low-priority finalization thread can process them. Meanwhile, the space on the heap used by the finalization-pending objects is unavailable for other uses. Eventually, your application may spend all of its time garbage collecting, because all of the available memory is in use by objects pending finalization.

    This is, of course, in addition to the other many reasons to not use finalizers that are described in Effective Java.

提交回复
热议问题