Tomcat (JVM) Performance Tuning

Source: Internet
Author: User

Tomcat Architecture

 

Tomcat and JVM version Optimization
Tomcat runs on a Java-based virtual machine. Sun's JVM dynamic library has two versions: client and server, which are optimized for desktop and server applications respectively. The Client Version loading speed is fast, server version loading speed is slow but it runs fast.
Enter Java-version on the command line to view the version currently configured by JVM. If you want to modify the jvm的, you can modify the JVM. dll called by java.exe, which is determined by JVM. cfg. Edit % java_home %/JRE/lib/i386/JVM. in the cfg file, the first line is-client (default is the Client Version), and the-server known of the second line is placed in the first line, as shown below:
-Server known
-Client known
-Hotspot aliased_to-Client
-Classic warn
-Native Error
-Green Error
Restart Tomcat and enter Java-version in the command line. The following information is displayed:
Java version "1.6.0 _ 13"
Java (TM) se Runtime Environment (build 1.6.0 _ 13-b03)
Java hotspot (TM) server VM (build 11.3-B02, mixed mode)
It indicates that tomcat has been changed to the JVM of the server version. You can compare and test the performance of the JVM and server versions of the Client Version to determine which version of JVM is used. In addition, you can also consider replacing the JVM of other vendors, such as Oracle's jrockit.

 

Tomcat and Memory leakage
By default, Tomcat can use 128 MB of memory (different versions). In larger application projects, this memory is insufficient and needs to be increased, otherwise, the "out of memery" issue may easily occur.
In Windows, modify the configuration file {tomcat_home}/bin/Catalina. bat. in Linux/Unix, modify the configuration file {tomcat_home}/bin/Catalina. SH and add the following settings:
Java_opts = '-XMS [initial memory size]-xmx [maximum memory available ]'
You need to increase the value of these two parameters based on the available memory of the server.
In addition, for Sun's JVM, the adjustment of the maxpermsize parameter is also important. The permsize determines the size of the object (class, method) to be retained. If you encounter an error message "outofmemoryerror: permgen", you should consider adjusting the size of this parameter. Sun JVM retains 64 MB as the permsize by default.
For other performance-related adjustment parameters of Sun's hotspot JVM, refer:
Http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html#PerformanceTuning

 

Maxthreads-monitoring and adjusting the number of connection threads
You can modify the configuration file server. XML in the conf folder of the tomcat installation directory to adjust the maximum number of Tomcat connection threads. Maxthreads is the maximum number of concurrent threads. If the concurrent Request volume exceeds this value, Tomcat will not add more threads, and concurrent requests will enter the queue. Increasing the value of maxthreads can increase the concurrent processing capability of Tomcat. However, setting a high value of maxthreads will also affect the performance, occupy too many system resources, and even cause Tomcat to crash.
The number of threads can be monitored on the Tomcat Manager status page, as shown in Figure 7.8:

Figure 7.8 Number of Tomcat threads monitored
The following are some experiments on Tomcat 7 to illustrate the impact of maxthreads on Tomcat server performance:
Tomcat heap memory is set to 256 MB. Use LoadRunner to start 50 virtual users to test the jpetstore homepage (http: // 192.168.1.101: 8080/jpetstore/shop/index.shtml ), the following test results are displayed:
(1) The default configuration is maxthreads = 200, and the average transaction response time is 0.390.
(2), maxthreads = 5, average Transaction Response Time: 0.355
(3), maxthreads = 50, average Transaction Response Time: 0.369
(4), maxthreads = 3, average Transaction Response Time: 0.559
As you can see, when maxthreads is set for an hour, Tomcat's request processing speed decreases. If strict concurrency is set in the test script (the lr_rendezvous function is inserted), the following result is displayed:
(1), maxthreads = 3, average Transaction Response Time: 2.609
(2), maxthreads = 50, average Transaction Response Time: 0.597
As you can see, in the case of high concurrency, if the maxthreads value of Tomcat is set relatively small, it will seriously affect the performance. Generally, you need to set a value greater than the maximum number of concurrent requests at the same time.
Note: Generally, the maximum number of connections allowed by Web servers is limited by the kernel parameter settings of the operating system. Generally, Windows has about 2000 connections and Linux has about 1000 connections. In centos, run the following command to view the maximum number of socket connections:
Ulimit-
Among them, open files are the maximum number of files that can be opened. The default value of centos is 1024.
You can set this value by modifying the VI/etc/security/limits. conf file. For example, to 32768, you can add the following values after the file:
* Soft nofile 32768
* Hard nofile 32768
Save and restart the system.
To verify the impact of Linux system parameters on socket connections, we can conduct a comparative test and modify/etc/security/limits. conf:
* Soft nofile 200
* Hard nofile 200
Start 200 VU concurrency in LoadRunner (set the set point). In the Controller's connection and connection per second graph, the number of connections is less (compared to the default 1024 ):
(1) set the parameter to 200
Average Transaction Response Time: 19.664
Connections: 177.376
Connections per second-New Connection: 28.082
Connections per second-connection shutdown: 28.082
(2) set the parameter to 1024 (default)
Average Transaction Response Time: 2.712
Connections: 315.136
Connections per second-New Connection: 114.000
Connections per second-connection shutdown: 114.000
When the establishment of a socket connection is restricted, Tomcat's concurrent processing capability is also significantly affected, and the average transaction response time increases sharply.

 

 

Connectiontimeout-connection timeout settings
Connectiontimeout is the wait time (in milliseconds) for connector to accept connections to the submitted URI. The default value is 60000 (60 seconds ).
This setting is in $ tomcat/CONF/server. xml:
<! -- Define a non-ssl http/1.1 Connector on port 8080 -->
<Connector Port = "8080" maxhttpheadersize = "8192"
Maxthreads = "150" minsparethreads = "25" maxsparethreads = "75"
Enablelookups = "false" redirectport = "8443" acceptcount = "100"
Connectiontimeout = "2000" disableuploadtimeout = "true"/>
For a detailed explanation and configuration of this item, you can refer to the Tomcat help documentation:
Http://tomcat.apache.org/tomcat-5.5-doc/config/http.html
If ontimeout is set to a relatively small value, a 502 error may occur when a large number of users access the website.
Connectiontimeout does not refer to the entire connection processing time, but to the waiting time of the URI from which the connection starts to process requests. You can use Telnet to perform a test:
Use telnet to connect to Tomcat directly. If no input is made, the socket will be disconnected soon. Enter the complete GET request package to obtain the output (server response package ). If continuous input is not kept, the connection will be disconnected soon (if the connectiontimeout time is set to a small value). If the input is kept continuously, the connection will be kept.
$ Telnet local host 8080

GET/index. jsp HTTP/1.1
Accept-language: ZH-CN
Connection: keep-alive
HOST: 192.168.0.11
Content-Length: 36

 

Acceptcount-Maximum number of queues
Acceptcount indicates how many new connection requests are allowed to wait in the queue when all threads are used to process requests. When the queue is full, any new request will be rejected. The default value is 100.
The configuration example is as follows:
<Connector Port = "8080" protocol = "HTTP/1.1"
Connectiontimeout = "20000"
Redirectport = "8443"
Maxthreads = "800" acceptcount = "1000"/>
Maxthreads is the maximum number of threads started by Tomcat, that is, the number of tasks simultaneously processed. The default value is 200. acceptcount is the number of requests queued when the number of threads started by Tomcat reaches the maximum, the default value is 100. If the two values work together, the following three situations may occur:
Scenario 1: accept a request. At this time, the number of threads started by tomcat has not reached the number set by maxthreads. Tomcat starts a thread to process the request.
Case 2: accept a request. At this time, the number of threads started by tomcat has reached the number set by maxthreads. Tomcat will put the request in the waiting queue and wait for the idle thread to be available.
Case 3: accept a request. At this time, the number of threads started by tomcat has reached the number set by maxthreads, and the number of requests in the waiting queue has reached the number set by acceptcount, at this time, Tomcat will directly reject this request and return the connection refused error.

 

Tomcat native Library (APR) Configuration
The so-called Apache Tomcat native library is actually called Apr, all called Apache Portable Runtime and tomcat. You can access it through the following address:
Http://tomcat.apache.org/tomcat-5.5-doc/apr.html
Apache Tomcat native library is a set of localized socket, thread, and Io components developed by Apache to improve Tomcat performance. That is to say, Apache Tomcat has advanced Io functions and calls functions at the operating system level, as well as local process processing, these can make Tomcat more like a web server (Like Apache), rather than just being used to interpret JSP, that is to say, to improve the throughput performance of individual Tomcat as a server.
If you do not configure Tomcat native library, the following message is displayed when you start Tomcat:
2011-7-3 23:59:16 org. Apache. Catalina. Core. aprlifecyclelistener init
Information: The APR based Apache Tomcat native library which allows optimal running C
E In production environments was not found on the java. Library. Path: e: \ Program
Files \ Java \ jdk1.6.0 _ 13 \ bin;.; E: \ windows \ sun \ Java \ bin; E: \ windows \ system32 ;...
In Linux, run the following command to view the preceding prompt in the Tomcat log output file:
Head logs/Catalina. Out
The following describes how to install and deploy APR:
1. Install APR in Windows
APR for Windows is a file named: tcnative-1.dll. You can download the matching version of The tcnative-1.dll at the following URL:
Http://www.apache.org/dist/tomcat/tomcat-connectors/native/1.1.20/binaries/win32/
Put the downloaded tcnative-1.dll file in the/bin directory, start Tomcat and you can see that tomcat has been loaded and deployed APR:
Loaded APR based Apache Tomcat native library 1.1.20
2. Install APR in Linux
(1) install APR
Download apr-1.2.12.tar.gz to the http://apr.apache.org and install it with the following command:
Tar-xvf apr-1.2.12.tar.gz
CD apr-1.2.12
./Configure -- prefix =/tomcat/APR
Make
Make install
(2) install APR-UTIL
Download apr-util-1.2.12.tar.gz to the http://apr.apache.org and install it with the following command:
Tar-xvf apr-util-1.2.12.tar.gz
CD apr-util-1.2.12
./Configure -- prefix =/tomcat/APR -- With-Apr =/tomcat/APR
Make
Make install
(3) install Tomcat native Library
CD/usr/local/tomcat/bin
Tar zxvf tomcat-native.tar.gz
CD tomcat-native-1.1.10-src/JNI/native
./Configure -- prefix =/tomcat/APR -- With-Apr =/tomcat/APR-with-Java-home =/usr/JDK
Make
Make install
(4) EDIT tomcat/bin/Catalina. Sh
Set java_opts = "$ catalina_opts-djava. Library. Path = tomcat/APR/lib"
Before # ----- execute the requested command ---------------------------------------------
(5) add Environment Variables
# Vi/etc/profile
Add:
Export LD_LIBRARY_PATH =/usr/local/APR/lib
(6) After restarting tomcat, check the Tomcat output log. It can be seen that APR has been loaded successfully.
# Vim/usr/local/tomcat/logs/Catalina. Out

Information: loaded Apache Tomcat native library 1.1.10.
2008-7-8 10:20:27 org. Apache. Catalina. Core. aprlifecyclelistener init
Information: Apr capabilities: ipv6 [True], sendfile [True], accept filters [false], random [True].
10:20:27 org. Apache. Coyote. http11.http11aprprotocol init

 

Connector-connector configuration
Tomcat 5.5 and later versions support the following four types of ctor configurations:
(1) NiO
NIO uses the local Io provided by APR to improve Tomcat's concurrent processing capability. The configuration example is as follows:
<Connector Port = "8081" protocol = "org. Apache. Coyote. http11.http11nioprotocol" connectiontimeout = "20000" redirectport = "8443"/>
(2) HTTP
HTTP is the default Tomcat Connector, with stable performance but low efficiency. The configuration example is as follows:
<Connector Port = "8081" protocol = "HTTP/1.1" connectiontimeout = "20000"
Redirectport = "8443" type = "regxph" text = "yourobjectname"/>
(3) tomcatthreadpool
The tomcatthreadpool uses the thread pool technology. client requests can be processed by reusing threads in the thread pool, saving the thread overhead for reallocation. The configuration example is as follows:
<Connector executor = "tomcatthreadpool"
Port = "8081" protocol = "HTTP/1.1"
Connectiontimeout = "20000"
Redirectport = "8443" type = "regxph" text = "yourobjectname"/>
(4) tomcatthreadpool + NiO
Tomcatthreadpool can be combined with NiO to provide services, that is, it is added to the thread pool on the basis of NiO. The configuration example is as follows:
<Connector executor = "tomcatthreadpool"
Port = "8081" protocol = "org. Apache. Coyote. http11.http11nioprotocol"
Connectiontimeout = "20000"
Redirectport = "8443" type = "regxph" text = "yourobjectname"/>
Readers can talk about Tomcat Connector configuration in these four ways, and then compare and test to check the performance differences.
In general, the performance of the second configuration method (HTTP) is relatively stable, but it is also the worst, and this method is the default configuration of Tomcat; the first configuration method (NIO) but the overall efficiency is relatively high. The third configuration method (tomcatthreadpool) has a large performance fluctuation. The fourth configuration method (tomcatthreadpool + NiO) it is added to the thread pool on the basis of NiO. It may be that the program processing is more complicated, so the performance is not better than NiO.

 

 

 

MATERIALS:

Influence of JVM parameters on J2EE performance optimization: http://sinckyzhang.blog.sohu.com/149067215.html

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.