OOM because ScriptEnginge Memory caches all ever compiled Groovy Classes

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

OOM because ScriptEnginge Memory caches all ever compiled Groovy Classes

Stefan Majer-2
Hello,

im using Groovy in a Java Applikation through ScriptEngineManager.
Im calling Groovy scripts every few seconds and im experiencing a
Memory Leak in my applikation.

To demostrate you can use this examples:

With Memory Leak:

public class GroovyLauncherWithLeak {
       public static void main(String[] args) throws InterruptedException,
ScriptException {
               ScriptEngineManager manager = new ScriptEngineManager();
               engine = manager.getEngineByName("groovy");
               int counter;
               while (true) {
                       StringBuilder sb = new StringBuilder();
                       sb.append("return \"hello
").append(counter).append(" \"");
                       long start = System.currentTimeMillis();
                       engine.eval(sb.toString()9;
                       System.out.println("Duration: " +
(System.currentTimeMillis() -
start) + "ms" );
                       Thread.sleep(50L);
                       counter += 1;
               }
       }
}

Run this an watch the JVM growing till OOM (PermGenSize)
Heapdump shows as much Instances of a Script Object how often this
loop was running.

Without Memory Leak:

public class GroovyLauncherWithLeak {

       private static ScriptEngineManager manager = new
ScriptEngineManager();
       private static ScriptEngine engine =
manager.getEngineByName("groovy");

       public static void main(String[] args) throws InterruptedException,
ScriptException {
               int counter;
               while (true) {
                       StringBuilder sb = new StringBuilder();
                       sb.append("return \"hello
").append(counter).append(" \"");
                       long start = System.currentTimeMillis();
                       engine.eval(sb.toString()9;
                       System.out.println("Duration: " +
(System.currentTimeMillis() -
start) + "ms" );
                       Thread.sleep(50L);
                       counter += 1;
               }
       }
}


From my point of view there must be a way to specify how ScriptEnginge
caches the compiled classes. Any Ideas ?


Greetings

Stefan Majer

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

RE: OOM because ScriptEnginge Memory caches all ever compiled Groovy Classes

Alexander Veit
Stefan,

> Stefan Majer [mailto:[hidden email]] wrote:
>
> From my point of view there must be a way to specify how
> ScriptEnginge caches the compiled classes. Any Ideas ?

JSR 223 does not specify if or how script engines may cache scripts. So
there is no API to tell script engines what to cache and what not.

However the Groovy ScriptEngine is Compilable. So you can implement a cache
for CompiledScripts in your application and reuse the cached script
instances for equal scripts. Since the number of distinct scripts is finite
this should avoid the perm space problem in most cases.

You also could integrate Groovy natively. This gives you slightly more
control over the class cache (see GroovyClassLoader#clearCache()).

Regards,
Alex


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email