Created
June 29, 2022 05:42
-
-
Save jsmucr/e3c36fd3393fc1345ad3dfa711b46960 to your computer and use it in GitHub Desktop.
Groovy @CompileStatic is slow and leaks resources
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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