RFR 9: 8138696 : java.lang.ref.Cleaner - an easy to use alternative to finalization

Kim Barrett kim.barrett at oracle.com
Tue Dec 8 18:44:59 UTC 2015


On Dec 8, 2015, at 7:44 AM, David Holmes <david.holmes at oracle.com> wrote:
> But thinking more on this approach this is simply not scalable. A
> Cleaner per cleanable-class could result in hundreds of threads being
> active!

That would indeed be awful.  However, the scope of a Cleaner should
not usually be a class.  More typically it would be a subsystem,
module, plugin, or some other sensible unit of sharing.  I assume
Peter's example had a class-scoped Cleaner for simplicity of
presentation.  A class-scoped Cleaner probably should be treated as a
canonical anti-pattern for Cleaner usage; appropriate in rare
circumstances, but usually a mistake.

> What exactly are the advantages over finalization again?

The same as PhantomReference-based cleanup vs finalization.

- Performance (especially after JDK-8071507).

- Semantic simplicity, avoiding the need to deal with resurrection of
  objects, and objects not needing to protect themselves from some
  kinds of post-cleanup misuse.






More information about the core-libs-dev mailing list