[ 
https://issues.apache.org/jira/browse/GROOVY-12579?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18134519#comment-18134519
 ] 

ASF GitHub Bot commented on GROOVY-12579:
-----------------------------------------

codeconsole opened a new pull request, #3095:
URL: https://github.com/apache/groovy/pull/3095

   On JDK 17, linking a call site no longer leaves the call site's types in the 
`asType` cache of a method handle that outlives it: the JDK's shared identity 
handle (used for casts) and argument collector (used by `catchException`), and 
the runtime's static guard and filter handles. JDK 17 keeps that cache 
strongly, so a cast to a class of a discarded script or Groovy copy kept its 
class loader reachable. JDK 18 and later take the unchanged path.
   
   On JDK 17 these conversions are now made on a new handle that invokes the 
long-lived one and that only the call site holds, and `catchException` is 
applied to the target with its reference types erased (after making a varargs 
target fixed arity), so the JDK's collector is converted to a type of JDK 
classes only.
   
   Tests: 
`bugs.Groovy12142.testDynamicCallsKeepNoDiscardedScriptLoaderReachable` runs a 
script with casts, typed receivers, null arguments, null-safe calls and a 
failing call in a `GroovyClassLoader` and checks that the loader is collected 
once dropped; it fails on JDK 17 without the change. The `groovy`, `gls`, 
`bugs` and `org.codehaus.groovy` packages pass (15,496 tests). The Checkstyle 
and CodeNarc gates pass. `bugs.Groovy12142` also passes on JDK 17.
   
   Seen on JDK 17 with 4.0.33, 5.0.8 and 6.0.0.
   




> Class loaders of discarded scripts stay reachable through method handle 
> caches on JDK 17
> ----------------------------------------------------------------------------------------
>
>                 Key: GROOVY-12579
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12579
>             Project: Groovy
>          Issue Type: Bug
>          Components: groovy-runtime
>    Affects Versions: 4.0.33, 5.0.8, 6.0.0
>            Reporter: Scott Murphy Heiberg
>            Priority: Major
>
> {{MethodHandle.asType}} caches its result on the handle converted. JDK 17 
> keeps that cache as a strong reference to the last conversion; JDK 18 and 
> later keep a conversion strongly only when the handle's own types keep the 
> classes of the new type reachable. Linking an indy call site converts handles 
> to the call site type, so on JDK 17 a handle that outlives the call site 
> keeps the classes of that type, and their class loader, reachable:
> * a cast converts {{MethodHandles.identity(Object.class)}}, which the JDK 
> shares across the JVM;
> * {{catchException}} converts the JDK's shared argument-array collector to 
> the type of its target, which every linked method call goes through;
> * the guard and filter handles in static fields of the runtime ({{IS_NULL}}, 
> {{NON_NULL}}, {{UNWRAP_METHOD}}, and the handle being cast at the end of 
> linking).
> A script in a {{GroovyClassLoader}} that casts to its own class is therefore 
> never released on JDK 17:
> {code:groovy}
> import java.lang.ref.WeakReference
> import groovy.transform.CompileStatic
> @CompileStatic
> WeakReference<ClassLoader> runDiscarded() {
>     def loader = new GroovyClassLoader(this.class.classLoader)
>     Script script = (Script) loader.parseClass('''
>         class Holder { String label = 'h'; Holder self(Holder o) { o ?: this 
> } }
>         def held = new Holder()
>         Holder holder = held
>         holder.self(null).label
>     ''').getDeclaredConstructor().newInstance()
>     script.run()
>     script = null
>     loader.close()
>     new WeakReference<ClassLoader>(loader)
> }
> @CompileStatic
> boolean collected(WeakReference<ClassLoader> ref) {
>     for (int i = 0; i < 100; i++) {
>         System.gc()
>         if (i % 50 == 10) {
>             try { List<byte[]> hog = []; while (true) hog << new byte[(int) 
> Math.max(1024L, Runtime.runtime.freeMemory() >> 2)] } catch (OutOfMemoryError 
> ignored) { }
>         }
>         if (ref.get() == null) return true
>         Thread.sleep(10)
>     }
>     false
> }
> println "collected: ${collected(runDiscarded())}" // -Xmx256m: false on JDK 
> 17, true on JDK 21
> {code}
> The class loader cleanup that the integration guide documents ends with such 
> a cast, so on JDK 17 it does not release the class loader of the Groovy copy 
> it cleans up either.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to