When you create a Script, and then call run() on that Script, it is important to remove any entries created in the MetaClassRegistry after you are done. This is done by calling InvokerHelper.removeClass(script.getClass()). If you do not, the MetaClassRegistry will keep growing until you run out of memory.
Secondly, make sure that the GroovyShell is non-static, i.e. garbage collectable. Otherwise the GroovyClassLoader associated with the GroovyShell will hold on the all the classes (scripts) ever created.
来自: