Adding RAM to Tomcat - java

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).

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.

CATALINA_OPTS vs JAVA_OPTS - What is the difference?

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.

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"

cross server environment variable

I would like to set to be able to set a string variable such as "DEVEL" or "PRODUCTION", in Glassfishv3 and Apache tomcat 6 servers, and want it to be accessible from java code so I can change behaviour of my app according to the variable. What is the easiest way of doing that?
I do not think there is a method of setting an environment variable that works for both of these servers. There are methods to set an environment variable for each of these servers though.
To set a system property that can be detected by your web application at run-time:
Tomcat : Set the value of the environment variable CATALINA_OPTS and start the server.
export CATALINA_OPTS=-DmyPropertyName=myPropertyValue
GlassFish 3 : There are a couple methods that you can use.
Direct use of a GlassFish system property. Start the server. Use the asadmin command 'create-system-properties' to define a System property. Restart the server.
asadmin create-system-property myPropertyName=myPropertyValue.
Direct use of a JVM Option: Start the server. Use the asadmin command 'create-jvm-option' to create a new JVM option that defines the System property that will be used when the server is started. Restart the server so that it uses the property.
asadmin create-jvm-options -DmyPropertyName=myPropertyValue
To change the value of a system property that can be detected by your web application at run-time:
Tomcat : Change the value of the CATALINA_OPTS environment variable and restart your server.
export CATALINA_OPTS=-DmyPropertyName=myNEWPropertyValue
GlassFish 3 : The method to change the property value depends on the method you used to set the property value.
Direct use of a GlassFish system property. Recreate the GlassFish system property with the 'create-system-properties' command and restart the server.
asadmin create-system-property myPropertyName=myNEWPropertyValue
Direct use of a JVM option: Delete the old jvm option and create a new one in its place. Restart the server.
asadmin delete-jvm-options -DmyPropertyName=myPropertyValue
asadmin create-jvm-options -DmyPropertyName=myNEWPropertyValue
See http://java.net/jira/browse/GLASSFISH-11253
To unset the value of a system property that can be detected by your web application at run-time:
Tomcat : Reset the value of the environment variable CATALINA_OPTS, without including the JVM Option definition. Restart the server.
export CATALINA_OPTS=
GlassFish 3 :
Using a GlassFish system property. Use the 'delete-system-properties' command and restart the server.
asadmin delete-system-property myPropertyName
Using a JVM Option to define the property. Use the delete-jvm-options command and restart the server.
asadmin delete-jvm-options -DmyPropertyName=myPropertyValue
To access a system property from inside your web application
Use System.getProperty(String) or [System.getProperty(String,String)][2]
After saying all this, I want to discourage you from using this info to pursue your stated development strategy.
There are other methods to differentiate a development and production environment that do not involve code changes.
[2]: http://download.oracle.com/javase/6/docs/api/java/lang/System.html#getProperty(java.lang.String, java.lang.String)
For tomcat you can use CATALINA_OPTS environment variable in order to set system properties:
CATALINA_OPTS (Optional) Java runtime options used when the "start",
or "run" command is executed.
So you can setup this in IDE or just in command line:
CATALINA_OPTS=-DdevelopmentMode=true
And then in your app:
System.getProperty("developmentMode")
Use an env-entry in the web.xml :
<env-entry>
<description>development or production</description>
<env-entry-name>devMode</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>PRODUCTION</env-entry-value>
</env-entry>
And in the Java code, you can do :
Context ctx = new InitialContext();
String devMode = (String) ctx.lookup("java:/comp/env/devMode");
It depends whether it should be constant or dynamic variable. Whether you want to change it during compilation time or in the runtime on the fly. With 1st approach you may do that in web.xml setting init parameter or env-entries or even use simple property file packed in the jar archive. With the 2nd approach you could use special kind of configuration table in your database which would be pooled by the application piropdically or maybe you could harness JMX management mechanism and modify configuration on the fly using jconsole

Categories

Resources