Optimizing Tomcat Configuration (4 aspects from memory, concurrency, caching)

Source: Internet
Author: User
Tags reserved server memory xms apache tomcat

Tomcat is often the default development environment for Javaweb practitioners, but the default configuration of Tomcat as a production environment, especially memory and thread configuration, is low by default and is easily a performance bottleneck.

Fortunately, Tomcat also has a lot of room for improvement. Below is a description of Tomcat optimizations, which can be divided into memory, threads, IO.

one: Tomcat memory optimization, tell the JVM at startup I want a big chunk of memory (tuning memory is the most direct way)

Catalina.bat under Windows

Linux under the catalina.sh such as:

java_opts= '-xms256m-xmx512m '

-xms<size> JVM Initialization Heap Size

The maximum actual parameter size of the-xmx<size> JVM Heap is based on server configuration or project specific settings.

two: Tomcat thread optimization in server.xml such as:

<connector port= "protocol=" http/1.1 "maxthreads=", "minsparethreads=" maxsparethreads= "" AcceptCount " = "connectiontimeout=" "
20000"  />

maxthreads= "X" indicates that the maximum number of simultaneous x connections is processed

minsparethreads= "x" initializes x connections

maxsparethreads= "x" means that if you can have up to x threads, if you exceed X, you will close the threads that are not needed

Acceptcount= "X" when the number of simultaneous connections reaches MaxThreads, it can also be queued and the queue size is X. Do not process more than X

three: Tomcat io optimization

1: Synchronous blocking IO (JAVA BIO) synchronization and blocking, the server implementation mode for a connection to a thread (one connection thread to think all feel scary, threads are very valuable resources), of course, can be improved through the thread pool mechanism.

2:java NIO: Also divided into synchronous non-blocking io, asynchronous blocking IO is the biggest difference from bio. One request thread. You can reuse the same thread to handle multiple connection (multiplexing).

3:, asynchronous non-blocking io (Java NIO2 is also called AIO) the main difference between NIO is the underlying difference between the operating system. You can do a metaphor: compared to express, NIO is the net after shopping to find out whether the express delivery has arrived (may be many times), and then take their own express , AIO is the courier door-to-door (do not pay attention to express progress).

The bio method is suitable for a small number of connections and a fixed architecture, this approach to the server resource requirements are relatively high, concurrent limited to applications, JDK1.4 before the only choice, but the program intuitive simple and easy to understand.

NiO method suitable for the number of connections and relatively short (light operation) of the architecture, such as chat server, concurrency limited to applications, programming is more complex, JDK1.4 began to support.

AIO mode for the number of connections and relatively long (heavy operation) of the architecture, such as photo albums Server, fully invoke the OS to participate in concurrent operations, programming more complex, JDK7 began to support.

In the Server.xml

<connector port= "protocol=" "Org.apache.coyote.http11.Http11NioProtocol" 
    connectiontimeout= "20000 
    " uriencoding= "UTF-8" 
    usebodyencodingforuri= "true" 
    enablelookups= "false" 
    redirectport= "8443"/>

Enable IO switching to Tomcat.

four: Big kill Apr

Apr solves asynchronous IO problems from the operating system level, dramatically improving performance. (http://apr.apache.org/).

APR (Apache portable Runtime) is a highly portable library, which is Apache HTTP Server 2. The core of X. Better integrate with other local web technologies, and overall make Java more efficient as a high-performance Web server platform rather than as a simple background container.

In a product environment, especially when using Tomcat directly as a Web server, you should use Tomcat Native to improve its performance. If you do not match the APR, basically 300 threads will soon be full, then the request will have to wait. But with the APR, the number of concurrent threads drops significantly, from 300 to dozens of, and new requests come in without a blockage.

In the local area network environment test, even if is 400 concurrent, also is the instantaneous processing/transmission completes, but in the real Internet environment, the page processing time only accounts for 0.1% not to, Most of the time is used for page transfers. If you don't use an APR, a thread can only handle one user at a time, which is bound to cause congestion. So it is very necessary to use APR in the production environment.

Install the Apache Tomcat Native Library, which supports APR (http://tomcat.apache.org/native-doc/) on its own based on Apr. Specific installation methods can refer to other blogs and articles. Troubleshoot code issues tomcat optimization to this level, can meet most of the performance requirements.




Tomcat has many aspects, I introduce the optimization method from the memory, concurrency, cache four aspects.
* * one. Tomcat Memory optimization

**

Tomcat memory optimizations are optimized primarily for Tomcat startup parameters, and we can set the java_opts parameters in Tomcat's startup script catalina.sh.
Java_opts parameter Description
-server enable the server edition of JDK;
-XMS Java Virtual Machine Initialization of the minimum memory;
-XMX the maximum memory that Java virtual machines can use;
-xx:permsize Memory permanently reserved area
-xx:maxpermsize memory maximum permanently reserved area
Server Parameter Configuration

Now the company server memory can be added to the maximum 2G, so you can take the following configuration:

java_opts= '-xms1024m-xmx2048m-xx:permsize=256m-xx:maxnewsize=256m-xx:maxpermsize=256m '

After the configuration is complete, you can restart Tomcat and see if the configuration is valid by using the following command:
First look at the Tomcat process number:

sudo lsof-i:9027

We can see that the Tomcat process number is 12222.
To see if the configuration is valid:

sudo jmap–heap 12222

We can see that parameters such as maxheapsize are in effect.

* * two. Tomcat Concurrency optimization

**

1.Tomcat Connection Related parameters

In the Tomcat configuration file Server.xml

<connector port= "9027" protocol= "http/1.1" maxhttpheadersize= "8192"
minprocessors=
" maxprocessors= "1000"
acceptcount= "1000" redirectport= "8443"
disableuploadtimeout= "true"/>
   
   
    
    1
    
    2
    
    3
    
    4
    
    5
    
    , 6 7 8 1 2 3 4 5 6 7 8
   
   

2. Adjust connector connector concurrent processing capability

1> parameter Description

MaxThreads the maximum number of threads requested by the customer
Number of socket threads created when Minsparethreads Tomcat is initialized
Maximum number of idle socket threads for the Maxsparethreads Tomcat connector
Enablelookups if set to True, support domain name resolution, IP address can be resolved to host name
Redirectport forward customer requests to SSL based Redirectport ports where security channels are needed
Acceptaccount listens for the maximum number of port queues and the client request is rejected (not less than maxsparethreads) after full
ConnectionTimeout Connection Timeout
Minimum number of processing threads when Minprocessors server is created
Maxprocessors Server simultaneous maximum number of threads
Uriencoding URL Unified Encoding

Examples of configurations in 2>tomcat

<connector port= "9027" protocol= "http/1.1" maxhttpheadersize= "8192"
maxthreads=
"1000"  minsparethreads= "maxsparethreads=" "
1000" minprocessors= "maxprocessors="
1000
"  Enablelookups= "false"
uriencoding= "Utf-8"
acceptcount= "
1000" redirectport= "8443" Disableuploadtimeout= "true"/>
   
   
    
    1
    
    2
    
    3
    
    4
    
    5
    
    6 7 8 9
   
   
   
   
    
    1
    
    2
    
    3
    
    4
    
    5
    
    6 7 8 9/12
    
    13
   
   

3.Tomcat Cache Optimization

1> parameter Description

C Ompression turn on the compression function
Compressionminsize enables compression of the output content size, which defaults to 2KB
Compressablemimetype compression type
ConnectionTimeout defines when a client connection timeout is established. If-1, indicates no limit on when client connections are established

Examples of configurations in 2>tomcat

<connector port= "9027" protocol= "http/1.1" maxhttpheadersize= "8192" maxthreads= "1000" minSpareThreads= "M" axsparethreads= "1000" minprocessors= "maxprocessors=" 1000 "enablelookups=" false "compression=" on "Compressio Nminsize= "2048" compressablemimetype= "Text/html,text/xml,text/javascript,text/css,text/plain" connectionTimeout=
   
   
    
    "20000" uriencoding= "Utf-8" acceptcount= "1000" redirectport= "8443" disableuploadtimeout= "true"/>
    
    1 2 3 4 5 6 7 8 9 10 11
    
    12 13 14 15 16 17 1 2 3
    
    4 5 6 7 8 9 10 11 12 13 14 15 16 17

4. Reference configuration

1> old Configuration

The reference network to the server has done the following configuration, to share the following:

<connector port= "9027"
protocol=
Related Article

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.