Showing posts with label OutOfMemoryError. Show all posts
Showing posts with label OutOfMemoryError. Show all posts

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!

Saturday, July 17, 2010

How to fix the OutOfMemoryError when writing tests with PowerMock - Part - 2

In my previous post we had a look at How to fix the OutOfMemoryError when writing tests with PowerMock

Looks like, the method I described in that post does not work on all machine or environments.

In one of the environment whose configuration is:
  • Windows 7 64 Bit
  • 4 GB RAM DDR3
  • Intel Core i7 processor
  • JDK 6 64 Bit  
  • PowerMock 1.3.7  
it seemed to work for some time, but as soon as we wrote a few more tests using PowerMock (Mockito api), the OutOfMemoryError re-appeared.

Now that's annoying, what the hell is wrong now!  Well as I said in the previous post memory leaks are hard to find and fix!

But we need a solution now!  Can't wait till PowerMock 1.4 version is released.  Hence, I was on a mission to fix the issue and get all my tests running by hook or crook!

In the process I found that, running the tests on a 32 bit machine had no problems what so ever!  Surprise, Surprise!

Investigated a little further, found that when tests are run on a machine with 64 bit OS but 32 bit JDK they all pass.  That was the moment of Truth!

These are the steps I followed to fix the OutOfMemoryError:
  • Installed the JDK 6 32 bit on my machine
  • Updated the JAVA_HOME environment variable
  • Added the JDK 6 32 bit bin directory to the PATH environment variable
  • Removed the earlier JDK 6 64 bit bin directory from the PATH environment variable.  
All set!  Ran all the test and bingo!  All pass its a SUCCESS!

Saturday, July 3, 2010

How to fix the OutOfMemoryError when writing tests with PowerMock

We use PowerMock (With the Mockito Api) in our current project as the Mocking/Stubbing framework. It has worked our really well so far!

Completely in love with the Mockito syntax and especially the error messages that it gives out when something is wrong.
Love the power that PowerMock adds to Mockito which enables us to test the un-testable code. The combination i.e. PowerMocks with Mockito, makes it a deadly combination to ignore and I call it Mocks on Steroids!

But life is not always perfect! is it? Recently I faced a situation, suddenly out of no where I started getting the

infamous java.lang.OutOfMemoryError: PermGen space  Error here is the stack trace.

Man! its always a challenge to fix such issues.
Googled a little, found that its a known issue have a look at this url

Its caused because of a Memory leak (wow! as if I am telling something new! Its obvious, we get OutOfMemoryError when we have memory leaks! Everyone knows that!). Yes, and as you guys already know finding the source of memory leak is a challenge in itself.

But looks like, they have narrowed the problem down to the MockClassLoader class.

The problem:

If your project uses third party frameworks like log4j or spring, then those classes are also loaded by the MockClassLoader class. Causing log4j or spring classes to be loaded multiple times by multiple instances of MockClassLoader class.

The long term solution:

This problem will be fixed in the PowerMock's 1.4 version for sure!

But what should you do if you can't wait till then?

The short term solution:

Use the @PowerMockIgnore annotation. This annotation signal PowerMock not to load third party framework jars such as log4j and spring using the MockClassLoader. Let the system class loader load them.

The sample test case looks like this

That fixes the problem for our project!

This problem is a perfect example of the fact that, there is always a flip side!
But, if the flip side is not as bad, then as said by Amir Khan in the movie 3 idiots

All izz well!
Have some Fun!