5

From Effective Java:

Oh, and one more thing: there is a severe performance penalty for using finalizers. On my machine, the time to create and destroy a simple object is about 5.6 ns. Adding a finalizer increases the time to 2,400 ns. In other words, it is about 430 times slower to create and destroy objects with finalizers.

What makes the Finalizers so costly ?

6
  • 3
    Wow, if you are cutting the nanosecond in half you probably shouldn't be using Java but consider a language that is closer to the metal :-) Jul 17, 2012 at 14:03
  • 1
    0.00000024 seconds isn't too much. If you're going to go through it like a million times then it might be 2 seconds.
    – alexyorke
    Jul 17, 2012 at 14:08
  • BTW In a finalize() method you can make the object live again. e.g. by adding it to a collection. :P Jul 17, 2012 at 14:14
  • Duplicate question... stackoverflow.com/questions/2860121/…
    – Flavio
    Jul 17, 2012 at 14:15
  • 1
    @alexy13 perhaps a million of these objects leads to a 2 second delay; however, if you do go through these objects frequently, then not using finalizers allow you to go through 430 of these objects. Without clear understanding of the Object's role, it is not a clear cut choice to "not worry about it". Perhaps they are transients in a rather large matrix calculation, in which case, 430 times faster would mean much larger matricies could be handled. Yes, you are generally right, but there are always exceptions.
    – Edwin Buck
    Jul 17, 2012 at 14:30

3 Answers 3

5

http://www.ibm.com/developerworks/java/library/j-jtp01274/index.html

extract from link above

Objects with finalizers (those that have a non-trivial finalize() method) have significant overhead compared to objects without finalizers, and should be used sparingly. Finalizeable objects are both slower to allocate and slower to collect. At allocation time, the JVM must register any finalizeable objects with the garbage collector, and (at least in the HotSpot JVM implementation) finalizeable objects must follow a slower allocation path than most other objects. Similarly, finalizeable objects are slower to collect, too. It takes at least two garbage collection cycles (in the best case) before a finalizeable object can be reclaimed, and the garbage collector has to do extra work to invoke the finalizer. The result is more time spent allocating and collecting objects and more pressure on the garbage collector, because the memory used by unreachable finalizeable objects is retained longer. Combine that with the fact that finalizers are not guaranteed to run in any predictable timeframe, or even at all, and you can see that there are relatively few situations for which finalization is the right tool to use.

5

The crucial difference is the very existence of a finalizer method, irrespective of what it does. As soon as the GC has finalizable objects, it must work much harder to get all the housekeeping right. It must mark them as finalizable, keep them in a pool, run finalization, check whether they're resurrected after that, juggle all kinds of multithreaded issues with finalizing code, and so on.

The difference can be seen most starkly in the GC's young generation: in an optimistic, and not entirely rare scenario, the GC'ing boils down to a single operation: decrementing the pointer to the first available memory address. With finalizers, it's the whole process outlined above.

2
  • You probably need to rephrase "The crucial difference is the very existence of a finalizer method," as "The crucial difference is the very existence of a non trivial finalizer method," since every object has a finalize() method .
    – Geek
    Jul 17, 2012 at 14:21
  • Actually, if I delete the word "method", I'll be in line with the JLS. However, I left it there because it seemed more obvious that way. Jul 17, 2012 at 14:22
0

When you benchmark, you probably benchmark the useful life of the Object.

Objects exist beyond their useful life, basically there is a time delay between having the last program reference disappear and the garbage collection routines (which run in a different thread) detect the detached Object. When you add a finalizer, your "useful life" of the Object now extends until after garbage collection detects the detached object, and after the JVM runs the finalization method.

It isn't 100% because a finalization method exists that finalization adds so much time, a lot of that time is waiting for the garbage collection threads to wake up and eventually stumble upon your detached object (and decide to do something about it).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.