Tomcat production server performance optimization

Source: Internet
Author: User
Tags browser cache

Http://www.oschina.net/translate/tomcat-performance-tuning? From = 20130727

 

Introduction

Consider this scenario. You have developed an application with excellent Layout Design, latest features, and other outstanding features. However, there is a lack of performance, and no matter what the application is, it will be rejected by the customer. Customers always expect their applications to have better performance. If you use the Tomcat server in the product, this article will give you several ways to improve the performance of the Tomcat server. Thanks to itworld article for providing resources for this article. After meditation, I have learned that Tomcat provides better performance and stability compared with the latest versions. Therefore, we have been using the latest tomcat version. The following steps are used to improve the performance of the Tomcat server.
1. Increase the JVM heap memory size.
2. Fixed JRE Memory leakage
3. Thread Pool settings
4. Compression
5. Database Performance Tuning
6. Local Tomcat Library
7. Other options

Step 1-improve JVM stack memory increase JVM heap memory

If you have used tomcat, it is simply "memory overflow ". generally, this problem occurs in the actual production environment. the cause of this problem is that Tomcat uses less memory for the process by configuring the tomcat configuration file (Catalina in windows. BAT or Catalina in Linux. SH) can solve this problem. this solution is implemented by adding the JVM stack memory. that is to say, the JVM usually does not call the Garbage Collector, so the server can pay more attention to processing Web requests and require them to be completed as soon as possible. The file to be modified (Catalina. Sh) is located in "\ Tomcat server Folder \ bin \ Catalina. Sh". The configuration information of this file is shown below,

Java_opts = "-djava. AWT. Headless = true-dfile. Encoding = UTF-8
-Server-xms1024m-xmx1024m
-XX: newsize = 512 M-XX: maxnewsize = 512 M-XX: permsize = 512 m
-XX: maxpermsize = 512 M-XX: + disableexplicitgc"

-XMS-specifies the stack memory for initialization.
-Xmx-specify the maximum stack memory
After you restart your Tomcat server, these configuration changes will be valid. The following describes how to handle JRE Memory leakage.

Step 2-solve JRE Memory leakage

Another major cause of poor performance is memory leakage, as I said before: always use the latest Tomcat server for better performance and scalability. Now, this sentence becomes true. If we use the latest tomcat version 6.0.26 or later, we can solve this error because it contains a listener to handle Memory leakage of JRE and permgen. The listener used is,

<Listener classname = "org. Apache. Catalina. Core. jrememoryleakpreventionlistener"/>
You can find the Listener Configuration in the server. xml file. The server. XML is located in "Tomcat project folder/CONF/server. xml ". Next, we will look at how to adjust the connection attribute "maxthreads ".

Step 3-thread pool settings

The thread pool specifies the number of Web Request loads. Therefore, be careful when dealing with this part for better performance. You can adjust the connector attribute "maxthreads" to complete the settings. The value of maxthreads should be based on the traffic size. If the value is too low, there will be enough threads to process all requests, and the request will enter the waiting state, it is processed only after a processing thread is released. If the setting is too large, Tomcat startup takes more time. Therefore, it depends on setting a correct value for maxthreads.

<Connector Port = "8080" address = "localhost" maxthreads = "250" maxhttpheadersize = "8192" emptysessionpath = "true" protocol = "HTTP/1.1" enablelookups = "false" redirectport = "8181" acceptcount = "100" connectiontimeout = "20000" disableuploadtimeout = "true"/>

In the preceding configuration, the maxthreads value is set to "250", which specifies the maximum number of concurrent requests that can be processed by the server. If not specified, the default value of this attribute is "200 ". Any extra concurrent requests will receive a "Connection denied" error prompt until another request processing process is released. The error looks as follows,

Org. Apache. tomcat. util. threads. threadpool logfull severe: All threads (250) are currently busy, waiting. Increase maxthreads (250) or check the servlet status
If the application prompts the preceding error, check whether the error is caused by a single request taking too long. The cause is that sometimes if the database connection is not released, the process will not process other requests.

Note: If the number of requests exceeds "750", this does not mean to set the maxthreads attribute value to "750". It is better to use multiple instances of the "Tomcat cluster. That is to say, if there is a "1000" request, set "maxthreads = 500" for two Tomcat instances, rather than set maxthreads = 1000 for a single Tomcat instance.
 
Based on my experience, setting accurate values can be obtained by testing applications in various environments. Next, let's take a look at how to compress the MIME type.

Step 2-compression

Tomcat has an option to set compression in the server. xml configuration file. Compression can be completed in the following settings for connector,

<Connector Port = "8080" protocol = "HTTP/1.1"
Connectiontimeout = "20000"
Redirectport = "8181" compression = "500"
Compressablemimetype = "text/html, text/XML, text/plain, application/octet-stream"/>

In the previous configuration, the file is compressed only when the file size is greater than or equal to bytes. If the file size is reached but it is not compressed, set the attribute "compression =" on ". Otherwise, Tomcat is set to "off" by default ". Next we will look at how to optimize the database.

Step 5-database performance tuning

Tomcat performance degrades while waiting for database queries to be executed. Today, most applications use relational databases that may contain "name queries. If that is the case, Tomcat loads the name query by default at startup, which may improve performance. Another important thing is to ensure that all database connections are properly closed. It is also important to set the correct value for the database connection pool. The value refers to the value of the maxidle, maxactive, and maxwait attributes of the resource element. Because of the configuration dependency and application requirements, I cannot specify the correct value in this article. You can find the correct value by calling the database performance test.

Step 2-Tomcat native Library

Tomcat's native library is based on Apache Portable Runtime (APR), providing programmers with super scalability and performance, in product operation, it helps to integrate native server technologies to demonstrate the best performance. For installation instructions, see Tomcat native library-(APR) installation.

Step 2-other options

These options are:
Enable the browser cache, so that reading static content stored in the webapps folder will be faster, greatly promoting the overall performance.
The Tomcat server should be automatically restarted whenever it is started.
HTTPS requests are generally slower than HTTP requests. If you want better security, we still need to select HTTPS even if it is slower.

That's all. In this article, I have taught you some ways to improve the performance of Tomcat servers. If you think this article is useful or you have other opinions on improving Tomcat server performance, do not forget to leave valuable comments. Wish you a pleasant programming today!

 

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.