Sunday, October 31, 2010

Configuring Java VM options to get rid of memory issues

When using applications such as Intellij IDEA or maven with projects those have a large code base such as WSO2 carbon, I have encountered these programs complaining about memory issues such as:

1. Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
2. java.lang.OutOfMemoryError: PermGen space

The first error occurs when the allocated java heap size for that particular application is not enough, regardless of how much memory the machine has. In order to get rid of this,  you can resize the initial and maximum java heap sizes allocated for that particular application.
In Ubuntu, those values can be configured in mvn.sh file (usually found in /usr/bin) for maven and idea.vmoptions file (found in #idea installation directory/bin) for IDEA.
Xms is the parameter that defines the initial java heap size and Xmx is the parameter that defines maximum java heap size. In other words, the application starts with a java heap size as defined in Xms and grow until it reaches the size defined in Xmx. Therefore it is recommended to give two values with latter larger than the former.
Eg: for IDEA I would set: Xms512m and Xmx1024m

You get the second error when the space that holds the permanent generation is filled up. This permanent generation holds meta-data about user classes. (Garbage collector maintains objects in several generations. Read more about this from here)
You can get rid of this error by assigning proper values to -XX:PermSize and -XX:MaxPermSize parameters according to the requirement of your application usage.
For an example I would set it in maven as:
-XX:PermSize=256m -XX:MaxPermSize=512m
In Ubuntu, you can set it by editing the mvn.sh file to include this line under Optional ENV vars.:
export MAVEN_OPTS="-Xmx1024m -XX:PermSize=256m -XX:MaxPermSize=512m"

2 comments:

  1. Thank You Hasini.
    It was a great help. I was struggling for a whole day to figure this out.

    ReplyDelete

Note: Only a member of this blog may post a comment.