OCW topic 7. Removing Finalization

Roger Riggs Roger.Riggs at oracle.com
Thu Aug 8 14:06:02 UTC 2019


Notes on the OpenJDK Committers Workshop topic:

The topic was focused on a transition where finalization as exposed by 
Object.finalize can be disabled and removed.
Intro comments attached in Finalize.txt.

Discussion included:

  * General agreement that finalization could/should be phased out
  * Need to document correct usage of cleaner and related mechanisms to
    replace use of Object.finalize
  * How can existing uses be flagged as warnings or errors?
    (javac and jdeprscan already reports overrides of deprecated
    Object.finalize)
  * Possibly logging or JFR logging of finalizer uses for analysis and
    reporting
  * Possibly switches to GC to flag or disable finalization as a way to
    see the effect of removal and to wean libraries/application off
    finalizer use
  * Gathering stats on canary/sample set of applications' use of
    finalize to advise on progress and advocate to mitigate its use
  * Possibly a flag to report loading of classes that override finalize
    after some particular JDK/bytecode version

Related off-topic discussion:

  * ByteBuffer releasing of Direct Buffers (not implemented using the
    finalize method override)
    The latency is still dependent on GC and not soon enough for some
    use cases
    The work on Panama Memory Segments with definite scope and release
    may be a replacement target for ByteBuffer use.

Regards, Roger



-------------- next part --------------
## Finalization!
- Reasons to remove
- Complexity and overheads in GC implementations
- Poor or weak semantics
- No requirement on timing or termination
  - (Implementations use best effort)


## Alternatives
- java.lang.ref.Cleaner
- ReferenceQueue implementations for handling Weak/Soft/Phantom Refs



## Progress
- Deprecated subclassing Object.finalize (@since JDK 9)
- Converted/remove most uses in base module
- Remaining work in some modules (Client, FX)



## How to Phase out use in 3rd party libraries
- Document, advertise, advocate, etc.
- How much time?
- Steps between here and there?



## Ideas
- Technically, spec allows actions to be deferred indefinitely
- Allow GC implementations to disable finalization
    - Add a switch to disable or enable
    - Pick a workable default and change it later
    - Applications may fail or degrade
# ...


More information about the workshop-discuss mailing list