Tomcat Tuning Notes (applies to all versions and a good start)
** Exerpted from:
http://jakarta.apache.org/tomcat/tomcat-3.2-doc/uguide/tomcat_ug.html.
Please see this for the complete Tomcat User Guide.
Real World Configuration Tips
By default the Tomcat distribution comes with a naive configuration
whose main goal is to promote first time user experience and an "out of
the box" operation... This configuration however is not the best way to
deploy Tomcat on real sites. For example, real sites may require some
performance tuning and site-specific settings (additional path elements
for example). This section will try to get you started by directing you
to the first steps that should be taken before publishing a Tomcat based
site.
Modify and Customize the Batch Files
As stated in the previous sections, the startup scripts are here for
your convenient. Yet, sometimes the scripts that are needed for
deployment should be modified:
To set resource limits such as maximum number of descriptors.
To add new CLASSPATH entries (for example, JDBC drivers).
To add new PATH/LD_LIBRARY_PATH entries (for example, JDBC drivers
DLLs).
To modify the JVM command line settings.
Make sure that you are using a specific JVM (out of the two or three
JVMs installed on your machine).
To switch user from root to some other user using the "su" UNIX command.
Your pet reason.
Some of these changes can be done without explicit changes to the basic
scripts; for example, the tomcat script can use an environment variable
named TOMCAT_OPTS to set extra command line parameters to the JVM (such
as memory setting etc.). On UNIX you can also create a file named
".tomcatrc" in your home directory and Tomcat will take environment
information such as PATH, JAVA_HOME, TOMCAT_HOME and CLASSPATH from this
file. On NT however (and also on UNIX when the modifications are for
something such as the JVM command line) you are forced to rewrite some
of the startup script...
Do not hesitate, just do it.
Modify the Default JVM Settings
The default JVM settings in the tomcat script are very na�ve; everything
is left for defaults. There are a few things that you should consider to
improve your Tomcat performance:
Modify your JVM memory configuration. Normally the JVM allocates an
initial size for the Java heap and that's it, if you need more then this
amount of memory you will not get it.
Nevertheless, in loaded sites, giving more memory to the JVM improves
Tomcat's performance. You should use command line parameters such as
-Xms/-Xmx/-ms/-mx to set the minimum/maximum size of the Java heap (and
check to see if the performance was improved).
Modify your JVM threading configuration. The SUN JDK1.2.2 for Linux
comes with support for both, green and native threads. In general native
threads are known to provide improved performance for I/O bound
applications, green threads on the other hand put less stress on the
machine. You should experiment with these two threading models and see
which model is better for your site (in general, native threads are
better).
Select the best JVM for the task. There are several JVM vendors, for
example on Linux there are today (21/03/2000) two product level JVMs:
the SUN JDK1.2.2 and the IBM JDK1.1.8. If your application does not
require a specific JDK functionality, you should benchmark the two JVMs
and select the better one. In my (Gal Shachor) internal tests I found
the IBM JVM significantly faster than the one created by SUN, you should
check that for yourself and make a calculated decision.
Modify your Connectors
The Connectors, as configured in Tomcat's default server.xml contains
two Connectors configured as in the next server.xml fragment:
The two default Connectors in server.xml
<!-- (1) HTTP Connector for stand-alone operation -->
<Connector
className="org.apache.tomcat.service.PoolTcpConnector">
<Parameter name="handler"
value="org.apache.tomcat.service.http.HttpConnectionHandler"/>
<Parameter name="port"
value="8080"/>
</Connector>
<!-- (2) AJPV12 Connector for out-of-process operation -->
<Connector
className="org.apache.tomcat.service.PoolTcpConnector">
<Parameter name="handler"
value="org.apache.tomcat.service.connector.Ajp12ConnectionHandler"/>
<Parameter name="port"
value="8007"/>
</Connector>
Is a Connector that listens on port 8080 for incoming HTTP requests.
This connector is needed for stand-alone operation.
Is a Connector that listens on port 8007 for incoming AJPV12 requests.
This connector is needed for web-server integration (out-of-process
servlet integration).
The AJPV12 Connector is required for Tomcat shutdown. However, the HTTP
Connector may be removed if stand-alone operation is not needed.
Use a Thread Pool in your Connectors
Tomcat is a multi-threaded servlet container this means that each
request needs to be executed by some thread. Prior to Tomcat 3.2, the
default was to create a new thread to serve each request that arrives.
This behavior is problematic for loaded sites because:
Starting and stopping a thread for every request puts a needless burden
on the operating system and the JVM.
It is hard to limit the resource consumption. If 300 requests arrive
concurrently Tomcat will open 300 threads to serve them and allocate all
the resources needed to serve all the 300 requests at the same time.
This causes Tomcat to allocate much more resources (CPU, Memory,
Descriptors...) than it should and it can lead to low performance and
even crashes if resources are exhausted.
The solution for these problems is to use a thread pool, which is the
default for Tomcat 3.2. Servlet containers that are using a thread pool
relieve themselves from directly managing their threads. Instead of
allocating new threads; whenever they need a thread they ask for it from
the pool, and when they are done, the thread is returned to the pool.
The thread pool can now be used to implement sophisticated thread
management techniques, such as:
Keeping threads "open" and reusing them over and over again. This saves
the trouble associated with creating and destroying threads
continuously.
Usually the administrator can instruct the pool not to keep too many
idle threads, freeing them if needed.
Setting an upper bound on the number of threads used concurrently. This
prevents the resources allocation problem associated with unlimited
thread allocation.
If the container maxed out to the threads upper limit, and a new request
arrives, the new request will have to wait for some other (previous)
request to finish and free the thread used to service it.
You can refine the techniques described above in various ways, but these
are only refinements. The main contribution of thread pools is
thread-reuse and having a concurrency upper bound that limits resource
usage.
Using a thread pool in Tomcat is a simple move; all you need to do is to
use a PoolTcpConnector in your <Connector> configuration. For example
the following server.xml fragment defines ajpv12, pooled Connector:
Pooled ajpv12 Connector
<!-- A pooled AJPV12 Connector for out-of-process operation -->
<Connector
className="org.apache.tomcat.service.PoolTcpConnector">
<Parameter
name="handler"
value="org.apache.tomcat.service.connector.Ajp12ConnectionHandler"/>
<Parameter
name="port"
value="8007"/>
</Connector>
This fragment is very simple and the (default) pool behaviour instructed
by it is:
Upper bound for concurrency of 50 threads.
When the pool has more then 25 threads standing idle it will start to
kill them.
The pool will start 10 threads on creation, and it will try to keep 10
vacant threads (as long as the upper bound is kept).
The default configuration is suitable for medium load sites with an
average of 10-40 concurrent requests. If your site differs you should
modify this configuration (for example reduce the upper limit).
Configuring the pool can be done through the <Connector> element in
server.xml as demonstrated in the next fragment:
Configuring the Thread Pool
<!-- A pooled AJPV12 Connector for out-of-process operation -->
<Connector
className="org.apache.tomcat.service.PoolTcpConnector">
<Parameter
name="handler"
value="org.apache.tomcat.service.connector.Ajp12ConnectionHandler"/>
<Parameter
name="port"
value="8007"/>
<Parameter
name="max_threads"
value="30"/>
<Parameter
name="max_spare_threads"
value="20"/>
<Parameter
name="min_spare_threads"
value="5" />
</Connector>
As can be seen the pool has 3 configuration parameters:
max_threads - defines the upper bound to the for the concurrency, the
pool will not create more then this number of threads.
max_spare_threads - defines the maximum number of threads that the pool
will keep idle. If the number of idle threads passes the value of
max_spare_threads the pool will kill these threads.
min_spare_threads - the pool will try to make sure that at any time
there is at least this number of idle threads waiting for new requests
to arrive. min_spare_threads must be bigger then 0.
You should use the above parameters to adjust the pool behavior to your
needs.
Disable Servlet Auto-Reloading
Servlet auto-reloading is really useful for development time. However it
is very expensive (in performance degradation terms) and may put your
application in strange conflicts when classes that were loaded by a
certain classloader cannot co-operate with classes loaded by the current
classloader.
So, unless you have a real need for class reloading during your
deployment you should turn off the reloadable flag in your contexts.
On Wed, 2002-10-30 at 09:39, [EMAIL PROTECTED] wrote:
> I'm running Tomcat 4 on a Solaris 8 Sparc server. I've been watching the
> traffic increase on my server as I add more and more resources over time.
> In the process of this I've increased the maximum memory the JVM uses to
> keep Tomcat from running out of memory. Then I had to increase the maximum
> simultaneous users for the database connection. The next thing I was
> getting was no processors error so I increased the maximum processors and
> that took care of that problem. Now I'm at a stopping point. The server
> doesn't seem to be working very hard. The CPU activity is low and memory
> usage is well below what the maximum is set to. The catalina log doesn't
> show anymore errors about no processor. All it shows is when another
> processor is started. After all this it's still running slow. I've had the
> network part checked and it's doing just fine. My question is how can I fix
> this problem. It seems to me like it's not allowing enough connections or
> something. For example when you load a page from the software it will just
> sit there for a little or a lot and then all of a sudden boom it sends the
> whole page. It's like the server won't service enough people a once. Can
> someone give me some direction?
>
>
> Thank You,
>
> Justin A. Stanczak
> Web Manager
> Shake Learning Resource Center
> Vincennes University
> (812)888-5813
>
>
>
> --
> To unsubscribe, e-mail: <mailto:tomcat-user-unsubscribe@;jakarta.apache.org>
> For additional commands, e-mail: <mailto:tomcat-user-help@;jakarta.apache.org>
>
--
To unsubscribe, e-mail: <mailto:tomcat-user-unsubscribe@;jakarta.apache.org>
For additional commands, e-mail: <mailto:tomcat-user-help@;jakarta.apache.org>