Every time a script is evaluated PermGen is keeping references to the classes created by Groovy, causing inevitably an OutOfMemoryError when working with heavy load. As stated in GROOVY-5187 we can see it is a Groovy issue. The last was reverted by GROOVY-5786 because of regression, but includes a workaroud by passing one setting to ScriptContext. Here JSR 223 Scripting with Groovy explains how.
I tried the solution just by passing this setting (scriptEngine.getContext().setAttribute("#jsr223.groovy.engine.keep.globals", "weak", ScriptContext.ENGINE_SCOPE); ) before each evaluation of script in ScriptingEngines.java but it didn't work, as I could see monitoring the JVM. Then I used the solution of the attached patch, just keeping a Map of ScriptEngine and configure each once. I don't know if this is a good approach, any hints will be appreciated.
IBM(R) 32-bit SDK for Windows(R), Java(TM) Technology Edition, Version 6