Wednesday, December 19, 2012

How To Resolve java.lang.OutOfMemoryError: Java heap space (The system is out of resources)

This  Java Heap Error is observed in different situations such as while running a Java Application, while executing Ant Scripts to run java projects, dealing with Java based web servers such as tomcat or while working with Java programs in Eclipse, Netbeans or intellij etc. The typical error on the console looks as shown below

[javac] The system is out of resources.
[javac] Consult the following stack trace for details.
[javac] java.lang.OutOfMemoryError: Java heap space
[javac] at com.sun.tools.javac.jvm.ClassReader.readMethod(ClassReader.java:1486)
[javac] at com.sun.tools.javac.jvm.ClassReader.readClass(ClassReader.java:1586)

....

The first being if you have a much bigger Java Application and if there are any memory leaks in the application code (memory is allocated for objects but never released once the objects are no more required by the application), then JVM throws java.lang.OutOfMemoryError: Java heap space. In this situation, the java application must be analysed to identify the memory leaks (may be using memory profiling) and fix the memory leaks in the code. This is going to be performance improvement task.

The error is thrown when JVM is asked to use a limitted memory space for java objects but in fact more memory is required than the default or specified heap space. Note that in this case there are no memory leaks. This can be resolved by allocating more heap memory while executing the java program by using VM command line parameters -Xms (initial memory size) and -Xmx the maximum memory.

java -Xms128m -Xmx512m

The java heap error may also be thrown while executing certain ant targets. Ant uses the javac command to compile and execute java programs and user can tell the ant tool how much heap memory has to be used. To achieve this, ant gives a environmnet variable called ANT_OPTS. The command line arguments that are required to be passed to the JVM such as setting the maximum Java heap space can be specified as a value to the environment variable ANT_OPTS.

Variable Name : ANT_OPTS
Variable Value: -Xms128m -Xmx512m -XX:MaxPermSize=256m

The value of ANT_OPTS is used by the Ant tool while dealing with your java programs. If you look at above example, multiple arguments can be specified, here MaxPermSize is also specified (just to demonstrate multiple arguments can be speicified using ':') If you are interested to know about PermGenSpace have a look at Prevent OutOfPermGenSpace Java Error.

How to set the environment Variables under Windows?

Its advisable to define the environment variables under My Computer->Right Click->Properties->Advanced System Settings->Enviroment Variables... and then do not forget to restart the command line prompt for new environment variable to take effect.
The environment variables can also be set in the command prompt such DOS prompt by using SET command. Example: Set ANT_OPTS=-Xms128m -Xmx512m -XX. However the environment variable set like above will go away once you close the command prompt so its temporary.

1 comment:

  1. When facing OutOfMemoryErrrors and suspecting memory leak - give http://www.plumbr.eu a try to verify your suspicion. Makes detection and fixing of the leak a lot easier than the profilers you refer ...

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...