import jdk.nashorn.api.scripting.NashornScriptEngineFactory; import javax.script.ScriptEngine; import javax.script.ScriptException; import java.lang.management.ClassLoadingMXBean; import java.lang.management.ManagementFactory; import java.util.Random; public class MemoryLeakTest { public static void main(String[] args) throws ScriptException { ClassLoadingMXBean classLoadingMXBean = ManagementFactory.getClassLoadingMXBean(); ScriptEngine engine = new NashornScriptEngineFactory().getScriptEngine(); for (int x=0; x<1000000;x ++){ //function has to be evald in a different call than actually executing the function, otherwise problem doesn't occur engine.eval("Wrapper = { smth: function() { return arguments.length } }"); //eval script with a random number to avoid hitting the compilation cache engine.eval("var blah = " + new Random().nextInt(100000) + " ; Wrapper.smth('/test', function() {})"); if(x % 1000 == 0){ System.out.println("Number of classes loaded: " + classLoadingMXBean.getLoadedClassCount()); } } } }