How to set the JVM options for Tomcat on Linux
Environment
- Red Hat Enterprise Linux (RHEL)
- Red Hat JBoss Web Server (JWS)
- Red Hat Enterprise Web Server (EWS)
- Apache Tomcat
Issue
- How do I set the Java VM options for Tomcat process with JBoss EWS on Linux?
- How do I set the Java VM options for Tomcat included in RHEL?
- How do I change the size of the Java VM's heap in Tomcat?
- Where can I update the jvm heap and other java runtime options in JBoss Web Server 3 for tomcat7 and tomcat8?
Resolution
The file to be created/updated depends on the package/product being used:
- JBoss Web Server (JWS) 5.x and above ZIP Install
- JBoss Web Server (JWS) 5.x and above RPM Install
- RHEL 7 and above
- JBoss Web Server (JWS) 3.x RPM installs (Unsupported; EOL)
- EWS RPM installs (Unsupported; EOL)
- RHEL 6 or EWS RPM installs (Unsupported; EOL)
- RHEL 5 (Unsupported; EOL)
- JVM Arguments Example
JBoss Web Server (JWS) 5.x and above ZIP Install
Create/edit the $CATALINA_HOME/bin/setenv.sh file.
The $CATALINA_HOME is the path where the ZIP was extracted.
JBoss Web Server (JWS) 5.x and above RPM Install
Edit the /opt/rh/jws5/root/usr/share/tomcat/conf/tomcat.conf file.
RHEL 7 and above
Edit /etc/tomcat/tomcat.conf file.
NOTE: the file is loaded as systemd environment file and NOT sourced as a bash script. This means that variable expansion does NOT WORK as it does in previous versions where systemd is not used. To workaround this limitation of systemd, you must specify your $CATALINA_OPTS on a single line. Likewise with other variables.
JBoss Web Server (JWS) 3.x RPM installs (Unsupported; EOL)
Edit /etc/tomcat7/tomcat7.conf for Tomcat 7 or /etc/tomcat8/tomcat8.conf for Tomcat 8.
EWS RPM installs (Unsupported; EOL)
Edit the /etc/sysconfig/tomcat7 service script
RHEL 6 or EWS RPM installs (Unsupported; EOL)
Edit the /etc/sysconfig/tomcat6 service script or the /etc/tomcat6/tomcat6.conf file
RHEL 5 (Unsupported; EOL)
Create/edit /usr/share/tomcat5/bin/setenv.sh
Here is an example of how to add JVM arguments
# Reset CATALINA_OPTS
CATALINA_OPTS=""
# Set Java Heap Size
CATALINA_OPTS="$CATALINA_OPTS -Xms2048m -Xmx2048m"
# Set Permanent Size
CATALINA_OPTS="$CATALINA_OPTS -XX:PermSize=256m -XX:MaxPermSize=256m"
# Enable Garbage Collection log
# -XX:+PrintGCTimeStamps outputs timestamp from start-up
# -XX:+PrintGCDateStamps outputs human readable timestamp
# You can add both -XX:+PrintGCTimeStamps and -XX:+PrintGCDateStamps at once
#CATALINA_OPTS="$CATALINA_OPTS -verbose:gc -Xloggc:/path/to/gc.log.`date +%Y%m%d%H%M%S` -XX:+PrintGCDetails -XX:+PrintGCTimeStamps"
#CATALINA_OPTS="$CATALINA_OPTS -verbose:gc -Xloggc:/path/to/gc.log.`date +%Y%m%d%H%M%S` -XX:+PrintGCDetails -XX:+PrintGCDateStamps"
CATALINA_OPTS="$CATALINA_OPTS -verbose:gc -Xloggc:/path/to/gc.log.`date +%Y%m%d%H%M%S` -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps"
# %p (pid) and %t (timestamp) can be used when JDK 8u20 and 7u80 or later are used
# CATALINA_OPTS="$CATALINA_OPTS -verbose:gc -Xloggc:/path/to/gc_%p_%t.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps"
# Enable PrintHeapAtGC
#CATALINA_OPTS="$CATALINA_OPTS -XX:+PrintHeapAtGC"
# Enable HeapDumpOnOutOfMemoryError
CATALINA_OPTS="$CATALINA_OPTS -XX:+HeapDumpOnOutOfMemoryError"
# Specify an output directory for heap dump
#CATALINA_OPTS="$CATALINA_OPTS -XX:HeapDumpPath=/path/to/outputdir/"
# Enable JMX Remote
#CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote"
#CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote=true"
#CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote.ssl=false"
#CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote.authenticate=false"
#CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote.port=12345"
NOTE: Please do NOT use JAVA_OPTS instead of CATALINA_OPTS because JAVA_OPTS is not only used for the tomcat java process. It is also used for another java process such as the catalina.sh stop command used for stopping the tomcat java process. When you use JAVA_OPTS, you will see issues like:
- The shutdown java process using large heap/perm sizes (and potentially crashing in the shutdown JVM start up because it can't allocate the large heap for this jvm as well)
- The shutdown java process overwritting existing tomcat garbage collector (GC) logs
- Failing to stop due to port conflict on JMX Remote port
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.