Sunday, November 15, 2009

Set Tomcat Memory Heap Size

Want to integrate charts into your webapp? Check out XChart.

If your Java Web Application running on Tomcat ever crashes and throws the exception:

SEVERE: Servlet.service() for servlet WebAppServlet threw exception
java.lang.OutOfMemoryError: Java heap space

, you need to increase the allocated memory that Tomcat has for running applications. This is accomplished by adding "-Xms256m -Xmx512m" to the catalina.sh file found in the "bin" directory of the Tomcat installation, where 256 and 512 should be replaced with appropriate values for your application. Xms mean starting heap size and Xmx means maximum heap size. I was able to increase the memory allocated to Tomcat on a Mac running Mac OS X Leopard with Tomcat 6.0.20 installed. These instructions should work for any *nix machine, but I can't guarantee it.

Step 1: Open up the catalina.sh file with any text editor.

Step 2: Search for "JAVA_OPTS=" in the file and in particular locate the following lines of code:


if [ -z "$LOGGING_MANAGER" ]; then
JAVA_OPTS="$JAVA_OPTS -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager"
else
JAVA_OPTS="$JAVA_OPTS $LOGGING_MANAGER"
fi


Step 3: At the end of the JAVA_OPTS definitions add "-Xms256m -Xmx512m".


if [ -z "$LOGGING_MANAGER" ]; then
JAVA_OPTS="$JAVA_OPTS -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Xms256m -Xmx512m"
else
JAVA_OPTS="$JAVA_OPTS $LOGGING_MANAGER -Xms256m -Xmx512m"
fi


Step 4: Save the file and restart Tomcat.


Piece of Cake!!

10 comments:

robertdouglass said...

Thanks for the tip.

What's really surprising is that in 2009, after so much time Tomcat and Java are still inane to configure. You shouldn't have to hack a bash script of this complexity to do the one task that every single freaking Tomcat user ends up needing to do to run any real sized web app. Someone in Java land is severely out of touch with people's needs and what it means to be user or developer friendly.

Anonymous said...

How to find out what is the original heap size before I make any change ?

Tim Molter said...

@Anonymous I'm not sure. I guess it could be found in the Tomcat documentation somewhere though.

Michael said...

I just wanted to take a second to thank you for posting this; my application runs quite a bit faster after that tweak. It's surprisingly difficult to find the answer to what should be a very simple question!

Divya sharma said...

thanks a lot.After 2 hours googling i found your post best to configure heap size in binary tomcat.It helps me

Unknown said...

Thanks for the tip, I searched around for a while trying to increase the max memory heap size. Do you know what its set to by default?

Tim Molter said...

Unknown, no unfortunately I don't know what the default is.

msolanki said...

Thank you for the absolutely clear post on setting heap size in Tomcat.

Ghayoor Outsourcingbe said...

this is very nice post , thanks alot
mac memory upgrade

S.E. Todd said...

I like to keep my custom configurations outside of the internal scripts. I set my JAVA_OPTS environment variables in /etc/init.d/tomcat so that if I ever upgrade tomcat my changes in catalina.sh don't get clobbered.