- How to Change JVM Heap Setting (-Xms -Xmx) of Tomcat – Configure setenv.sh file – Run catalina.sh
- -Xmx
- -Xmn
- -XX:PermSize
- Let’s get started:
- Step-1
- Step-2
- Step-3
- Step-4
- Step-5
- Command for Windows Environment:
- Suggested Articles
- Multiline JAVA_OPTS in setenv.sh
- JAVA_OPTS set in catalina.sh not working for TOMCAT private instance
- 3 Answers 3
- Add JVM options in Tomcat
- 6 Answers 6
How to Change JVM Heap Setting (-Xms -Xmx) of Tomcat – Configure setenv.sh file – Run catalina.sh
Apache Tomcat is widely used Web Container in the world. Very big companies run on Apache Tomcat now a days. There are quite a few other alternatives like IBM WebSphere, Geronimo, IIS, etc. but Tomcat is my favorite one too.
It’s very critical for us to configure all correct parameters while running your application in Production environment or even in development env.
In this tutorial we will go over steps on how to configure -Xms, -Xmx and -XX:PermSize value for Tomcat server. Let’s first understand few terms.
-Xmx
Specifies the maximum size, in bytes , of the memory allocation pool. This value must a multiple of 1024 greater than 2MB. Append the letter k or K to indicate kilobytes, or m or M to indicate megabytes. The default value is 64MB. The upper limit for this value will be approximately 4000m on Solaris 7 and Solaris 8 SPARC platforms and 2000m on Solaris 2.6 and x86 platforms, minus overhead amounts. So, in simple words, you are saying Java to use Maximum of 1024 MB from available memory.
NOTE : there is NO SPACE between -Xmx and 1024m
-Xmn
It’s a size of the heap for the young generation.
-XX:PermSize
It’s used to set size for Permanent Generation. It is where class files are kept.
Let’s get started:
Below are the simple steps to change -Xmx / -Xms values or other JVM parameters if you are running Tomcat from command prompt .
Step-1
Step-2
Go to Apache Tomcat /bin directory.
Step-3
By default you wont see setenv.sh (for Linux/Mac) or setenv.bat (for windows) file under /bin directory. You have to create one with below parameters.
export CATALINA_OPTS="$CATALINA_OPTS -Xms512m" export CATALINA_OPTS="$CATALINA_OPTS -Xmx8192m" export CATALINA_OPTS="$CATALINA_OPTS -XX:MaxPermSize=256m"
Step-4
Step-5
Monitor logfile and you should see your Tomcat started with all your specified parameters in setenv.sh file.
Command for Windows Environment:
You need to create setenv.bat file with below content:
set "JAVA_OPTS=%JAVA_OPTS% -Xms512m -Xmx8192m -XX:MaxPermSize=256m -server"
and run Tomcat with catalina.bat
If you liked this article, then please share it on social media. Have a question or suggestion? Please leave a comment to start the discussion. 👋
Suggested Articles
Multiline JAVA_OPTS in setenv.sh
I’m trying to setup my setenv.sh on Ubuntu 12/Tomcat 7. Tomcat has been installed with apt-get I’ve tried to create a multiline JAVA_OPTS variable but keep running into error messages.
#!/bin/sh export JAVA_OPTS="$JAVA_OPTS -server \ -Xms512m -Xmx512m" : not foundtomcat7/bin/catalina.sh: 4: /usr/share/tomcat7/bin/setenv.sh: Using CATALINA_BASE: /usr/share/tomcat7 Using CATALINA_HOME: /usr/share/tomcat7 Using CATALINA_TMPDIR: /usr/share/tomcat7/temp Using JRE_HOME: /usr/lib/jvm/java-7-oracle/ Using CLASSPATH: /usr/share/tomcat7/bin/bootstrap.jar:/usr/share/tomcat7/bin/tomcat-juli.jar Error: Could not find or load main class
#!/bin/sh export JAVA_OPTS="$JAVA_OPTS -server -Xms512m -Xmx512m" : not foundtomcat7/bin/catalina.sh: 3: /usr/share/tomcat7/bin/setenv.sh: Using CATALINA_BASE: /usr/share/tomcat7 Using CATALINA_HOME: /usr/share/tomcat7 Using CATALINA_TMPDIR: /usr/share/tomcat7/temp Using JRE_HOME: /usr/lib/jvm/java-7-oracle/ Using CLASSPATH: /usr/share/tomcat7/bin/bootstrap.jar:/usr/share/tomcat7/bin/tomcat-juli.jar Invalid maximum heap size: -Xmx512m Error: Could not create the Java Virtual Machine. Error: A fatal exception has occurred. Program will exit.
#!/bin/sh JAVA_OPTS="$JAVA_OPTS -server -Xms704m -Xmx704m" JAVA_OPTS="$JAVA_OPTS -XX:OnOutOfMemoryError=/usr/share/scripts/on_server_crash.sh" JAVA_OPTS="$JAVA_OPTS -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/log/tomcat7" JAVA_OPTS="$JAVA_OPTS -XX:MaxPermSize=128m -XX:MaxNewSize=256m -XX:NewSize=256m" echo $JAVA_OPTS JAVA_OPTS="$JAVA_OPTS -XX:SurvivorRatio=12 -XX:MaxTenuringThreshold=0" JAVA_OPTS="$JAVA_OPTS -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalMode" JAVA_OPTS="$JAVA_OPTS -XX:+CMSIncrementalPacing -XX:+CMSClassUnloadingEnabled" JAVA_OPTS="$JAVA_OPTS -XX:+CMSPermGenSweepingEnabled -XX:+DisableExplicitGC" JAVA_OPTS="$JAVA_OPTS -XX:+UseParNewGC -XX:+UseTLAB" JAVA_OPTS="$JAVA_OPTS -Djava.awt.headless=true" JAVA_OPTS="$JAVA_OPTS -javaagent:$NR_JAR -Dnewrelic.environment=production" echo $JAVA_OPTS export JAVA_OPTS
When I try «each command on its own line» without the export option, I get «unrecognized option -server
#!/bin/sh JAVA_OPTS="$JAVA_OPTS -server" JAVA_OPTS="$JAVA_OPTS -Xms704m" JAVA_OPTS="$JAVA_OPTS -Xmx704m" JAVA_OPTS="$JAVA_OPTS -XX:OnOutOfMemoryError=/usr/share/scripts/on_server_crash.sh" JAVA_OPTS="$JAVA_OPTS -XX:+HeapDumpOnOutOfMemoryError" JAVA_OPTS="$JAVA_OPTS -XX:HeapDumpPath=/var/log/tomcat7" JAVA_OPTS="$JAVA_OPTS -XX:MaxPermSize=128m" JAVA_OPTS="$JAVA_OPTS -XX:MaxNewSize=256m" JAVA_OPTS="$JAVA_OPTS -XX:NewSize=256m" JAVA_OPTS="$JAVA_OPTS -XX:SurvivorRatio=12" JAVA_OPTS="$JAVA_OPTS -XX:MaxTenuringThreshold=0" JAVA_OPTS="$JAVA_OPTS -XX:+UseConcMarkSweepGC" JAVA_OPTS="$JAVA_OPTS -XX:+CMSIncrementalMode" JAVA_OPTS="$JAVA_OPTS -XX:+CMSIncrementalPacing" JAVA_OPTS="$JAVA_OPTS -XX:+CMSClassUnloadingEnabled" JAVA_OPTS="$JAVA_OPTS -XX:+CMSPermGenSweepingEnabled" JAVA_OPTS="$JAVA_OPTS -XX:+DisableExplicitGC" JAVA_OPTS="$JAVA_OPTS -XX:+UseParNewGC" JAVA_OPTS="$JAVA_OPTS -XX:+UseTLAB" JAVA_OPTS="$JAVA_OPTS -Djava.awt.headless=true" JAVA_OPTS="$JAVA_OPTS -javaagent:$NR_JAR" JAVA_OPTS="$JAVA_OPTS -Dnewrelic.environment=production" echo JAVA_OPTS
JAVA_OPTS set in catalina.sh not working for TOMCAT private instance
I have placed JVM options via JAVA_OPTS in catalina.sh in the catalina base. However, the system doesn’t pick those options—I am trying to pass profiling information to set paths for project properties and logging files. I have to set the options in setenv.sh in the private instance’s bin. Even the echo command that I put in catalina.sh to view the JAVA_OPTS doesn’t get printed-defaults like CATALINA_BASE,etc. do get printed. Is catalina.sh even being processed? At the end of the day, my system works fine with setenv.sh. I am curious as to why JAVA_OPTS are not being picked up from catalina.sh. I am using Ubuntu 12.04 with TOMCAT 7 installed and JDK 1.7. Thanks
3 Answers 3
You are not supposed to edit the catalina.sh file — it states so in that file. Instead, to set your environmental variables, create a setenv.sh file in the same directory where catalina.sh is (called CATALINA_BASE/bin) and write your code in it.
I had to set the JAVA_OPTS variable myself, and I created the bin/setenv.sh file, set it to executable chmod +x bin/setenv.sh and wrote in it:
JAVA_OPTS=»$JAVA_OPTS -Xms128m -Xmx512m -server»
which set my initial allocated memory to 128 and max memory to 512 MB. And it was working.
catalina.sh has a lot of conditionals — it happened to me more than once that I edited the wrong position, or one that was overwritten later in that file. setenv.sh works fine, and that’s exactly what it’s there for: Imagine you’re installing a tomcat update — this will overwrite your catalina.sh . However, tomcat never comes with setenv.sh , thus it won’t overwrite your changes.
Further, you might want to define CATALINA_OPTS instead of JAVA_OPTS : Those are the options that are used to start tomcat. If part of your configuration is JAVA_OPTS=»-Xmx16G -Xms16G» , you’d allocate 16G heap space when you try to shut down tomcat: The shutdown process spawns a JVM with the JAVA_OPTS parameters as well. Only the startup process spawns with the CATALINA_OPTS environment (in addition to JAVA_OPTS ), thus that’s most likely what you want to configure/tune, otherwise you risk not being able to stop tomcat due to ridiculous memory requirements of shutdown.sh.
Add JVM options in Tomcat
How can I add jvm(Java virtual machine) options in Apache Tomcat 6? Is there a administration console in tomcat? I tried http://localhost:8080/admin but I could not get anything out of it. I want to add the following jvm options:
-agentpath:C:\calltracer\jvmti\calltracer5.dll=traceFile-C:\calltracer\call.trace,filterFile-C:\calltracer\filters.txt,outputType-xml,usage-uncontrolled -Djava.library.path=C:\calltracer\jvmti -Dcalltracerlib=calltracer5
6 Answers 6
As Bhavik Shah says, you can do it in JAVA_OPTS, but the recommended way (as per catalina.sh) is to use CATALINA_OPTS:
# CATALINA_OPTS (Optional) Java runtime options used when the "start", # "run" or "debug" command is executed. # Include here and not in JAVA_OPTS all options, that should # only be used by Tomcat itself, not by the stop process, # the version command etc. # Examples are heap size, GC logging, JMX ports etc. # JAVA_OPTS (Optional) Java runtime options used when any command # is executed. # Include here and not in CATALINA_OPTS all options, that # should be used by Tomcat and also by the stop process, # the version command etc. # Most options should go into CATALINA_OPTS.
Set it in the JAVA_OPTS variable in [path to tomcat]/bin/catalina.sh. Under windows there is a console where you can set it up or you use the catalina.bat.
JAVA_OPTS=-agentpath:C:\calltracer\jvmti\calltracer5.dll=traceFile-C:\calltracer\call.trace,filterFile-C:\calltracer\filters.txt,outputType-xml,usage-uncontrolled -Djava.library.path=C:\calltracer\jvmti -Dcalltracerlib=calltracer5
At least in Tomcat 7, this is not the recommended place for customisations. From catalina.sh : «Do not set the variables in this script. Instead put them into a script setenv.sh in CATALINA_BASE/bin to keep your customizations separate». Also, as Mitch notes, CATALINA_OPTS is the more canonical variable to store this in.
To expand on setenv.sh, here is an example entry: export CATALINA_OPTS=»$CATALINA_OPTS -XX:MaxPermSize=256m» (this would increase max perm size).
After checking catalina.sh (for windows use the .bat versions of everything mentioned below)
# Do not set the variables in this script. Instead put them into a script # setenv.sh in CATALINA_BASE/bin to keep your customizations separate.
# CATALINA_OPTS (Optional) Java runtime options used when the "start", # "run" or "debug" command is executed. # Include here and not in JAVA_OPTS all options, that should # only be used by Tomcat itself, not by the stop process, # the version command etc. # Examples are heap size, GC logging, JMX ports etc
So create a setenv.sh under CATALINA_BASE/bin (same dir where the catalina.sh resides). Edit the file and set the arguments to CATALINA_OPTS
For e.g. the file would look like this if you wanted to change the heap size
Or in your case since you’re using windows setenv.bat would be
set CATALINA_OPTS=-agentpath:C:\calltracer\jvmti\calltracer5.dll=traceFile-C:\calltracer\call.trace,filterFile-C:\calltracer\filters.txt,outputType-xml,usage-uncontrolled -Djava.library.path=C:\calltracer\jvmti -Dcalltracerlib=calltracer5
To clear the added options later just delete setenv.bat/sh