Main Content

Increasing Java Virtual Machine memory for Tomcat

Archive - Originally posted on "The Horse's Mouth" - 2008-07-24 18:41:18 - Graham Ellis

Q. "How do I increase the memory that my Java Virtual Machine in Tomcat can use?" A question asked - not for the first time - by a delegate on todays's Deploying Apache httpd and Tomcat course.

A. Set the JAVA_OPTS environment variable. Longer answer:

If you're running on a Unix or Linux system, edit the setenv.sh file in Tomcat's bin directory to add in a line like:
export JAVA_OPTS="-Xms128m -Xmx512m"
or on a Windows system, add in to setenv.bat something like:
set JAVA_OPTS=-Xms128 -Xmx512
and restart the Tomcat server.

By default (out of the tar), Tomcat starts up with just 64 Mbytes allocated to the JVM, and that's also it's maximum. This is a very low figure, but it's a sensible default for a new install where the person setting the system up isn't into the tuning of the system.

The two extra parameters specified via JAVA_OPTS are as follows:
-Xms - the amount of memory that the JVM starts with.
-Xmx - the maximum memory that the JVM may have.
I have seen advise (but have no proof) that your should keep the -Xms figure rather lower than the -Xmx figure, since Tomcat uses more memory at startup than when it's running. The result (if you have them set to the same value) is a peak and unnecessary memory demand at startup which in an extreme case can cause a failure.

A couple of other tips:

a) You should consider using the -server option too to run your JVM in server rather than (default) client mode (actually, they're two different but related VMs). The server that's selected with -server is slower to load but is said to run faster.

b) We have customers who run multiple instances of Tomcat on the same box (but on different ports) - not a bad idea if you're sharing load between multiple processors. You can front them with httpd using mod-proxy-balance or mod-jk to distribute the requests, but at the same time to provide "sticky sessions" so that a series of requests made by an individual user will all be handled by the same instance.