How to set HeapMemory or JVM Arguments in Tomcat

The Overview

In this post, we are going to explore how and where to configure the heap memory in tomcat servlet container ( application server).  How and where to set the XMX and XMS values for your Tomcat JVM and where to configure the different GC policies like Parallel GC, Concurrent GC etc.

We discuss two different methods of achieving the same requirement and you can choose to use whichever is feasible for you and your infrastructure

 

Step1: Know your CATALINA_HOME and Tomcat Version Installed

First, we must know what is our CATALINA_HOME in order to place the configuration file.
You can find the value of  CATALINA_HOME if you execute version.sh available under the installation_path/bin.
In general,  installation_path will be your CATALINA_HOME
[tomcat@localhost bin]$ ./version.sh
Using CATALINA_BASE:   /opt/tomcat/apache-tomcat-7.0.69
Using CATALINA_HOME:   /opt/tomcat/apache-tomcat-7.0.69
Using CATALINA_TMPDIR: /opt/tomcat/apache-tomcat-7.0.69/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /opt/tomcat/apache-tomcat-7.0.69/bin/bootstrap.jar:/opt/tomcat/apache-tomcat-7.0.69/bin/tomcat-juli.jar
Server version: Apache Tomcat/7.0.69
Server built:   Apr 11 2016 07:57:09 UTC
Server number:  7.0.69.0
OS Name:        Linux
OS Version:     2.6.32-504.el6.x86_64
Architecture:   amd64
JVM Version:    1.7.0_79-b15
JVM Vendor:     Oracle Corporation
[tomcat@localhost bin]$ pwd
/opt/tomcat/apache-tomcat-7.0.69/bin

Step2a: Using Setenv.sh file to set JVM and Heap arguments to Tomcat instance

This is the first method and widely used and recommended. In this method, you will place your environment specific Configuration like Heap, GC policy,  JMX,  Classpath  in setenv.sh file
If you have just installed the tomcat, you may not be having setenv.shfile available under the CATALINA_HOME/bin location. So you must create it.
Here we go.
I am creating a new file called setenv.shunder bin location and defining my JVM arguments.
[tomcat@localhost bin]$ cat > setenv.sh
JAVA_OPTS="-Xms1024m -Xmx1024m -Xss228k -XX:+UseParallelGC"

[tomcat@localhost bin]$ cat setenv.sh
JAVA_OPTS="-Xms1024m -Xmx1024m -Xss228k -XX:+UseParallelGC"

[tomcat@localhost bin]$ pwd
/opt/tomcat/apache-tomcat-7.0.69/bin

You can also do the same in another way which is given below.

Note*: Choose either 2a or 2b.  You should not use both at the same time. So pick one

Step 2b: Another way to update JVM arguments in Tomcat - Catalina.sh

you can add these parameters in the very beginning of catalina.sh file but this is not a recommended approach.
#!/bin/sh
......
......
#   LOGGING_MANAGER (Optional) Override Tomcat's logging manager
#                   Example (all one line)
#                   LOGGING_MANAGER="-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager"
# – ---------------------------------------------------------------------------
JAVA_OPTS="-Xms1024m -Xmx1024m -Xss228k -XX:+UseParallelGC"

Verification:

Start the tomcat instance using the following command
./startup.sh
you can verify the JVM arguments in two ways.
1)  In Catalina.outlog you would be able to something like this.
INFO: Command line argument: -Xms1024m
May 13, 2016 10:34:46 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Xmx1024m
May 13, 2016 10:34:47 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Xss228k
May 13, 2016 10:34:47 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -XX:+UseParallelGC
2)  using ps command  you can find the JVM arguments
[tomcat@localhost ~]$ ps -eaf|grep -i java
tomcat    5944  3481  3 10:34 pts/0    00:00:05 /usr/bin/java -Djava.util.logging.config.file=/opt/tomcat/apache-tomcat-7.0.69/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Xms1024m -Xmx1024m -Xss228k -XX:+UseParallelGC -Djdk.tls.ephemeralDHKeySize=2048 -Djava.endorsed.dirs=/opt/tomcat/apache-tomcat-7.0.69/endorsed -classpath /opt/tomcat/apache-tomcat-7.0.69/bin/bootstrap.jar:/opt/tomcat/apache-tomcat-7.0.69/bin/tomcat-juli.jar -Dcatalina.base=/opt/tomcat/apache-tomcat-7.0.69 -Dcatalina.home=/opt/tomcat/apache-tomcat-7.0.69 -Djava.io.tmpdir=/opt/tomcat/apache-tomcat-7.0.69/temp org.apache.catalina.startup.Bootstrap start
tomcat    6008  5988  0 10:37 pts/1    00:00:00 grep -i java
[tomcat@localhost ~]$

 

Hope it helps.

Thanks
Sarav AK

Follow me on Linkedin My Profile
Follow DevopsJunction onFacebook orTwitter
For more practical videos and tutorials. Subscribe to our channel

Buy Me a Coffee at ko-fi.com

Signup for Exclusive "Subscriber-only" Content

Loading