Hi Rick,
Allow me to join the discussion...
On 05/04/2017 04:21 AM, Rick Hillegas wrote:
Thanks, Roger. That is a helpful example.
Derby is a component which can be quiesced and unloaded on
resource-constrained platforms, then re-loaded when resources free up.
Unloading means garbage collecting all the classloaders created by the
component so that, at the end, not a single scrap of Derby code
remains loaded. Has the Cleaner pattern been tested under these kinds
of conditions? Can you point me at a Cleaner-based library which
successfully unloads and reloads itself without leaking classloaders?
Can you point me to a place in Derby code where you are currently using
finalize() method for your purpose and I'll try to show you how to
convert this to Cleaner API...
I'm surprised that you actually need finalize() in Derby. Isn't this
pure Java code? Usually finalize() is needed in situations where there's
some non-Java resource which has to be cleaned-up when all the
references to some Java object that represents it are gone. I'm curious
what you need to clean in a pure Java library.
Regards, Peter
Thanks,
-Rick
On 5/3/17 9:04 AM, Roger Riggs wrote:
Hi Rick,
The general nature of changes to use the Cleaner involve factoring
out the cleanup
code from the object being cleaned, though in many cases it does not
require restructuring.
For example, if an object holds a reference to a network socket that
needs to be closed
when the object is collected the socket.close() can called by the
cleaner:
Cleaner cleaner = ...;
final Socket socket = ...;
Object obj = ...;
cleaner.register(obj, () -> {
try {
socket.close();
} catch (IOException ioe) { // ignore}
});
Creating a cleaner starts a thread that does the work so you'll want
to decide
how to share it across the uses or to use than one.
Using lambdas for the cleaner functions is very lightweight but be
careful to avoid
the using bound variables in the lambda body because they implicitly
retain
a reference to the enclosing instance which will prevent the instance
from becoming unreferences.
If there are more specific cases of interest let me know,
Regards, Roger
On 5/2/2017 10:08 PM, Rick Hillegas wrote:
When I compile Apache Derby using JDK 9 build 167, I see several
instances of the following warning:
warning: [deprecation] finalize() in Object has been deprecated
The javadoc for java.lang.Object.finalize() suggests that affected
classes should migrate their finalization to a coding pattern based
on the newly introduced java.lang.ref.Cleaner class. I am hesitant
to try my hand at this without more guidance. Can you point me at a
tutorial or list of best practices for implementing Cleaner-based
finalization?
Thanks,
-Rick