After precompiling my javaScript and loading generated classes with org.mozilla.javascript.GeneratedClassLoader I stored scripts main class in a variable and tried to generate a lot of instances (~200) from it using newInstance() method.
At some point java.lang.OutOfMemoryError: PermGen space hit me (stack is in the end of this mail).
When reading the stack I started to think what is the difference between using precompiled script class and scope where compiled (ctx.compileReader) script is executed? Precompiled solution seems just slightly faster but several instances take up more space than multiple scopes in the memory. And precompiled instances fill up permgen not heap and thus is harder to collect.
A thought while writing this: Am doing it all wrong and should be using javascript constructor instead and call newInstance just once??
java.lang.OutOfMemoryError: PermGen space
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at org.mozilla.javascript.DefiningClassLoader.defineClass(DefiningClassLoader.java:62)
at org.mozilla.javascript.optimizer.Codegen.defineClass(Codegen.java:146)
at org.mozilla.javascript.optimizer.Codegen.createScriptObject(Codegen.java:101)
at org.mozilla.javascript.Context.compileImpl(Context.java:2393)
at org.mozilla.javascript.Context.compileString(Context.java:1343)
at org.mozilla.javascript.Context.compileString(Context.java:1332)
at org.mozilla.javascript.tools.shell.Main.loadScriptFromSource(Main.java:456)
at org.mozilla.javascript.tools.shell.Main.processFileSecure(Main.java:444)
at org.mozilla.javascript.tools.shell.Main.processFile(Main.java:413)
at org.mozilla.javascript.tools.shell.Global.load(Global.java:247)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:161)
at org.mozilla.javascript.FunctionObject.call(FunctionObject.java:415)
at org.mozilla.javascript.optimizer.OptRuntime.callName(OptRuntime.java:97)
at AppHtmlBuilder1._c0(htmlGen:11)
at AppHtmlBuilder1.call(htmlGen)
at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:401)
at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3003)
at AppHtmlBuilder1.call(htmlGen)
at AppHtmlBuilder1.exec(htmlGen)
at org.mozilla.javascript.JavaAdapter$2.run(JavaAdapter.java:588)
at org.mozilla.javascript.Context.call(Context.java:499)
at org.mozilla.javascript.ContextFactory.call(ContextFactory.java:511)
at org.mozilla.javascript.JavaAdapter.runScript(JavaAdapter.java:583)
at AppHtmlBuilder.<init>(<adapter>)
at sun.reflect.GeneratedConstructorAccessor11.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
Best regards
Priit