3

I implemented a class in Java7. It does not inherit/implements anything. It uses Tess4J so I thought it would be nice to free the resources in the end. So I overrode the finalize() method like this:

@Override
protected void finalize() throws Throwable 
{
    try
    {
        TessAPI1.TessBaseAPIDelete(handle);
    }
    catch(Throwable t)
    {
        throw t;
    }
    finally
    {
        super.finalize();
    }
}

Netbeans 8.0.2 gives me warning for this method:

finalize declared()

The description on Netbeans website is not more useful to me:

warns about implementation of Object.finalize()

I didn't overrode any other method like equals or anything (maybe I should?). Can you tell me why I get this warning?

nuoritoveri
  • 2,494
  • 1
  • 24
  • 28

3 Answers3

4

finalize methods have the problem that they may be called at an arbitrary time by an arbitrary thread or even never at all. And like discussed in this question they may be called surprisingly early, i.e. when instance methods are still being executed, so using them to free a resource is quite dangerous.

So if they are not really useful for what is their original purpose, it’s legitimate to always warn when you are using them.


If you want to implement code for cleaning up a resource, when client code has forgotten to call close or dispose or whatever you provide for explicit resource management (which you should if there are associated native resources) can be done using a PhantomReference to the instance and a ReferenceQueue.

The advantage is that you have control over when to poll the queue and perform the cleanup and you may even opt-out the post-mortem cleanup by letting the PhantomReference go out of scope (it will be ordinarily collected and not enqueued) in the case that the client code did not forget to call close (it’s strongly recommended to implemented AutoClosable to allow using try with resources”). So this also solves the small performance issue that objects having a non-trivial finalize method must be collected twice as executing the finalize method implies that they become reachable again.

Community
  • 1
  • 1
Holger
  • 285,553
  • 42
  • 434
  • 765
2

Generally speaking, most Java programmers never have a reason to implement finalize(), since their own code only uses classes which are already managed (and therefore have their own finalizers). (See comments below the question).

Some developers might not be aware of how the garbage collector really works, and therefore rely on finalize() to do cleanup work that should be done somewhere else. This mistake can especially lead to the kind of defects that go unnoticed in testing, but cause failures in production.

For that reasons, I think a warning about finalize() is appropriate.

Erich Kitzmueller
  • 36,381
  • 5
  • 80
  • 102
0

finalize() is called when the jvm going to gc. so maybe the resource is not released as you wish.

use try finally instead.

roger zhan
  • 144
  • 3
  • The processing is called multiple times during object life and there are different types of it. Can I surround each processing method code with try-finally (will it be a good practice)? – nuoritoveri Dec 03 '14 at 12:09