On Tue, 17 Nov 2026 19:52:38 GMT, Markus Grönlund <[email protected]> wrote:

>> Greetings,
>> 
>> Repeated class retransformation can cause lambda invokedynamic call sites to 
>> be linked again, generating additional hidden lambda proxy classes. These 
>> classes are defined with STRONG_LOADER_LINK, preventing them from unloading 
>> independently of their defining class loader. Consequently, obsolete proxy 
>> classes accumulate across retransformation cycles, causing continued 
>> class-count and HotSpot Metaspace growth when the class loader remains 
>> alive. For built-in class loaders, that means for the lifetime of the JVM.
>> 
>> The problem is independent of the instrumentation mechanism that triggers 
>> retransformation. It can be reproduced without a third-party agent by 
>> repeatedly starting and stopping JFR recordings with method tracing enabled, 
>> while executing methods containing lambda call sites.
>> 
>> The fix introduces a necessary condition for lambda proxy classes to be 
>> defined with STRONG_LOADER_LINK: the caller class has not been 
>> redefined/retransformed.
>> 
>> The reproducers, before-and-after comparisons, and improvements are all 
>> detailed in the JIRA issue.
>> 
>> Testing:
>> 
>> - Tier 1 - 6
>> - Newly introduced test LambdaClassUnloading repeated 400 times.
>> 
>> ---------
>> - [x] I confirm that I make this contribution in accordance with the 
>> [OpenJDK Interim AI Policy](https://openjdk.org/legal/ai).
>
> Markus Grönlund has updated the pull request incrementally with one 
> additional commit since the last revision:
> 
>   Allow obsolete record and type-switch helper classes to unload

> FWIW, the suggestion to drop the STRONG link to the class loader has been 
> discussed in the past as well: 
> [#12493](https://github.com/openjdk/jdk/pull/12493)
> 
> In short: the motivation for having the strong link is that lambda classes 
> typically need the same lifetime as the enclosing class, so tying them to the 
> same class loader avoids needing additional memory for a separate class 
> loader data.

I understand the footprint argument for `STRONG` linkage in the common case. 
Like Volker, I still find it unfortunate that these dynamically generated 
implementation classes cannot be reclaimed when they would otherwise be 
unreachable. With the built-in class loaders, they effectively remain for the 
lifetime of the JVM. As more core-library functionality uses this dynamic 
machinery, that lifetime choice becomes increasingly consequential.

Redefinition and retransformation make the problem particularly clear. 
Previously linked `invokedynamic` call sites can be linked again, producing 
replacement implementation classes while the enclosing class remains alive. The 
obsolete generations remain strongly attached to the loader, so repeated 
instrumentation produces continuing class and Metaspace accumulation.

At least this change addresses that repeated accumulation: it preserves 
`STRONG` linkage in the common case and allows subsequently generated 
implementations to unload when the redefinition predicate is true and those 
implementations are no longer reachable.

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

PR Comment: https://git.openjdk.org/jdk/pull/33921#issuecomment-6961550961

Reply via email to