42

I need to configure Tomcat memory settings as part of a larger installation, so manually configuring tomcat with the configuration app after the fact is out of the question. I thought I could just throw the JVM memory settings into the JAVA_OPTS environment variable, but I'm testing that with jconsole to see if it works and it... doesn't.

As per the comment below, CATALINA_OPTS doesn't work either. So far, the only way I can get it to work is via the Tomcat configuration GUI, and that's not an acceptable solution for my problem.

f_puras
  • 2,521
  • 4
  • 33
  • 38
red
  • 684
  • 1
  • 6
  • 10

13 Answers13

71

Serhii's suggestion works and here is some more detail.

If you look in your installation's bin directory you will see catalina.sh or .bat scripts. If you look in these you will see that they run a setenv.sh or setenv.bat script respectively, if it exists, to set environment variables. The relevant environment variables are described in the comments at the top of catalina.sh/bat. To use them create, for example, a file $CATALINA_HOME/bin/setenv.sh with contents

export JAVA_OPTS="-server -Xmx512m"

For Windows you will need, in setenv.bat, something like

set JAVA_OPTS=-server -Xmx768m

Hope this helps, Glenn

Glenn
  • 6,455
  • 4
  • 33
  • 42
  • 2
    "Some other" guide neglected to put in the "export" command and tomcat wouldn't start. I should have figured this out since it is, after all, a .sh file...but thanks for spelling it out so nicely for a sleepy coder... – jsh Feb 22 '12 at 15:23
  • For some reason the RPM package (e.g. in CentOS) for tomcat6 has an init.d that doesn't use catalina.sh (which is the one calling setnev.sh) so in other words - on CentOS /etc/tomcat6/tomcat6.conf is the place for such configurations – Eran Medan Apr 30 '14 at 19:18
24

Create a setenv.(sh|bat) file in the tomcat/bin directory with the environment variables that you want modified.

The catalina script checks if the setenv script exists and runs it to set the environment variables. This way you can change the parameters to only one instance of tomcat and is easier to copy it to another instance.

Probably your configuration app has created the setenv script and thats why tomcat is ignoring the environment variables.

Serxipc
  • 6,639
  • 9
  • 40
  • 51
5

Use the CATALINA_OPTS environment variable.

matt b
  • 138,234
  • 66
  • 282
  • 345
  • I've tried CATALINA_OPTS, too. I'm pretty sure that only works for Tomcat 4.0 and earlier. – red Nov 13 '08 at 02:27
  • I can guarantee it works for 5.0.28. Check the startup.bat/sh and catalina.bat/sh to check what's being used. Perhaps you are passing the arguments in the incorrect format? – matt b Nov 13 '08 at 02:50
5

If you using Ubuntu 11.10 and apache-tomcat6 (installing from apt-get), you can put this configuration at /usr/share/tomcat6/bin/catalina.sh

# -----------------------------------------------------------------------------

JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8 -server -Xms1024m \
-Xmx1024m -XX:NewSize=512m -XX:MaxNewSize=512m -XX:PermSize=512m \
-XX:MaxPermSize=512m -XX:+DisableExplicitGC"

After that, you can check your configuration via ps -ef | grep tomcat :)

martinusadyh
  • 1,120
  • 1
  • 8
  • 11
3

I use following setenv.bat contents:

==============setenv.bat============

    set JAVA_OPTS=-XX:MaxPermSize=256m -Xms256M -Xmx768M -Xdebug -Xnoagent  -Xrunjdwp:transport=dt_socket,address=7777,server=y,suspend=n %JAVA_OPTS%

====================================

It also enables debugging and sets debug port to 7777, and appends previous content of JAVA_OPTS.

prayagupa
  • 30,204
  • 14
  • 155
  • 192
3

Handy for linux virtual machines; Use 75% of your total system memory for Tomcat. Yay AWK.

Put at start of "{tomcat}/bin/startup.sh"

export CATALINA_OPTS="-Xmx`cat /proc/meminfo | grep MemTotal | awk '{ print $2*0.75 } '`k"
DrTune
  • 39
  • 2
2

Just to add to the previous comment, the documentation for the command line tool for updating the Tomcat service settings (if Tomcat is running as a service on Windows) is here. This tool updates the registry with the proper settings. So if you wanted to update the max memory setting for the Tomcat service you could run this (from the tomcat/bin directory), assuming the default service name of Tomcat5:

tomcat5 //US//Tomcat5 --JvmMx=512
Cozzman
  • 86
  • 3
2

I like the idea of seting tomcat6 memory based on available server memory (it is cool because I don't have to change the setup after hardware upgrade). Here is my (a bit extended memory setup):

export CATALINA_OPTS="-Xmx`cat /proc/meminfo | grep MemTotal | awk '{ print $2*0.75 } '`k -Xms`cat /proc/meminfo | grep MemTotal | awk '{ print $2*0.75 } '`k -XX:NewSize=`cat /proc/meminfo | grep MemTotal | awk '{ print $2*0.15 } '`k -XX:MaxNewSize=`cat /proc/meminfo | grep MemTotal | awk '{ print $2*0.15 } '`k -XX:PermSize=`cat /proc/meminfo | grep MemTotal | awk '{ print $2*0.15 } '`k -XX:MaxPermSize=`cat /proc/meminfo | grep MemTotal | awk '{ print $2*0.15 } '`k"

Put it to: "{tomcat}/bin/startup.sh" (e.g. "/usr/share/tomcat6/bin" for Ubuntu 10.10)

Andre
  • 3,874
  • 3
  • 35
  • 50
Ondrej Kvasnovsky
  • 4,592
  • 3
  • 30
  • 40
1

If you run Tomcat on Windows, you can use the neat "Tomcat Monitor" application that ships with Tomcat.

Go to the Java tab. At the bottom, below the "Java Options" textarea, you will find 3 input fields:

  • Initial memory pool ___ MB
  • Maximum memory pool ___ MB
  • Thread stack size _____ KB
Erel Segal-Halevi
  • 33,955
  • 36
  • 114
  • 183
  • I changed this and save.But when I try to start it again, the tomcat is not being started. Once I change back to default memory size, I am able to start tomcat again. Can you please suggest what to do? – Ankita Mar 10 '15 at 13:29
  • @Ankita I don't work with Tomcat anymore... it's too complicated. I switched to Node.js – Erel Segal-Halevi Mar 10 '15 at 18:12
1

Not sure that it will be applicable solution for you. But the only way for monitoring tomcat memory settings as well as number of connections etc. that actually works for us is Lambda Probe.

It shows most of informations that we need for Tomcat tunning. We tested it with Tomcat 5.5 and 6.0 and it works fine despite beta status and date of last update in end of 2006.

FoxyBOA
  • 5,788
  • 8
  • 48
  • 82
1

If you'd start Tomcat manually (not as service), then the CATALINA_OPTS environment variable is the way to go. If you'd start it as a service, then the settings are probably stored somewhere in the registry. I have Tomcat 6 installed in my machine and I found the settings at the HKLM\SOFTWARE\Apache Software Foundation\Procrun 2.0\Tomcat6\Parameters\Java key.

kgiannakakis
  • 103,016
  • 27
  • 158
  • 194
0

Just edit your your catalina/bin/startup.sh script. Add the following commands in it:

#Adjust it to the size you want. Ignore the from bit.
export CATALINA_OPTS="-Xmx1024m"
#This should point to your catalina base directory 
export CATALINA_BASE=/usr/local/tomcat
#This is only used if you editing the instance of your tomcat
/usr/share/tomcat6/bin/startup.sh

Sailab: http://www.facejar.com/member/page-id-477.html

Sailab Rahi
  • 581
  • 1
  • 8
  • 11
0

In my case there was a /etc/sysconfig/tomcat5.conf file overwriting all settings in /etc/tomcat5/tomcat5.conf

Torge
  • 2,174
  • 1
  • 23
  • 33