Tomcat performance optimization, tomcat

Source: Internet
Author: User

Tomcat performance optimization, tomcat

I. JVM Optimization

1. Memory optimization.

2. Garbage collection policy optimization.

2. server ctor Optimization of Server. xml (connector is a container related to HTTP request processing. The initialization sequence of the three containers is server-> Service-> connector)

(1) Specify the NIO model to accept HTTP requests

(2) http ctor optimization, specifying the number of processing threads

(3) thread pool

(4) other general settings

3. Set the session expiration time

Iv. apr plug-in improves tomcat Performance

V. Clusters

Vi. Problem locating

I. JVM Optimization

Modify TOMCAT_HOME/bin/catalina. sh in linux and add

JAVA_OPTS="-XX:PermSize=64M -XX:MaxPermSize=128m -Xms512m -Xmx1024m -Duser.timezone=Asia/Shanghai"

Modify TOMCAT_HOME/bin/catalina. bat in windows and add

set JAVA_OPTS=-XX:PermSize=64M -XX:MaxPermSize=128m -Xms512m -Xmx1024m

 1. Memory Optimization 
In the memory mode, adjust the JAVA_OPTS variable in catalina. sh, because the following startup parameters process JAVA_OPTS as JVM startup parameters.

The specific settings are as follows:
JAVA_OPTS = "$ JAVA_OPTS-Xmx3550m-Xms3550m-Xss128k-XX: NewRatio = 4-XX: Export vorratio = 4"

The parameters are as follows:
-Xmx3550m: sets the maximum available JVM memory to 3550 MB. The Heap Size cannot exceed 80% of the available physical memory.
-Xms3550m: Set JVM to enable the memory to be 3550 MB. This value can be set to the same as-Xmx to avoid JVM re-allocating memory after each garbage collection.
-Xmn2g: Set the young generation to 2 GB. Total heap size = size of the young generation + size of the old generation + size of the persistent generation. The permanent generation usually has a fixed size of 64 m. Therefore, increasing the size of the young generation will reduce the size of the old generation. This value has a great impact on the system performance. Sun officially recommends 3/8 of the total heap configuration.
-Xss128k: Set the stack size of each thread. After JDK 256, the size of each thread stack is 1 MB, and the size of each previous thread stack is K. The memory size required for more application threads is adjusted. Reduce this value to generate more threads in the same physical memory. However, the operating system still has a limit on the number of threads in a process. It cannot be generated infinitely. The experience is between 3000 and ~ About 5000.
-XX: NewRatio = 4: Set the ratio of the young generation (including Eden and two region vor regions) to the old generation (excluding the permanent generation ). If this parameter is set to 4, the ratio of the young generation to the old generation is, and the young generation accounts for 1/5 of the entire stack.
-XX: Region vorratio = 4: Set the ratio of Eden to region vor in the young generation. If this parameter is set to 4, the ratio of two vor zones to One Eden zone is, and one vor zone accounts for 1/6 of the young generation.
-XX: MaxPermSize = 16 m: sets the persistent generation size to 16 m.
-XX: MaxTenuringThreshold = 0: sets the maximum age of spam. If it is set to 0, the young generation object directly enters the old generation without going through the VOR area. For applications with many older generations, the efficiency can be improved. If this value is set to a greater value, the young generation object will be copied multiple times in the same vor area, which can increase the survival time of the young generation object, added an overview of being recycled in the young generation.

2. Garbage collection policy optimization 
The garbage collection setting is also in catalina. sh to adjust the JAVA_OPTS variable.
The specific settings are as follows:
JAVA_OPTS = "$ JAVA_OPTS-Xmx3550m-Xms3550m-Xss128k-XX: + UseParallelGC-XX: MaxGCPauseMillis = 100"
The specific garbage collection policy and corresponding policy parameters are as follows:

Serial collector (main recycling methods before JDK1.5)
-XX: + UseSerialGC: sets the serial collector.
Parallel collector (throughput first)
Java-Xmx3550m-Xms3550m-Xmn2g-Xss128k-XX: + UseParallelGC-XX: MaxGCPauseMillis = 100

-XX: + UseParallelGC: select the garbage collector as the parallel collector. This configuration is only valid for young generations. In the preceding configuration, the young generation uses concurrent collection, while the old generation uses serial collection.
-XX: ParallelGCThreads = 20: Number of threads configured for the parallel collector, that is, the number of threads at the same time for garbage collection. It is best to set this value to be equal to the number of processors.
-XX: + UseParallelOldGC: configure the garbage collection mode for the old generation to parallel collection. JDK6.0 supports parallel collection of older generations
-XX: MaxGCPauseMillis = 100: sets the maximum time for garbage collection for each young generation. If this time cannot be met, JVM automatically adjusts the size of the young generation to meet this value.
-XX: + UseAdaptiveSizePolicy: After this option is set, the parallel collector automatically selects the ratio of the young generation zone and the corresponding region vor zone, to achieve the minimum corresponding time or collection frequency specified by the target system. We recommend that you enable parallel collectors all the time.

Concurrent collector (Response Time First)
Example: java-Xmx3550m-Xms3550m-Xmn2g-Xss128k-XX: + UseConcMarkSweepGC
-XX: + UseConcMarkSweepGC: set the age for concurrent collection. After this is configured in the test, the-XX: NewRatio = 4 configuration is invalid because the cause is unknown. Therefore, it is best to set the size of the young generation with-Xmn.
-XX: + UseParNewGC: sets the young generation for parallel collection. It can be used together with CMS collection. JDK5.0 or above, JVM will be set based on the system configuration, so you do not need to set this value.
-XX: CMSFullGCsBeforeCompaction: Because the concurrent collector does not compress and organize the memory space, "fragments" are generated after a period of operation, reducing the running efficiency. This value sets the number of GC operations to compress and organize the memory space.
-XX: + UseCMSCompactAtFullCollection: Open the compression for the old generation. Performance may be affected, but fragments can be eliminated.

3. Summary 
In the memory settings, we need to make a trade-off.
1) The larger the memory, the higher the processing efficiency, but the longer the time required for garbage collection, the processing efficiency during this period of time will inevitably be affected.
2) Xmx and Xms are recommended to be consistent in most network articles, saying that frequent recovery is avoided, which did not show obvious results during testing, the usage of memory is basically the result of the tooth shape, so this should be determined according to the actual situation.

 

Ii. Connection Optimization of Server. xml to improve Tomcat's concurrency

1. Apache and Tomcat are combined to use Apache for static pages, while Tomcat for dynamic pages, while reducing the connectionTimeout time to cope with situations where large concurrent threads cannot be recycled.
2. If the stress is too high, Server Load balancer can be implemented. a tomcat cannot handle so many thread loads at any time. Moreover, if the JVM is too large, the memory management costs will increase significantly. 2 GB memory, making 3-4 TOMCAT instances (512RAM * 4), more scientific and reasonable.
3. For database connection pools, C3P0 is recommended for many users, which can improve the concurrent performance of database access by several times. (Some blog posts said it is better to use the jdbc-pool that comes with tomcat. I have not tried it yet)
4. Tomcat clusters can maximize the performance of servers. You can deploy multiple Tomcat servers on servers with high configurations, or deploy Tomcat on multiple servers, apache and Tomcat are integrated in JK mode. After verification, the system responds to a large number of users. Apache + 3366cat cluster> Apache + 2Tomcat cluster> Apache integrated Tomcat> A single Tomcat cluster. In addition, when you deploy Apache + Multi-Tomcat clusters, if a Tomcat instance goes down, the system can continue to use it. Therefore, when the hardware system performance is superior enough, to maximize the performance of the software, you can add a Tomcat cluster.
5. Enable KeepAlive support
KeepAlive on, KeepAliveTimeout 15 MaxKeepAliveRequests 1000
Based on actual experience, the effect of improving system performance through Apache and Tomcat clusters is very obvious. This method can maximize the use of hardware resources, the processing of multiple Tomcat servers is used to share the pressure on a single Tomcat server.
The maximum number of connections allowed by the web server is also subject to the kernel parameter settings of the operating system. Generally, Windows has about 2000 connections, and Linux has about 1000 connections.

1. Specify to use the NIO model to accept HTTP requests 
Protocol = "org. apache. coyote. http11.Http11NioProtocol" specifies that the NIO model is used to accept HTTP requests. The default value is BlockingIO and is protocol = "HTTP/1.1"
AcceptorThreadCount = "2" indicates the number of threads received when the NIO model is used.

2,Number of processing threads 

<Connector port="80" protocol="HTTP/1.1" maxThreads="600" minSpareThreads="100" maxSpareThreads="500" acceptCount="700"connectionTimeout="20000" redirectPort="8443" />

MaxThreads = "600" // maximum number of threads
MinSpareThreads = "100" // Number of threads created during initialization
MaxSpareThreads = "500" // once the created thread exceeds this value, Tomcat closes the socket thread that is no longer needed.
AcceptCount = "700" // specifies the number of requests that can be placed in the processing queue when the number of threads that can be used to process requests is used. Requests that exceed this number will not be processed.

Here is the optimization of http ctor. If you use apache and tomcat for Cluster load balancing and the ajp protocol for apache and tomcat protocol forwarding, you also need to optimize ajp connector.

<Connector port="8009" protocol="AJP/1.3" maxThreads="600" minSpareThreads="100" maxSpareThreads="500" acceptCount="700"connectionTimeout="20000" redirectPort="8443" />

3. Thread Pool

Because tomcat has multiple ctor S, the configuration of tomcat threads supports multiple connector s to share one thread pool.

First. Open/conf/server. xml and add

<Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="500" minSpareThreads="20" maxIdleTime="60000" />

The maximum number of threads is 500 (generally enough on the server), the minimum number of Idle threads is 20, and the maximum idle time of the thread is 60 seconds.

Then, modify the <Connector...> node, add the executor attribute, and set executor to the thread pool Name:

<Connector executor="tomcatThreadPool" port="80" protocol="HTTP/1.1"  connectionTimeout="60000" keepAliveTimeout="15000" maxKeepAliveRequests="1"  redirectPort="443" />

One thread pool can be shared by multiple ctor s, so ajp connector can also be set to use the tomcatThreadPool thread pool.

 4. Other common settings 
MaxHttpHeaderSize = "8192" indicates the maximum length of http request header information. Generally 8 K.
URIEncoding = "UTF-8" specifies the URL encoding format for the Tomcat container. Do not omit URIEncoding = "GBK" to ensure that the Chinese parameters are correct when the page url is passed.
DisableUploadTimeout = "true" whether to use the timeout Mechanism During upload
EnableLookups = "false" -- whether to reverse query the domain name. The default value is true. Set to false to improve processing capability.
Compression = "on" enable the compression function. Compression will increase the Tomcat burden. It is best to use Nginx + Tomcat or Apache + Tomcat, and Compression should be done by Nginx/Apache.
CompressionMinSize = "10240" enables the size of compressed output content. The default value is 2 kb.
NoCompressionUserAgents = "gozilla, traviata" does not enable compression for the following browsers:
CompressableMimeType = "text/html, text/xml, text/javascript, text/css, text/plain" which resource types need to be compressed
5. Summary 
The introduction of Tomcat Nio and ThreadPool improves the processing complexity. Therefore, we need to verify the efficiency.

3. Set the session expiration time

In conf \ web. xml, specify the following parameters:

<Session-config> <session-timeout> 180 </session-timeout> </session-config> is measured in minutes.

 

Iv. Apr plug-in improves Tomcat Performance

Tomcat can use APR to provide super scalability and performance, and better integrate local server technology.

APR (Apache Portable Runtime) is a highly Portable library, which is the core of Apache HTTP Server 2.x. APR has many functions, including access to advanced IO functions (such as sendfile, epoll, and OpenSSL), OS-level functions (Random Number Generation, system status, and so on), and local process management (shared memory, NT pipeline and UNIX sockets ). These functions enable Tomcat as a common front-end WEB server to better integrate with other local web technologies. In general, Java is more efficient as a high-performance web server platform rather than simply as a backend container.

In the product environment, especially when Tomcat is directly used as a WEB server, Tomcat Native should be used to improve its performance.

The best way to test the benefits of APR for tomcat is to increase the number of Tomcat threads to over 300 on a slow network (simulating the Internet), and then simulate a large number of concurrent requests.
If it is not compatible with APR, basically 300 threads will soon be used up, and later requests will have to wait. However, with APR, the number of concurrent threads is significantly reduced, from 300 to dozens immediately, and new requests are not blocked.
In the LAN environment test, even if it is 400 concurrent threads, the processing/transmission is completed in an instant. However, in the real Internet environment, the page processing time is less than 0.1%, most of the time is used for page transmission. If APR is not used, a thread can only process one user at a time, which may cause blocking. Therefore, apr is necessary in the production environment.

(1) install APR tomcat-native apr-1.3.8.tar.gz installed in/usr/local/apr # tar zxvf apr-1.3.8.tar.gz # cd apr-1.3.8 #. /configure; make install apr-util-1.3.9.tar.gz installed in/usr/local/apr/lib # tar zxvf apr-util-1.3.9.tar.gz # cd apr-util-1.3.9 #. /configure -- with-apr =/usr/local/apr ---- with-java-home = JDK; make; make install # cd apache-tomcat-6.0.20/bin # tar zxvf tomcat-native.tar.gz # cd tomcat-native/jni/native #. /conf Igure -- with-apr =/usr/local/apr; make install (2) Set Tomcat integration APR to modify tomcat startup shell (startup. sh), add the startup parameter CATALINA_OPTS = "$ CATALINA_OPTS-Djava. library. path =/usr/local/apr/lib ". (3) check whether the installation is successful. If the following startup log is displayed, the installation is successful. 2007-4-26 15:34:32 org. apache. coyote. http11.Http11AprProtocol init
V. Cluster Solution

The processing performance of a single Tomcat is limited. When the concurrency is large, you need to deploy multiple sets for load balancing.

The key points of a cluster are as follows:
1. Introduce the load end 
Soft load can be carried out using nginx or apache, mainly using a Distribution Function
Refer:
Http://ajita.iteye.com/blog/1715312 (nginx load)
Http://ajita.iteye.com/blog/1717121 (apache load)

2. Shared session Processing 
The current processing methods are as follows:
1). Use the Session replication function of Tomcat itself
Reference http://ajita.iteye.com/blog/1715312 (Session replication configuration)
The solution is easy to configure. The disadvantage is that when the number of clusters is large, Session replication takes a long time and affects the response efficiency.
2). Use a third party to store shared sessions
Currently, memcached is used to manage shared sessions. memcached-sesson-manager is used to manage Tomcat sessions.
Reference http://ajita.iteye.com/blog/1716320 (managing Tomcat cluster sessions with MSM)
3). Use the sticky session Policy
For scenarios where the session requirement is not too strong (billing is not involved, and re-request is allowed if a failure occurs), the session of the same user can be handled by nginx or apache and handed over to the same Tomcat, this is the so-called session sticky policy. Currently, there are many applications.
Reference: http://ajita.iteye.com/blog/1848665 (tomcat session sticky)
Nginx does not include the session sticky module by default. You need to re-compile it (I don't know how to re-compile it in windows)
The advantage is that the processing efficiency is much higher, and the disadvantage is that it is not suitable for scenarios with strong session requirements.

3. One instance for each site. Start Multiple tomcat servers.

Do not use a Tomcat VM. Each site has one instance. That is, Start Multiple tomcat.

This is also a common mistake in php o & M. The PHP practice is to place multiple virtual hosts under a Web, rather than starting a web server on each host. Tomcat is a multi-thread, shared memory. The application crashes on any virtual host, which affects all applications. Although the overhead of multiple instances is large, Application Isolation and security are ensured.

4. Summary 
The preceding figure shows the key points for cluster implementation. You can use both 1 and 2 in combination. analyze the specific scenarios ~

Vi. Problem locating

The time-consuming problems of Tomcat processing are mainly caused by the concurrency at that time, the number of sessions, memory and memory collection. After the problem occurs, analyze it.

1. About the number of Tomcat sessions 
This can be viewed directly from the Tomcat web management interface.
Or, you can use a third-party tool Lambda Probe to view the details. It has a little more functionality than the built-in management functions of Tomcat.

2. Monitor Tomcat memory usage 
You can use the jconsole that comes with JDK to clearly view the memory usage, thread status, and total number of classes currently loaded.
JDK's built-in jvisualvm can download plug-ins (such as GC) to view more information. If you are analyzing local Tomcat, you can also perform memory sampling to check the usage of each class.

3. Print the loading and collection of classes. 
You can configure JVM startup parameters to print the information (to the screen (to the catalina. log by default) or file). The specific parameters are as follows:
-XX: + PrintGC: Output Format: [GC 118250 K-> 113543 K (130112 K), 0.0094143 secs] [Full GC 121376 K-> 10414 K (130112 K ), secs 0.0650971]
-XX: + PrintGCDetails: Output Format: [GC [DefNew: 8614 K-> 781 K (9088 K), 0.0123035 secs] 118250 K-> 113543 K (130112 K ), 0.0124633 secs] [GC [DefNew: 8614 K-> 8614 K (9088 K), 0.0000665 secs] [Tenured: 112761 K-> 10414 K (121024 K ), 0.0433488 secs] 121376 K-> 10414 K (130112 K), 0.0436268 secs]
-XX: + PrintGCTimeStamps-XX: + PrintGC: PrintGCTimeStamps can be used together with the above two. Output Format: 11.851: [GC 98328 K-> 93620 K (130112 K), 0.0082960 secs]
-XX: + PrintGCApplicationConcurrentTime: Specifies the execution time of the program that is not interrupted before each garbage collection. Can be used together with the above. Output Format: Application time: 0.5291524 seconds
-XX: + PrintGCApplicationStoppedTime: specifies the time when the program is suspended during garbage collection. Can be used together with the above. Output Format: Total time for which application threads were stopped: 0.0468229 seconds
-XX: PrintHeapAtGC: prints detailed stack information before and after GC.
-Xloggc: filename: used in combination with the above items to record relevant log information to the file for analysis

-Verbose: monitors the status of loaded classes.
-Verbose: gc displays information on the output device when memory is recycled from the VM.
-Verbose: jni outputs native method call information, which is generally used to diagnose jni call errors.

4. Add JMS Remote Monitoring 
For Tomcat deployed on other machines in the LAN, you can open the JMX monitoring port, other machines in the LAN can use this port to view some common parameters (but some complex functions are not supported). You can also configure them in the JVM startup parameters as follows:
-Dcom. sun. management. jmxremote. ssl = false-Dcom. sun. management. jmxremote. authenticate = false
-Djava. rmi. server. hostname = 192.168.71.38: Set the IP address of the jvm jms monitoring listener to prevent the error listening from being 127.0.0.1.
-Dcom. sun. management. jmxremote. port = 1090: Set the jvm jms Monitoring port
-Dcom. sun. management. jmxremote. ssl = false: Setting jvm jms monitoring is not applicable to SSL.
-Dcom. sun. management. jmxremote. authenticate = false: Set jvm jms monitoring without authentication.

5. Professional Analysis Tools 
Ibm isa, JProfiler, and so on. You can search online for specific monitoring and analysis methods.

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.