Sunday, August 29, 2010

How to fix "OutOfMemoryError: PermGen space" errors when running Cobertura from Maven2

Recently while working on a project, I was faced with ugly OutOfMemoryError being thrown while running cobertura.

We used Maven2 as the build tool on the project.

I tried various things to fix it but without luck.

A few of those were:

  • Setting the MAVEN_OPTS=-Xms512m -Xmx1024m -XX:MaxPermSize=512m
  • Setting the JVM_OPTS=-Xms512m -Xmx1024m -XX:MaxPermSize=512m
  • Trying the <maxmem> property of cobertura in pom.xml
None of them fixed the issue.

The real problem is surefire plug-in forks a new jvm to run the tests and this jvm does not seem to inherit the memory settings.

So what is the way around is?

We can use the argLine configuration property of the maven-surefire-plugin to bump up the memory.

The above code means:

  • Initial java heap size is 512m
  • Maximum java heap size is 1024m 
  • Maximum size of permanent generation heap is 512m.  
Basically all we are doing is bumping up the memory used by the surefire plugin to run the tests.

All said and done, the real test is in the real world.

After those settings I ran mvn clean cobertura:cobertura and Yes, it worked!

No more OutOfMemoryError!

The sample pom.xml that I used is:

Have fun!
Have some Fun!