Tomcat Tuning Summary

Source: Internet
Author: User
Tags app service config garbage collection socket thread server port tomcat xms
Tomcat optimization is divided into system optimization, Java Virtual machine tuning, Tomcat itself optimization.
How Tomcat stops./catalina.sh stop./catalina.sh start
/sbin/service Tomcat restart/webagme/tomcat/bin/catalina.sh Start 1. How to adjust Tomcat's memory footprint A:The method is as follows: 1. Under Linux, edit the catalina.sh file under the Bin directory under the Tomcat installation directory under Windows for Catalina.bat VI catalina.sh 2. Find the tomcat memory parameter line:/java_opts, if not found, write 3 on the first line. Will java_opts= "-xms 1024x768M–xmx 1520M "row of two parameters according to the actual amount of memory of the server to make changes:-XMS for tomcat boot initial memory, generally for the server after the free memory minus 100M-XMX for Tomcat maximum memory, usually after the server power free memory minus 5 0M Generally, you should use 80% of the physical memory as the heap size. Note: The above two parameters relate to the access performance that Tomcat is subjected to, but it is also set according to the actual memory of the server. It is suggested that XMS and XMX are the same value, saying that it can speed up memory recovery. But without my verification. Interested to try. The size of these two values is typically configured as needed. The size of the initialized heap performs the amount of memory requested by the virtual machine to the system at startup. Generally speaking, this parameter is not important. However, some applications in the case of heavy load will take up more memory, when this parameter is very important, if the virtual machine is configured to use less memory and in this case there are many objects to initialize, the virtual machine must repeatedly increase the memory to meet the use. For this reason, we generally set-XMS and-xmx to be the same size, and the maximum value of the heap is limited by the physical memory used by the system. Applications that use large amounts of data typically use persistent objects, and memory usage can grow rapidly. When the application requires more memory than the maximum heap value, the virtual machine prompts for a memory overflow and causes the app service to crash. Therefore, it is generally recommended that the maximum value of the heap be configured to 80% of the maximum available memory.


Tomcat uses 128MB of memory by default, and in larger applications, this memory is not enough and needs to be scaled up.


Under Windows, under Document/bin/catalina.bat,unix, in front of document/BIN/CATALINA.SH, add the following configuration:


Java_opts= '-xms "Initialize memory size"-XMX "Maximum memory Available" '


You need to increase the value of this two parameter. For example:


java_opts= '-xms256m-xmx512m '


Indicates that the initialized memory is 256MB and the maximum memory that can be used is 512MB.


Another thing to consider is the garbage collection mechanism provided by Java. The heap size of a virtual machine determines the time and frequency at which the virtual machine spends collecting garbage. The rate at which garbage collection can be accepted is related to the application and should be adjusted by analyzing the time and frequency of actual garbage collection. If the heap size is large, then the total garbage collection will be slow, but the frequency will be reduced. If you match the size of the heap to the needs of the memory, the collection will be quick, but more frequent. The purpose of sizing the heap is to minimize garbage collection time to maximize processing of customer requests over a specific time period. In benchmarking, to ensure the best performance, the size of the heap must be large to ensure that garbage collection does not occur throughout the benchmark test process.


If the system spends a lot of time collecting garbage, please reduce the heap size. A complete garbage collection should be no more than 3-5 seconds. If garbage collection becomes a bottleneck, you need to specify the size of the generation, check the detailed output of the garbage collection, and study the effects of garbage collection parameters on performance. Generally, you should use 80% of the physical memory as the heap size. When adding the processor, remember to increase the memory, because the allocation can be done in parallel, and garbage collection is not parallel.
2. How to adjust the thread parameters of Tomcat A:Here's how: 5. Edit the Server.xml file in the Conf directory under the Tomcat installation directory in the <connector/> configuration in the Tomcat configuration file Server.xml, the parameters related to the connection number are: maxthreads= " 150 "means that up to 150 connections are processed at the same time, and Tomcat uses threads to handle each request received. This value represents the maximum number of threads that Tomcat can create. The default value is 200.
Minsparethreads= "25" means that even if no one is using it, open so many empty threads waiting
maxsparethreads= "75" means that if you can empty up to 75 threads, such as 80 people at a time, and no one has access after that, Tomcat does not keep 80 empty threads, but turns off 5 empty. (Once you create a thread that exceeds this value, Tomcat shuts down the socket thread that is no longer needed.) The default value is 50.


acceptcount= "100" when the number of simultaneous connections reaches MaxThreads, the number of connections queued can also be received, and the connection will be returned directly to the denied connection. (Specifies the number of requests that can be placed in the processing queue when any number of threads that can be used to process requests is used, and requests that exceed this number will not be processed.) The default value is 100. The parameters associated with the maximum number of connections are maxthreads and Acceptcount. If you want to increase the number of concurrent connections, both parameters should be increased.
The maximum number of connections allowed by Web server is also subject to the kernel parameter settings of the operating system, usually Windows is about 2000, and Linux is about 1000. Examples of configurations in TOMCAT5:
<connector port= "8080"
maxthreads= "minsparethreads=" maxsparethreads= "75"
Acceptcount= "/>
"
mainly to adjust the values of MaxThreads and Acceptcount.
The listening configuration for the other ports, and so on. In the configuration in Tomcat Configuration document Server.xml, additional parameters related to the number of connections are:

Enablelookups:

Whether to reverse the domain name, the default value is true. To improve processing power, it should be configured to False

Connnectiontimeout:

Network connection timed out, default value 60000, in milliseconds. A configuration of 0 means that never times out, so the configuration is hidden. Typically, it can be configured for 30000 milliseconds.

Maxkeepaliverequests:nginx Dynamic Transfer to Tomcat,nginx is not keepalive, and the Tomcat side of the default open keepalive, will wait for the KeepAlive timeout, Default is not set to use ConnectionTimeout.
So you must set the Tomcat timeout and turn off Tomcat's keepalive. Otherwise, a large number of Tomcat socket timewait will be generated.
maxkeepaliverequests= "1" prevents Tomcat from producing a large number of time_wait connections, which in some way avoids the ability to feign death.
Try setting Tomcat/conf/server.xml:

maxkeepaliverequests= "1"
connectiontimeout= "20000"

maxkeepaliverequests= "1" means that each connection is closed only once, so that it does not wait for timeout. <connector executor= "Tomcatthreadpool"
port= "8080" protocol= "http/1.1"
Connectiontimeout= "30000" maxkeepaliverequests= "1"
Redirectport= "8443" buffersize= "8192" sockedbuffer= "65536" acceptcount= "$"/>

BufferSize:

Input stream buffer size, default value 2048 bytes.

Compression

Compressed transfer, value On/off/force, default value off. 3. How to disable and allow documents in a column directory in Tomcat

In {tomcat_home}/conf/web.xml, configure the listings parameter to False, as follows:



<servlet>
...
<init-param>
<param-name>listings</param-name>
<param-value>false</param-value>
</init-param>
...
</servlet>
4. How to disable and allow host or IP address access in Tomcat
...
<valve classname= "Org.apache.catalina.valves.RemoteHostValve"
allow= "*.mycompany.com,www.yourcompany.com"/>
<valve classname= "Org.apache.catalina.valves.RemoteAddrValve"
deny= "192.168.1.*"/>
...
</Host>
Cat/webgame/tomcat/conf/server.xml

<?xml version= ' 1.0 ' encoding= ' utf-8 '?>
<!--
Licensed to the Apache software Foundation (ASF) under one or more
Contributor license agreements. See the NOTICE file distributed with
This is for additional information regarding copyright ownership.
The ASF licenses this file to you under the Apache License, Version 2.0
(the "License"); Except in compliance with
The License. Obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

  unless required by applicable or agreed to writing, software
  distributed under the License is Distributed on a "as is" BASIS,
  without warranties or CONDITIONS of any KIND, either express OR implied.
& nbsp See the License for the specific language governing permissions and
  limitations under the License.
--> <!--note:  a "Server" is not itself a ' Container ', so if not
     define Subcompo Nents such as "valves" at the This level.
     documentation at/docs/config/server.html
 -->
<server port= "8005" shutdown= "Shutdown" >

<!--APR Library loader. Documentation At/docs/apr.html--
<listener classname= "Org.apache.catalina.core.AprLifecycleListener" sslengine= "on"/>
<!--Initialize Jasper prior to WebApps is loaded. Documentation At/docs/jasper-howto.html--
<listener classname= "Org.apache.catalina.core.JasperListener"/>
<!--JMX support for the TOMCAT server. Documentation At/docs/non-existent.html--
<listener classname= "Org.apache.catalina.mbeans.ServerLifecycleListener"/>
<listener classname= "Org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>

<!--Global JNDI Resources
Documentation at/docs/jndi-resources-howto.html
-
<GlobalNamingResources>
<!--Editable User database that can also is used by
Userdatabaserealm to authenticate users
-
<resource name= "Userdatabase" auth= "Container"
Type= "Org.apache.catalina.UserDatabase"
deion= "User database that can be updated and saved"
factory= "Org.apache.catalina.users.MemoryUserDatabaseFactory"
Pathname= "Conf/tomcat-users.xml"/>
</GlobalNamingResources>

<!--a "Service" is a collection of one or more "connectors" that share
A single "Container" Note:a "Service" was not itself A "Container",
Define subcomponents such as "valves" at the This level.
Documentation at/docs/config/service.html
-
<service name= "Catalina" >

<!--the connectors can use a GKFX executor, you can define one or more named thread pools-->
<executor name= "Tomcatthreadpool" nameprefix= "catalina-exec-"
maxthreads= "minsparethreads=" maxidletime= "60000"/>


<!--A "Connector" represents an endpoint by which requests is received
and responses are returned. Documentation at:
Java HTTP Connector:/docs/config/http.html (Blocking & non-blocking)
Java AJP Connector:/docs/config/ajp.html
APR (HTTP/AJP) Connector:/docs/apr.html
Define a Non-ssl http/1.1 Connector on port 8080
-
<!--
<connector port= "8080" protocol= "http/1.1"
connectiontimeout= "20000"
Redirectport= "8443" maxthreads= "/>"
-
<!--A "Connector" using the shared thread pool-->
<connector executor= "Tomcatthreadpool"
port= "8080" protocol= "http/1.1"
connectiontimeout= "20000"
Redirectport= "8443" maxkeepaliverequests= "1"/>
<!--Define a SSL http/1.1 Connector on port 8443
This connector uses the JSSE configuration, when using APR, the
Connector should be using the the OpenSSL style configuration
Described in the APR documentation--
<!--
<connector port= "8443" protocol= "http/1.1" sslenabled= "true"
maxthreads= "Scheme=" "https" secure= "true"
Clientauth= "false" sslprotocol= "TLS"/>
-

<!--Define an AJP 1.3 Connector on port 8009-
<connector port= "8009" protocol= "ajp/1.3" redirectport= "8443"/>


<!--an Engine represents the entry point (within Catalina) that processes
Every request. The Engine implementation for Tomcat stand alone
Analyzes the HTTP headers included with the request, and passes them
The appropriate host (virtual host).
Documentation At/docs/config/engine.html--

<!--should set Jvmroute to support load-balancing via AJP ie:
<engine name= "Catalina" defaulthost= "localhost" jvmroute= "jvm1" >
-
<engine name= "Catalina" defaulthost= "localhost" >

      <!--for clustering, documentation a look at:
   & nbsp;     /docs/cluster-howto.html  (Simple)
          /docs/config/cluster.html (reference documentation)-->
       <!--
      <cluster classname= " Org.apache.catalina.ha.tcp.SimpleTcpCluster "/>
     -->        

      <!--the request dumper valve dumps useful debugging information about
&NBSP;&NB sp;         the request and response data received and sent by Tomcat.
           documentation at:/docs/config/valve.html--
      <!--
      <valve classname= " Org.apache.catalina.valves.RequestDumperValve "/>
     

      <!--This Realm uses the userdatabase configured in the global JNDI
  & nbsp;        resources under the key "Userdatabase" .  any edits
            that is performed against this userdatabase is immediately
           available for use by the realm. -->
      <realm classname= "Org.apache.catalina.realm.UserDatabaseRealm"
              resourcename= "Userdatabase"/>

      <!--Define The default virtual host
            note:xml Schema validation won't work with Xerces 2.2.
      -->
                  unpackwars= "true" Autodeploy= "true"
            xmlvalidation= "false" Xmlnamespaceaware= "false" >

        <!--Singlesignon valve, share authentication between web Applications
             documentation at:/docs/ config/valve.html-->
        <!--
         <valve classname= "Org.apache.catalina.authenticator.SingleSignOn"/>
        

<!--Access log processes all example.
Documentation at:/docs/config/valve.html--
<!--
<valve classname= "Org.apache.catalina.valves.AccessLogValve" directory= "Logs"
Prefix= "Localhost_access_log." suffix= ". txt" pattern= "common" resolvehosts= "false"/>
-

</Host>
</Engine>
</Service>
</Server>

Reference Document: Tomcat Tuning Configuration Tips Collection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.