RFR: 8078641: MethodHandle.asTypeCache can retain classes from unloading

Vladimir Ivanov vlivanov at openjdk.java.net
Wed Aug 25 11:25:37 UTC 2021


`MethodHandle.asTypeCache` keeps a strong reference to adapted `MethodHandle` and it can introduce a class loader leak through its `MethodType`.

Proposed fix introduces a 2-level cache (1 element each) where 1st level can only contain `MethodHandle`s which are guaranteed to not introduce any dependencies on new class loaders compared to the original `MethodHandle`. 2nd level is backed by a `SoftReference` and is used as a backup when the result of `MethodHandle.asType()` conversion can't populate the higher level cache.  

The fix is based on [the work](http://cr.openjdk.java.net/~plevart/jdk9-dev/MethodHandle.asTypeCacheLeak/) made by Peter Levart @plevart back in 2015.

Testing: tier1 - tier6

-------------

Commit messages:
 - Remove the assert
 - Introduce 2nd level MethodHandle.asType cache
 - keepsAlive logic

Changes: https://git.openjdk.java.net/jdk/pull/5246/files
 Webrev: https://webrevs.openjdk.java.net/?repo=jdk&pr=5246&range=00
  Issue: https://bugs.openjdk.java.net/browse/JDK-8078641
  Stats: 108 lines in 2 files changed: 87 ins; 3 del; 18 mod
  Patch: https://git.openjdk.java.net/jdk/pull/5246.diff
  Fetch: git fetch https://git.openjdk.java.net/jdk pull/5246/head:pull/5246

PR: https://git.openjdk.java.net/jdk/pull/5246


More information about the core-libs-dev mailing list