Skip to content

Instantly share code, notes, and snippets.

@jsmucr
Created June 29, 2022 05:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsmucr/e3c36fd3393fc1345ad3dfa711b46960 to your computer and use it in GitHub Desktop.
Save jsmucr/e3c36fd3393fc1345ad3dfa711b46960 to your computer and use it in GitHub Desktop.
Groovy @CompileStatic is slow and leaks resources
import groovy.lang.GroovyClassLoader;
class LeakDynamic {
public static interface TestClass {
String getThingy();
}
static String getTheThingy() {
return "the thingy";
}
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
for (int i = 0; i < 1000; i++) {
var cl = new GroovyClassLoader();
var c = cl.parseClass("class TestClass" + i + " implements LeakDynamic.TestClass { public String getThingy() { return LeakDynamic.getTheThingy(); } }");
var t = (TestClass) c.getConstructor().newInstance();
assert t.getThingy() == getTheThingy();
}
}
}
import groovy.lang.GroovyClassLoader;
class LeakStatic {
public static interface TestClass {
String getThingy();
}
static String getTheThingy() {
return "the thingy";
}
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
for (int i = 0; i < 1000; i++) {
var cl = new GroovyClassLoader();
var c = cl.parseClass("class TestClass" + i + " implements LeakStatic.TestClass { @groovy.transform.CompileStatic public String getThingy() { return LeakStatic.getTheThingy(); } }");
var t = (TestClass) c.getConstructor().newInstance();
assert t.getThingy() == getTheThingy();
}
}
}
#!/bin/sh
sdk install groovy 3.0.11
javac -cp ~/.sdkman/candidates/groovy/3.0.11/lib/groovy-3.0.11.jar LeakDynamic.java
# Lowering the heap to 16 MiB is still OK.
export JAVA_OPTS="-Xmx32M -XX:+HeapDumpOnOutOfMemoryError -Xlog:class+unload=info"
# Way faster than with CompileStatic.
time java $JAVA_OPTS -cp ~/.sdkman/candidates/groovy/3.0.11/lib/groovy-3.0.11.jar:. LeakDynamic
#!/bin/sh
sdk install groovy 3.0.11
javac -cp ~/.sdkman/candidates/groovy/3.0.11/lib/groovy-3.0.11.jar LeakStatic.java
# Lowering the heap to 16 MiB leads to OOM.
export JAVA_OPTS="-Xmx32M -XX:+HeapDumpOnOutOfMemoryError -Xlog:class+unload=info"
# Way faster than without CompileStatic.
time java $JAVA_OPTS -cp ~/.sdkman/candidates/groovy/3.0.11/lib/groovy-3.0.11.jar:. LeakStatic
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment