How to increase Heap memory of Apache Tomcat Server

Increasing Heap size of Tomcat
You can increase heap size of Tomcat by setting JAVA_OPTS or CATALINA_OPTS at the top of the, file if you are running Tomcat in Linux and by putting JAVA_OPTS or CATALINA_OPTS into the catalina.bat file if you are running Tomcat in Windows. This approach will work irrespective of tomcat version i.e. you can increase the heap memory of tomcat 5.5, tomcat 6, tomcat 7 and even tomcat 8 by using this technique. You can find the and catalina.bat file in the bin directory of tomcat installation folder e.g. in my machine and the catalina.bat file is located under C:\Program Files\Apache Software Foundation\Apache Tomcat 7.0.41\bin directory. If you open the catalina.bat property you will see the advice given by Tomcat creator itself on how to give additional JVM options to Tomcat process.

When you increase heap memory by setting JAVA_OPTS option as shown below. Don't worry that it will not override any previous JVM options, all the places a new value is set they ensure that it is appending to the existing values.

Btw, I prefer setting it in the instead of Setting it in would apply to the process started by shutdown command as well, which may not be necessary.

Steps to increase heap size in Tomcat on Windows:

Go to catalina .bat and set this

set CATALINA_OPTS=-Xms1024m -Xmx2048m;


set JAVA_OPTS=-Xms2048m -Xmx2048m;

The above command will increase the heap size of tomcat to 2GB for both startup and maximum memory.

Steps to increase heap size in Tomcat on  Linux:

export CATALINA_OPTS=-Xms1023m -Xmx2048m;

-Xms – is the minimum or initial size of your heap
-Xmx – is the maximum size


export JAVA_OPTS=”-Xms1024m -Xmx1024m”

On Linux, you can also set in the file which is also available in the same bin folder.

How to increase Heap memory of Apache Tomcat

Note that CATALINA_OPTS is a better place than JAVA_OPTS for putting JVM heap memory settings. The former is only used when actually starting the Tomcat instance. JAVA_OPTS is also used to start JVMs that only interact with Tomcat (for example the JVM instance that is used to send the shutdown message to a running Tomcat instance).

If you open the catalina.bat or file, you will see this approach is recommended by Tomcat itself:
rem   CATALINA_OPTS   (Optional) Java runtime options used when the "start",
rem                   "run" or "debug" command is executed.
rem                   Include here and not in JAVA_OPTS all options, that should
rem                   only be used by Tomcat itself, not by the stop process,
rem                   the version command etc.
rem                   Examples are heap size, GC logging, JMX ports etc.
rem   JAVA_OPTS       (Optional) Java runtime options used when any command
rem                   is executed.
rem                   Include here and not in CATALINA_OPTS all options, that
rem                   should be used by Tomcat and also by the stop process,
rem                   the version command etc.
rem                   Most options should go into CATALINA_OPTS.

One more advantage of this approach is that it doesn't matter whether you are running Tomcat in Eclipse or NetBeans, this approach will work in all condition because even IDEs will call these scripts to start the Tomcat in the respective operating system.

Btw, If your Tomcat is constantly going down with java.lang.OutOfMemoryError: Java Heap space then it might be the time to move to 64-bit JVM and increasing heap size of Tomcat. Since Tomcat is also a Java process, you can increase Tomcat's heap size just like any other Java application e.g. by using -Xmx option.

The 64-bit JVM allows you to set even larger heap memory e.g. 8 to 10GB to cater the need of bigger Java web application which holds millions of user records. On the other hand, 32-bit JVM theoretically allows 4GB of the heap but practically it even lesser than that. See here for more details on maximum heap memory available on 32-bit and 64-bit Java virtual machines.

Other related Java tutorials you may like:
How to increase heap size of Java program running in Eclipse? (tutorial)
How to increase heap memory of Maven and ANT? (guide)
What is the difference between Stack and Heap memory in JVM? (answer)
How to increase heap memory of Eclipse IDE? (steps)
How to fix java.lang.OutOfMemoryError: PermGen Space in Tomcat? (guide)
How to get the max, free and total memory in Java? (article)
What is -XX:UseCompressedOops in 64-bit JVM? (article)

If you face any problem while increasing tomcat heap memory then you can post it on comments and we'll try to solve it together. 

No comments :

Post a Comment