Copied from my comment at
https://issues.apache.org/jira/browse/GROOVY-7591 (partially in response
to what Craig added there yesterday):
To me the ideal solution would be to drop the "groovy.use.classvalue"
system property in Groovy 2.4.7, to always use ClassValue (with Java 7
and later), but to give the Java VM a ClassValue that it can garbage
collect already without a JVM fix.
Why drop the system property: The matter is complex enough, maintaining
more variants than absolutely necessary should be avoided.
Why not wait for a fix in the Java VM: Because even if/when it comes to
Java 9/8/7, it will still require people to upgrade their VMs which is
not a given at all.
Here is a first attempt to resolve this as simply as possible in
principle: A modified version of the test sources that were submitted
with the Java VM bug report.
I am simply wrapping the MyClassValue class with another instance of
ClassValue that only has weak references to the MyClassValue instance.
This worked on Java 7 and 8. With Java 7 I saw used PermGen rise close
to the configured maximum and then it was collected. With Java 8,
Metaspace did not even rise, which is important because by default there
is no maxium set for Metaspace.
-- See https://issues.apache.org/jira/browse/GROOVY-7591 for Java sources --
I wrap the classes resulting from compiling MyClassValue and
MyClassValueWrapper into t/t.jar and then run the test with "java -ea
-XX:MaxMetaspaceSize=64m -cp . CVTest" resp. "java -ea
-XX:MaxPermGenSize=64m -cp . CVTest".
I know that there is already much more specific work going on with
regard to fixing these issues and there might be more issues with
garbage collecting classes in the Groovy code - I just wanted to say
that it might be good to strive for a solution with minimal complexity.
PS: I hope it is OK to copy this, when I post here, code gets broken up
with line breaks, making it possibly harder to read/try out, on the
other hand that issue seems like maybe not the ideal place to discuss
the issue?
Alain