A quote from Effective Java states that:
A second legitimate use of finalizers concerns objects with native peers. A
native peer is a native object to which a normal object delegates via native methods. Because a native peer is not a normal object, the garbage collector doesn’t
know about it and can’t reclaim it when its Java peer is reclaimed. A finalizer is an
appropriate vehicle for performing this task, assuming the native peer holds no
critical resources.
I’ve not done C++ before, though I’m vaguely aware that file handlers and database connections are critical resources. But what exactly does it mean for a resource to be non-critical?
Or rather, what are some examples of non-critical resources?
I don’t think it’s really the resource that’s critical, despite the phrase used. I think it’s recovering the resource that may or may not be critical, and the quote could be rephrased, “assuming it is not critical that the resource is freed”.
If it’s critical that the resource is freed by a particular point in program execution, after the object is unreachable but before the resource is needed for some other purpose, then a finalizer is inadequate. Instead you need some program logic to make sure it happens.
So, file handles or db connections are critical if you’re worried that you might run out, they’re not critical otherwise. If you’ve reached some limit of open DB connections, because the finalizers that would close your old ones haven’t been run yet, and you try to open another DB connection, chances are it’ll fail. The situation with memory is rather better, since if you’ve run out of memory because of unreachable objects, and try to create a new object, then the GC will at least make an effort to find something to finalize and free.
Thus, file handles and db connections should have a
close()function that the user can call to free all resources in cases where the program logic is able to determine that the object will not be used again. Expecting the GC to close the connection via a finalizer isn’t reliable enough. It also doesn’t deal well with the possibility of a flush or commit failing, although that’s a separate issue.