CATALINA_OPTS vs JAVA_OPTS - What is the difference? - java

I was trying to find out the difference between Apache Tomcat variables - CATALINA_OPTS and JAVA_OPTS in SO and surprised to see that there is no question/answer posted here yet. So I thought of sharing it here (with answer) after finding out the difference. Check the answer/difference below.
NOTE: At the time of this posting, we're running Apache Tomcat v6.0.10 with JDK 6u32 on CentOS5 64-bit arch.

There are two environment variables - CATALINA_OPTS and JAVA_OPTS - which are both used in the catalina.sh startup and shutdown script for Tomcat.
CATALINA_OPTS: Comment inside catalina.sh:
# 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: Comment inside catalina.sh:
# 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.
So why are there two different variables? And what's the difference?
Firstly, anything specified in EITHER variable is passed, identically, to the command that starts up Tomcat - the start or run command - but only values set in JAVA_OPTS are passed to the stop command. That probably doesn't make any difference to how Tomcat runs in practice as it only effects the END of a run, not the start.
The second difference is more subtle. Other applications may also use JAVA_OPTS, but only Tomcat will use CATALINA_OPTS. So if you're setting environment variables for use only by Tomcat, you'll be best advised to use CATALINA_OPTS, whereas if you're setting environment variables to be used by other java applications as well, such as by JBoss, you should put your settings in JAVA_OPTS.
Source: CATALINA_OPTS v JAVA_OPTS - What is the difference?

I'd like to add that JAVA_OPTS and CATALINA_OPTS are mutually complemental: If you define both environment variables, the content of both will be concatenated and passed to the start and run command - as explained by Gnanam above.
You can also refer to the original source of catalina.sh

Durung shutdown, tomcat launches mutiple vm as explain in comment by #joao.
If you are recording some data during tomcat shutdowm, use CATALINA_OPTS not JAVA_OPTS. A great example is when I want save the data during jacoco.exec, I should be using CATALINA_OPTS not JAVA_OPTS.

Related

Want to allocate more memory to Tomcat but catalina.sh is missing

I am running a Tomcat8 server on linux (RedHat). The machine it runs on has a total of 15GB Ram of which i want to allocate 11GB to the tomcat server. Currently it only has 3.3GB avaialable for use.
I have openjdk 64 bit installed.
I am trying to set CATALINA_OPTS to -Xms512M -Xmx11g and the JAVA_OPTS to -d64 -Xms256m -Xmx12g.
I found several how-to's saying something about a setenv.sh or catalina.sh both of which I cannot find: neither under tomcat8/bin, catalina_home or catalina_base which both point to /root.
So how do I increase available memory for my Tomcat server?
Thanks!
It seems that catalina.sh is either no longer existent in the latest tomcat release or it has something to do with the fact that i am using a amazon vm and they have some kind of modified tomcat8 distribution what i would doubt.
Solution:
under usr/share/tomcat8/conf/ there is a tomcat8.conf file.
Adding the row JAVA_OPTS="-Xms1g -Xmx12g" did the trick.
UPDATE:
Please check the comments below this post
In distributions such as RedHat the standard Tomcat structure is dispersed in different locations and the main catalina.sh is indeed not present.
There are two configuration files you may find. Assuming Tomcat 8, you'll find:
/etc/tomcat8/tomcat8.conf - this is the master file
/etc/sysconfig/tomcat8 - this is per service file which inherits from the above.
This split is designed so that you can have multiple Tomcat instances running which share some settings and have distinct settings for the 1st/2nd/3rd instance.
If you want to change the location of the software, for example, normally this would be shared so you would change /etc/tomcat8/tomcat8.conf, but if you want to change command line arguments (to pass context to a Tomcat instance) or change memory of the particular instance, you'd use /etc/sysconfig/tomcat8. You second instance of tomcat could be named tomcat8b in which case its configuration file would be /etc/sysconfig/tomcat8b.
If you have no intention of running a second instance, then change either file.
catalina.sh is under tomcat/bin

Set Java / Tomcat heap size (Xmx) without modifying catalina.sh

Most people seem to suggest setting the Java/Tomcat heap memory size for Tomcat6 by editing the /usr/share/tomcat6/bin/catalina.sh file, and adding something like:
# Set specific memory requirements for Tomcat6 (for server with ~512MB RAM).
CATALINA_OPTS="$CATALINA_OPTS -server -Xms128m -Xmx256m"
I am trying to build an Ansible playbook to configure a Tomcat-based server on Ubuntu, and it doesn't seem to me like having an entire custom catalina.sh file would be ideal—is there some other configuration file or local settings file/system used by Tomcat and/or Java to get the Xms and Xmx values?
Or... do most people use a custom catalina.sh file, and I'm just making a mountain out of a molehill?
Tomcat reads setenv.sh from bin directory for these extra settings. setenv.sh doesn't come with tomcat but you can create one for you and set CATALINA_OPTS in that.
Using JAVA_OPTS to set memory is not suggested by Tomcat because it applies to both startup and shutdown.
If you are launching via the "catalina.sh" script (directly or indirectly), then you have two options:
Modify ("hack"!) the script as suggested.
Find where the script is being invoked, and ensuring that CATALINA_OPTS is set to include the "-mx" option ... and exported ...
The latter is probably "cleaner", but there is no simple, one-size-fits-all way to get that variable set and exported. If you are launching Tomcat as a service, it depends on how your distro has wrapped Tomcat.
[Am I] just making a mountain out of a molehill?
It depends. If you are just doing this on a non-production platform, then Yes. If you are trying to build a production system that will continue to work over package upgrades and upgrades in your distro version, then No. (In the latter case, it is worth figuring out the best way to do this for your particular distro.)
For clarity: I used this in a setenv.sh script that goes in tomcat/bin. I can verify that it works for Ansible or my own install scripts:
CATALINA_OPTS="$CATALINA_OPTS -server -Xms512m -Xmx2048m"
Note that this worked for Tomcat 8 but should work for Tomcat 7 too.

Adding RAM to Tomcat

Which environment variable should I use to add more RAM to Tomcat?
JAVA_OPTS="-Xmx1024m -Xms256m"
or
CATALINA_OPTS="-Xmx1024m -Xms256m"?
Either will work. However, from skill-guru...
Note that CATALINA_OPTS is a better place than JAVA_OPTS. 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).
CATALINA_OPTS is used to control Tomcat environment options, whereas JAVA_OPTS controls the environment options at a higher level ie. for any Java library.
Windows: set CATALINA_OPTS=-Xms(min heap)m -Xmx(max heap)m
Linux: export CATALINA_OPTS=”-Xms(min heap)m -Xmx(max heap)m”
When you'll use CATALINA_OPTS then it's clear that this option is related to Tomcat server. Also when you'll use JAVA_OPTS then any other application could use this settings, and that isn't something that you probably intentionally want to achieve.
JAVA_OPTS. You are providing options to the java command, not the catalina one (which is a shell script).

Adding -javaagent to Tomcat 6 server, where do I put it and in what format?

I´m trying to install an application health monitoring application that can monitor J2EE web transactions and I need to put a javaagent into my Tomcat somehow but am not clear on exactly how to do this, I am using Linux and have been instructed by the software company that makes this product to do something like below:
-javaagent:<Path to the WebTransactionAgent.jar>
I have received further support from them and they basically said to put this into the appropriate .sh file (but they weren´t able to tell me which file that is for Tomcat)
I tried putting this in the catalina.sh file but it does not seem to be working:
JAVA_OPTS=%JAVA_OPTS% -Xms128m -Xmx512m -XX:MaxPermSize=256m -javaagent:"C:\WebTransactionAgent.jar"
Any advice is appreciated
For Unix/Linux, do this in <tomcat_home>/bin/setenv.sh, e.g.
CATALINA_OPTS="$CATALINA_OPTS -javaagent:/path/to/YourJar.jar"
You might need to create this file if not present and chmod it to 711 or 755.
For Windows, the counterpart is setenv.bat.
To add to mindas' answer, the -javaagent command could also be added to the JAVA_OPTS environment variable in one of the following (if they exist):
<tomcat_home>/conf/tomcat6.conf
JAVA_OPTS="${JAVA_OPTS} -javaagent:/full/path/to/YourJar.jar"
or <tomcat_home>/bin/catalina.sh
export JAVA_OPTS="$JAVA_OPTS -javaagent:/full/path/to/YourJar.jar"
Use JAVA_OPTS as CATALINA_OPTS would not allow JVM options [option2]=[value2].
Example, for adding jacocoagent.jar with options as below, only JAVA_OPTS will work.
JAVA_OPTS="${JAVA_OPTS} -javaagent:$CATALINA_HOME/lib/jacocoagent.jar=destfile=/tmp/jacoco.exec"

java.lang.OutOfMemoryError: Java heap space with NetBeans

This is the error I get when I run my web application in an instance of the Tomcat servlet container started by NetBeans. To fix this I even changed the heap size in netbeans.conf, but still it shows the same error. How can I keep this from happening?
HTTP Status 500 -
--------------------------------------------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Servlet execution threw an exception
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:362)
root cause
java.lang.OutOfMemoryError: Java heap space
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.9 logs.
Changing the heap size in netbeans.conf only changes the heap for NetBeans itself, not for applications run through NetBeans.
The correct way is to right-click on the project and select "Properties" and then "Run"; there you can set the VM options appropriately (-Xmx256m, for instance). It should look something like this:
(Thanks to VonC for finding this picture.)
Stop Tomcat server, set environment variable CATALINA_OPTS, and then restart Tomcat. Look at the file tomcat-install/bin/catalina.sh or catalina.bat for how this variable is used. For example,
set CATALINA_OPTS="-Xms512m -Xmx512m" (Windows)export CATALINA_OPTS="-Xms512m -Xmx512m" (ksh/bash)setenv CATALINA_OPTS "-Xms512m -Xmx512m" (tcsh/csh)
In catalina.bat or catallina.sh, you may have noticed CATALINA_OPTS, JAVA_OPTS, or both can be used to specify Tomcat JVM options.
What is the difference between CATALINA_OPTS and JAVA_OPTS?
The name CATALINA_OPTS is specific for Tomcat servlet container, whereas JAVA_OPTS may be used by other java applications (e.g., JBoss). Since environment variables are shared by all applications, we don't want Tomcat to inadvertently pick up the JVM options intended for other apps. I prefer to use CATALINA_OPTS.
How to set java heap size in JBoss?
Stop JBoss server, edit $JBOSS_HOME/bin/run.conf, and then restart JBoss server. You can change the line with JAVA_OPTS to something like:
JAVA_OPTS="-server -Xms128m -Xmx128m"
How to set java heap size in Eclipse?
You have 2 options:
Edit eclipse-home/eclipse.ini to be something like the following and
restart Eclipse.
-vmargs-Xms64m-Xmx256m
Or, you can just run eclipse command with additional options at the
very end. Anything after -vmargs will be treated as JVM options and
passed directly to the JVM. JVM options specified in the command
line this way will always override those in eclipse.ini. For
example,
eclipse -vmargs -Xms64m -Xmx256m
How to set java heap size in NetBeans?
Exit NetBeans, edit the file netbeans-install/etc/netbeans.conf. For example,
netbeans_default_options="-J-Xms512m -J-Xmx512m -J-XX:PermSize=32m -J-XX:MaxPermSize=128m -J-Xverify:none
How to set java heap size in Apache Ant?
Set environment variable ANT_OPTS. Look at the file $ANT_HOME/bin/ant or %ANT_HOME%\bin\ant.bat, for how this variable is used by Ant runtime.
set ANT_OPTS="-Xms512m -Xmx512m" (Windows)export ANT_OPTS="-Xms512m -Xmx512m" (ksh/bash)setenv ANT_OPTS "-Xms512m -Xmx512m" (tcsh/csh)
If you increase the virtual memory of your Tomcat server then it will be OK.
Steps:
In NB go through the windows menu and add Services
You will find Tomcat in the services. Right click on Tomcat server and select Properties
Go to the platform in the properties and write -Xms512m in VM options field
I'm guessing that increasing the memory won't fix the problem. What is that MonitorFilter doing? What's eating up all that memory?
Your best bet is to figure that out. If this is a web app, see if you can turn off that filter and run without it. If you have success, you know that the MonitorFilter is causing your to fail.
This has nothing to do with NetBeans (well, perhaps), rather it has to do with Tomcat. Tomcat is the process that is running out of heap, not NetBeans. Track down the startup process for your Tomcat. If it's bundled with NB, then Tomcat is buried within the NB installation, check for an "enterpriseN" directory, N being a number, Tomcat is probably in there and it's a rather generic distribution of it.
As to why the monitor is run OOM, that's hard to say, it's a pretty simple process when you think about it. You can also try disabling HTTP monitoring to see if it's a problem with the Monitoring itself or something with your application.

Categories

Resources