Stefan Majer | 18 Jun 15:05

[groovy-dev] OOM because ScriptEnginge Memory caches all ever compiled Groovy Classes

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;
               }
       }
}
(Continue reading)

Alexander Veit | 18 Jun 22:55

RE: [groovy-dev] OOM because ScriptEnginge Memory caches all ever compiled Groovy Classes

Stefan,

> Stefan Majer [mailto:stefan.majer@...] 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


Gmane