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

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.

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

In which file Tomcat Java Memory options are saved

I want to change memory configuration of Tomcat. I know it can be done at
Start > Apache Tomcat > Configure Tomcat > Java Tab] as can be seen below
and specifying options like
-Xms512m
-Xmx1024m
-XX:MaxPermSize=512m
-XX:MaxHeapFreeRatio=70
there.
However this program group is created if I install Tomcat from an executable. I have installed Tomcat by simply extracting the zip which does not create Program group. So now where can I specify this information
With regards to the question of where Tomcat's memory options are saved when using the service version, on my machine at least they can be found in the Windows Registry at:
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Apache Software Foundation\Procrun 2.0\Tomcat-6\Parameters\Java
Initial memory and Maximum memory are recorded against the JvmMs and JvmMx values, while the options text is recorded against the Options value.
The Tomcat-6 portion of the address on mine is, I believe, based on the identifier I gave to the service (i.e. within the service manager Tomcat is named "Apache Tomcat 6.0 Tomcat-6"), and you may not need to go through Wow6432Node depending on whether you're using 32-bit or 64-bit Tomcat.
I'm assuming here that they haven't made any dramatic changes in where this information is stored in versions of Tomcat more recent than Tomcat 6.
Ohkay while digging inside Tomcat I found some information in one of its txt files (point 3.4) and online:
Apart from CATALINA_HOME (which is set to apache-tomcat-x.x.x directory, for e.g. C:\Program Files\Apache Software Foundation\apache-tomcat-7.0.41) and CATALINA_BASE (optional, defaults to CATALINA_HOME), all environment variables can
be specified in the setenv script. The script is placed either into
CATALINA_BASE/bin or into CATALINA_HOME/bin directory and is named
setenv.bat (on Windows) or setenv.sh (on *nix). The file has to be
readable.
By default the setenv script file is absent. If the script file is present
both in CATALINA_BASE and in CATALINA_HOME, the one in CATALINA_BASE is
preferred.
For example, to configure the JRE_HOME and CATALINA_PID variables you can
create the following script file:
On Windows, %CATALINA_BASE%\bin\setenv.bat:
set "JRE_HOME=%ProgramFiles%\Java\jre6"
exit /b 0
On *nix, $CATALINA_BASE/bin/setenv.sh:
JRE_HOME=/usr/java/latest
CATALINA_PID="$CATALINA_BASE/tomcat.pid"
To set Java options for memory allocation for web app, add something like following (as per memory requirements of your application) to the same file
set JAVA_OPTS="-Xms256m -Xmx512m"
The CATALINA_HOME and CATALINA_BASE variables cannot be configured in the
setenv script, because they are used to locate that file.
All the environment variables described here and the setenv script are
used only if you use the standard scripts to launch Tomcat. For example, if
you have installed Tomcat as a service on Windows, the service wrapper
launches Java directly and does not use the script files.

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