[ https://issues.apache.org/jira/browse/GROOVY-10957?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17696255#comment-17696255 ]
Nicolas Filotto edited comment on GROOVY-10957 at 3/3/23 6:14 PM: ------------------------------------------------------------------ To fix the leak, we need to remove the associated value for all existing instances of {{ClassValue}} by calling {{ClassValue#remove(Class)}}. In {{Groovy}}, the easiest way I found is by getting all the {{ClassInfo}} and calling {{InvokerHelper.removeClass(Class)}} on each underlying class when closing the {{ClassLoader}} as next: {noformat} for (ClassInfo ci : ClassInfo.getAllClassInfo()) { InvokerHelper.removeClass(ci.getTheClass()); } {noformat} Like I did in this commit https://github.com/essobedo/testCLLeak/commit/5a308e7e57a268ad7dadc12a356dba61be9e31a4 was (Author: JIRAUSER285918): To fix the leak, we need to remove the associated value for all existing instances of {{ClassValue}} by calling {{ClassValue#remove(Class)}}. In {{Groovy}}, the easiest way I found is by getting all the {{ClassInfo} and calling {{InvokerHelper.removeClass(Class)}} on each underlying class when closing the {{ClassLoader}} as next: {noformat} for (ClassInfo ci : ClassInfo.getAllClassInfo()) { InvokerHelper.removeClass(ci.getTheClass()); } {noformat} Like I did in this commit https://github.com/essobedo/testCLLeak/commit/5a308e7e57a268ad7dadc12a356dba61be9e31a4 > How to prevent ClassLoader leaks when using ClassValue? > ------------------------------------------------------- > > Key: GROOVY-10957 > URL: https://issues.apache.org/jira/browse/GROOVY-10957 > Project: Groovy > Issue Type: Question > Affects Versions: 4.0.6 > Reporter: Nicolas Filotto > Priority: Major > Attachments: JNI-Global-Reference.png > > > For my tests, I'm using > [rest-assured|https://github.com/rest-assured/rest-assured] which is > partially written in Groovy. > In my application, I use a custom {{ClassLoader}} to load rest-assured and > Groovy classes, after each test, the {{ClassLoader}} is closed and ready to > be collected by the GC. > In case, I launch my tests with {{-Dgroovy.use.classvalue=false}}, the > {{ClassLoaders}} are properly removed from the heap but when I use > {{-Dgroovy.use.classvalue=true}}, I end up with an OOME. > I can see thanks to my profiler that each {{ClassLoader}} is actually > retained by a JNI Global Reference to each primitive class ({{void.class}}, > {{float.class}}, {{boolean.class}}, {{int.class}}, {{double.class}}, > {{long.class}}, {{char.class}}, {{byte.class}}). > If I naively call {{InvokerHelper.removeClass(Class)}} on each of these > classes, my {{ClassLoaders}} don't have any GC root anymore but > unfortunately, they are still in the heap. > I created a small project to reproduce > https://github.com/essobedo/testCLLeak, any idea/help is more than welcome. > *_Environment:_* > +Java version:+ > {noformat} > openjdk version "11.0.18" 2023-01-17 > OpenJDK Runtime Environment Temurin-11.0.18+10 (build 11.0.18+10) > OpenJDK 64-Bit Server VM Temurin-11.0.18+10 (build 11.0.18+10, mixed mode) > {noformat} > +OS:+ > {noformat} > Darwin LT-C02CP37VMD6R 22.3.0 Darwin Kernel Version 22.3.0: Mon Jan 30 > 20:42:11 PST 2023; root:xnu-8792.81.3~2/RELEASE_X86_64 x86_64 > {noformat} -- This message was sent by Atlassian Jira (v8.20.10#820010)