This is mostly out of curiosity.
I was wandering if anyone has encountered any good usage for Object.finalize() except for debugging/logging/profiling purposes ?
If you haven't encountered any what would you say a good usage would be ?
This is mostly out of curiosity.
I was wandering if anyone has encountered any good usage for Object.finalize() except for debugging/logging/profiling purposes ?
If you haven't encountered any what would you say a good usage would be ?
If your Java object uses JNI to instruct native code to allocate native memory, you need to use finalize to make sure it gets freed.
dispose()
or close()
that does the cleanup. –
Hairstreak close
them, but some native memory may be no problem, so we can handle it in the Java way. –
Clink finalize()
is “the Java way”, compared to using a “try with resource” or calling close()
manually? –
Gause Late to the party here but thought I would still chime in:
One of the best uses I have found for finalizers is to call explicit termination methods which, for what ever reason, were not called. When this occurs, we also log the issue because it is a BUG!
Because:
This leaves only a handful of tasks that they can address without much risk.
But it is just a fallback that is used is "normal" mechanism did not work. Normal mechanism should be initiated explicitly.
int
or long
representing an address or handle. When optimized code keeps that primitive value in a CPU register, the object can be garbage collected. –
Gause Release resources that should be released manually in normal circumstances, but were not released for some reason. Perhaps with write a warning to the log.
I use it to write back data to a database when using soft references for caching database-backed objects.
I see one good use for finalize(): freeing resources that are available in large amounts and are not exclusive.
For example, by default there are 1024 file handles available for a Linux process and about 10000 for Windows. This is pretty much, so for most applications if you open a file, you don't have to call .close() (and use the ugly try...finally blocks), and you'll be OK - finally() will free it for you some time later. However for some pieces of code (like intensive server applications), releasing resources with .close() is a must, otherwise finally() may be called too late for you and you may run out of file handles.
The same technique is used by Swing - operating system resources for displaying windows and drawing aren't released by any .close() method, but just by finalize(), so you don't have to worry about all .close() or .dispose() methods like in SWT for example.
However, when there is very limited number of resources, or you must 'lock' resource to use it, also remember to 'unlock' it. For example if you create a file lock on a file, remember also to remove this lock, otherwise nobody else will be able to read or write this file and this can lead to deadlocks - then you can't rely on finalize() to remove this lock for you - you must do it manually at the right place.
close()
. –
Clematis dispose()
is called and only when dispose()
is called. As long as a window is not explicitly disposed, it’s reachable via Window.getWindows()
and will not get garbage collected. So its finalize()
method would not get invoked, even if it had a finalizer. Further, the finalizer of a FileDescriptor
will close the handle, but not flush any buffers. So not closing a file related resource can lead to data loss. –
Gause © 2022 - 2024 — McMap. All rights reserved.