package redbridge; import java.io.StringReader; import javax.script.Invocable; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; public class MemoryLeak { private MemoryLeak() { try { ScriptEngineManager man = new ScriptEngineManager(); long end = System.currentTimeMillis() + (5 * 60000); System.out.println("Generating garbage for 5 mins..."); while(end > System.currentTimeMillis()) { /* * creating the engine here corresponds to our real-world * case, but you can move the two wollowing lines outside the * loop with the same result */ ScriptEngine engine = man.getEngineByExtension("rb"); engine.eval(new StringReader("def methodA() y = 10 end")); ((Invocable)engine).invokeFunction("methodA"); /* * We'll do a small wait here to let the VM breathe... */ Thread.sleep(5); } /* * And here we'll simply hang the system so that * a memory dump can be created etc... */ System.out.println("Garbage generated, hanging..."); synchronized(this) { this.wait(); } } catch(Exception e) { e.printStackTrace(); } } public static void main(String[] args) throws ScriptException { new MemoryLeak(); } }