0

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?

tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • Do you have this error if you aren't trying to do it? – Roman C Jan 24 '16 at 08:09
  • Have you tried dropping a more recent version of groovy in? Just to see if it solved the problem? You could then take that to the people who decide things can't be updated, to help argue your case for sanity – tim_yates Jan 24 '16 at 08:59
  • The problem occur only when using Groovy. – Itay Hudedi Jan 24 '16 at 09:39
  • I can't update my groovy version because it's a legacy system – Itay Hudedi Jan 24 '16 at 09:39
  • 1
    If you can't fix bugs in it, why the question? Just leave it to fester – tim_yates Jan 24 '16 at 09:58
  • have you tried creating a new shell every so often, rather than reusing the old one? But I know a lot of stuff was fixed well after groovy 1.0. I still think you need to upgrade – tim_yates Jan 24 '16 at 10:55
  • What java veresion and garbage collection settings are you using? Some strategies require specific JVM flags to tell it to collect from permgen - so if you don't have those the GC won't even be trying to clear it - see: http://stackoverflow.com/a/28446672/258813 – rhinds Jan 25 '16 at 16:47

1 Answers1

0

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

Community
  • 1
  • 1
pczeus
  • 7,709
  • 4
  • 36
  • 51