Wednesday, August 26, 2009

java.lang.OutOfMemoryError: Java heap space Solutions

Two JVM options are often used to tune JVM heap size: -Xmx for maximum heap size, and -Xms for initial heap size. Here are some common mistakes I have seen when using them:


Missing m, M, g or G at the end (they are case insensitive). For example,

java -Xmx128 BigAppjava.lang.OutOfMemoryError: Java heap space
The correct command should be: java -Xmx128m BigApp. To be precise, -Xmx128 is a valid setting for very small apps, like HelloWorld. But in real life, I guess you really mean -Xmx128m


Extra space in JVM options, or incorrectly use =. For example,

java -Xmx 128m BigAppInvalid maximum heap size: -XmxCould not create the Java virtual machine.java -Xmx=512m HelloWorldInvalid maximum heap size: -Xmx=512mCould not create the Java virtual machine.

The correct command should be java -Xmx128m BigApp, with no whitespace nor =. -X options are different than -Dkey=value system properties, where = is used.


Only setting -Xms JVM option and its value is greater than the default maximum heap size, which is 64m. The default minimum heap size seems to be 0. For example,

java -Xms128m BigAppError occurred during initialization of VMIncompatible initial and maximum heap sizes specified
The correct command should be java -Xms128m -Xmx128m BigApp. It's a good idea to set the minimum and maximum heap size to the same value. In any case, don't let the minimum heap size exceed the maximum heap size.


Heap size is larger than your computer's physical memory. For example,

java -Xmx2g BigAppError occurred during initialization of VMCould not reserve enough space for object heapCould not create the Java virtual machine.
The fix is to make it lower than the physical memory: java -Xmx1g BigApp


Incorrectly use mb as the unit, where m or M should be used instead.

java -Xms256mb -Xmx256mb BigAppInvalid initial heap size: -Xms256mbCould not create the Java virtual machine.
The heap size is larger than JVM thinks you would ever need. For example,

java -Xmx256g BigAppInvalid maximum heap size: -Xmx256gThe specified size exceeds the maximum representable size.Could not create the Java virtual machine.
The fix is to lower it to a reasonable value: java -Xmx256m BigApp

The value is not expressed in whole number. For example,

java -Xmx0.9g BigAppInvalid maximum heap size: -Xmx0.9gCould not create the Java virtual machine.
The correct command should be java -Xmx928m BigApp

********************************************************************

How to set java heap size in Tomcat?
Stop Tomcat server, set environment variable CATALINA_OPTS, and then restart Tomcat. Look at the file tomcat-install/bin/catalina.sh or catalina.bat for how this variable is used. For example,

set CATALINA_OPTS="-Xms512m -Xmx512m" (Windows)export CATALINA_OPTS="-Xms512m -Xmx512m" (ksh/bash)setenv CATALINA_OPTS "-Xms512m -Xmx512m" (tcsh/csh)
In catalina.bat or catallina.sh, you may have noticed CATALINA_OPTS, JAVA_OPTS, or both can be used to specify Tomcat JVM options. What is the difference between CATALINA_OPTS and JAVA_OPTS? The name CATALINA_OPTS is specific for Tomcat servlet container, whereas JAVA_OPTS may be used by other java applications (e.g., JBoss). Since environment variables are shared by all applications, we don't want Tomcat to inadvertently pick up the JVM options intended for other apps. I prefer to use CATALINA_OPTS.

How to set java heap size in JBoss?
Stop JBoss server, edit $JBOSS_HOME/bin/run.conf, and then restart JBoss server. You can change the line with JAVA_OPTS to something like:

JAVA_OPTS="-server -Xms128m -Xmx128m"

How to set java heap size in Eclipse?
You have 2 options:
1. Edit eclipse-home/eclipse.ini to be something like the following and restart Eclipse.

-vmargs-Xms64m-Xmx256m

2. Or, you can just run eclipse command with additional options at the very end. Anything after -vmargs will be treated as JVM options and passed directly to the JVM. JVM options specified in the command line this way will always override those in eclipse.ini. For example,

eclipse -vmargs -Xms64m -Xmx256m

How to set java heap size in NetBeans?
Exit NetBeans, edit the file netbeans-install/etc/netbeans.conf. For example,

netbeans_default_options="-J-Xms512m -J-Xmx512m -J-XX:PermSize=32m -J-XX:MaxPermSize=128m -J-Xverify:none

How to set java heap size in Apache Ant?
Set environment variable ANT_OPTS. Look at the file $ANT_HOME/bin/ant or %ANT_HOME%\bin\ant.bat, for how this variable is used by Ant runtime.

set ANT_OPTS="-Xms512m -Xmx512m" (Windows)export ANT_OPTS="-Xms512m -Xmx512m" (ksh/bash)setenv ANT_OPTS "-Xms512m -Xmx512m" (tcsh/csh)


How to increase heap size :
step 1.
open D:\jakarta-tomcat-5.0.28\bin\catalina.bat
step 2.
add -Xmx512M before %JAVA_OPTS%

Where you find %JAVA_OPTS% .. before this just put -Xmx512M

like this
%_EXECJAVA% -Xmx512M %JAVA_OPTS%

or

these are the lines i have changed in my machine
this is for 512 MB Setting.


All the line are there already ..just replace
-------------------------------------------------
rem Execute Java with the applicable properties

if not "%JPDA%" == "" goto doJpda
if not "%SECURITY_POLICY_FILE%" == "" goto doSecurity
%_EXECJAVA% -Xmx512M %JAVA_OPTS% %CATALINA_OPTS% %DEBUG_OPTS% -Djava.endorsed.dirs="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%
goto end
:doSecurity
%_EXECJAVA% -Xmx512M %JAVA_OPTS% %CATALINA_OPTS% %DEBUG_OPTS% -Djava.endorsed.dirs="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Djava.security.manager -Djava.security.policy=="%SECURITY_POLICY_FILE%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%
goto end
:doJpda
if not "%SECURITY_POLICY_FILE%" == "" goto doSecurityJpda
%_EXECJAVA% -Xmx512M %JAVA_OPTS% %CATALINA_OPTS% -Xdebug -Xrunjdwp:transport=%JPDA_TRANSPORT%,address=%JPDA_ADDRESS%,server=y,suspend=n %DEBUG_OPTS% -Djava.endorsed.dirs="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%
goto end
:doSecurityJpda
%_EXECJAVA% -Xmx512M %JAVA_OPTS% %CATALINA_OPTS% -Xrunjdwp:transport=%JPDA_TRANSPORT%,address=%JPDA_ADDRESS%,server=y,suspend=n %DEBUG_OPTS% -Djava.endorsed.dirs="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Djava.security.manager -Djava.security.policy=="%SECURITY_POLICY_FILE%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%
goto end

:end

No comments:

Post a Comment

Happy Reading!

Ganex Improves .. Headline Animator