Skip to content

Avoid overriding finalize() #1465

@bt90

Description

@bt90

Is your feature request related to a problem? Please describe.

The current implementation to make Resource.setNonDisposehandler(Consumer) work, uses finalize() under the hood:

protected void finalize() {
if (ignoreMe) return;
if (nonDisposedReporter == null) return;
// If the Resource is GC'ed before it was disposed, this is a leak.
if (!resource.isDisposed())
nonDisposedReporter.accept(allocationStack);
}

This method is deprecated for removal since Java 18 as described in JEP 421.

Describe the solution you'd like

We should rewrite this functionality using PhantomReferences and a daemon thread polling from the shared ReferenceQueue.

Describe alternatives you've considered

The cleaner API might also work:

https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/ref/Cleaner.html

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions