How to Increase Java Heap Size for JVM in Apache Tomcat

Apache Tomcat is a popular web application server that is widely used for deploying Java-based web applications. By default, Tomcat is configured with a fixed heap size for the Java Virtual Machine (JVM), which can cause issues if the application requires more memory than what is available. In such cases, it is necessary to increase the Java heap size in Tomcat. A lack of memory can cause serious issues such as slow response times, out of memory errors, and even crashes. In this article, we will explain how to increase the heap memory of Apache Tomcat, step by step in linux.

Increase Java Heap Size

Step 1: Check the current heap size

Before increasing the heap size, it is important to check the current heap size. The current heap size can be checked using the following command:

$ ps -ef | grep tomcat
root      1167     1  0 14:12 ?        00:00:00 /usr/libexec/tomcat/server start
root      1170  1167  0 14:12 ?        00:00:00 /usr/libexec/tomcat/server run
root      1184  1170  3 14:12 ?        00:01:28 /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.302.b08-0.el8_4.x86_64/jre/bin/java -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Xms128m -Xmx512m -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -classpath /usr/share/tomcat/bin/bootstrap.jar:/usr/share/tomcat/bin/tomcat-juli.jar:/usr/share/java/commons-daemon.jar -Dcatalina.base=/usr/share/tomcat -Dcatalina.home=/usr/share/tomcat -Djava.io.tmpdir=/usr/share/tomcat/temp org.apache.catalina.startup.Bootstrap start

The line that includes the Java Virtual Machine (JVM) parameters will show you the current heap size. In the example output above, you can see that the “-Xms” parameter is set to 128 MB, and the “-Xmx” parameter is set to 512 MB. This indicates that the current heap size is between 128 MB and 512 MB, depending on the JVM’s memory requirements.

Step 2: Edit the Catalina.sh file

The next step is to edit the Catalina.sh file. This file is located in the bin directory of the Tomcat installation directory. For example, when you install Apache Tomcat 8 by extracting the tar file, the catalina.sh file should be located in the bin directory of the Tomcat installation directory (/opt/tomcat in this case). So, the full path to the Catalina.sh file should be /opt/tomcat/bin/catalina.sh. This script file is used to start, stop, and manage the Tomcat server. Open the file in a text editor and add the following lines at the end of the file:

JAVA_OPTS="-Xms512m -Xmx1024m"

The -Xms parameter sets the initial heap size, and the -Xmx parameter sets the maximum heap size. In the example above, the initial heap size is set to 512 MB, and the maximum heap size is set to 1024 MB. You can adjust these values based on your application’s requirements.

After make a changes, save the file and close the text editor.

Step 3: Restart Tomcat

Restart Tomcat to apply the changes by running the following command:

$ systemctl restart tomcat

Step 4: Verify the new heap size

Verify that the new heap size has been set by checking the process information again using the following command:

$ ps -ef | grep tomcat

Conclusion

Increasing the Apache Tomcat Java heap size for the JVM is a simple process. By following the steps outlined above, system administrators and Java developers can increase the heap size to meet the memory requirements of their applications. It is important to note that setting the heap size too high can lead to performance issues, so it is recommended to adjust the values based on the application’s needs.

Leave a Comment