Can't unloaded Groovy classes - PermGen Erros

540 views Asked by At

I have a legacy system that is extensively using Groovy version 1.0 and currently I can not update Groovy to an updated version. From time to time I'm getting a PermGen error due to the fact that all the Groovy classes/scripts are kept in memory even if no one is using them any more.

I'm trying to figure out what is the correct way to unload those classes after I've finished using them.

I'm using the following code to create the Script object:

GroovyShell shell=new GroovyShell();  
Script script = shell.parse("ANY_TEXT");  
.....

And I'm trying the following code to unload the generated class:

MetaClassRegistry metaClassRegistry = MetaClassRegistry.getIntance(0);  
metaClassRegistry.removeMetaClass(script.getMetaClass().getClass());  
metaClassRegistry.removeMetaClass(script.getClass());  
metaClassRegistry.removeMetaClass(script.getBinding().getClass());  
script.setBinding(null);  
script.setMetaClass(null);  
script = null;  

Unfortunately, this doesn't seems to work because I keep on getting a PermGen error. How can I unloaded Groovy classes and keep the PermGen size reasonable?

1

There are 1 answers

0
pczeus On

The reason you are experiencing this issue with Groovy is due to the nature of how scripting languages like Groovy work. They create a lot of extra Class objects behind the scenes, causing your PermGen space requirements to increase.

You can not force a Class loaded into the PermGen space to be removed. However, you can increase the PermGen space:

Or for additional options, like JVM settings to increase PermGen space and add settings to allow unloading of PermGen classes during GC, see this post:

Increase permgen space