Reprint: Optimization of Tomcat performance

Source: Internet
Author: User
Tags name server lookup xms

Considering this scenario, you have developed an application that has excellent layout design, the latest features, and other outstanding features. But in terms of performance this is lacking, regardless of how the application will be rejected by customers. Customers always expect their applications to have better performance. If you use a Tomcat server in your product, this article will give you several ways to improve the performance of your Tomcat server. Thank ITWorld article for providing resources for this article. After meditation I've learned that the latest Tomcat offers better performance and stability than earlier versions. So always use the latest version of Tomcat. Now this article uses the following steps to improve the performance of the Tomcat server.

    1. Increase JVM heap memory size
    2. Repairing JRE Memory leaks
    3. Thread pool Settings
    4. Compression
    5. Database Performance Tuning
    6. Tomcat Local Library
    7. Other options

First step – Increase JVM stack memory increase JVM heap memories

If you have used Tomcat, simply say "memory overflow" .  Typically, this problem occurs in the actual production environment. This problem arises because Tomcat uses less memory for the process, by configuring the Tomcat configuration file (Windows   under Catalina.bat or Linux catalina.sh) can solve this problem. This The solution is implemented by increasing the JVM's stack memory. That is, the JVM usually does not call the garbage collector, so the server can pay more attention to the processing of the Web request and request it to complete as soon as possible. To change the file (catalina.sh) located in "\tomcat Server folder\bin\catalina.sh", below, give the configuration information for this file,

1234 JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8-server -Xms1024m -Xmx1024m-XX:NewSize=512m -XX:MaxNewSize=512m -XX:PermSize=512m-XX:MaxPermSize=512m -XX:+DisableExplicitGC"

-xms– specifying the stack memory initialized
-xmx– specifying the maximum stack memory
These configuration changes will not be valid until you restart your Tomcat server. The following describes how to handle JRE memory leaks .

Step Two – Troubleshoot JRE memory leaks

Another major cause of poor performance is memory leaks, as I said before: Always use the latest Tomcat server for better performance and scalability. Now, this sentence becomes true. This error can be resolved if we use the latest Tomcat version 6.0.26 and above, because it contains a listener to handle the memory leaks of the JRE and PermGen. The listener used is,

?
1 <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener"/>

You can find the configuration of this listener in the Server.xml file,server.xml location in "Tomcat Project Folder/conf/server.xml". Next, we'll look at how to adjust the Connection property "MaxThreads".

Step three – Thread pool settings

The thread pool specifies the number of Web request payloads, so This section should be handled with care to get better performance. You can complete the setup by adjusting the connector properties "MaxThreads". the value of the MaxThreads should be based on the size of the traffic, if the value is too low, there will not be enough threads to handle all requests, the request will go to the waiting state, and only if one of the processing threads is released, and if the setting is too large, Tomcat will take more time to start. So it depends on us setting a correct value for MaxThreads.

<connector port="8080"address="localhost"MaxThreads=" -"Maxhttpheadersize="8192"Emptysessionpath="true"Protocol="http/1.1"enablelookups="false"redirectport="8181"Acceptcount=" -"ConnectionTimeout="20000"disableuploadtimeout="true"/>

In the above 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 for this property is "200". Any additional concurrent requests will receive a "Deny connection" error until another processing request process is released. The error looks like this

org.apache.tomcat.util.threads.ThreadPool logFull SEVERE: All threads ( 250 ) are currently busy, waiting. Increase maxThreads ( 250 ) or check the servlet statusIf the application prompts the above error, it is important to check whether the above error is caused by a single request that takes too long, and sometimes the process will not process other requests if the database connection is not released. Note:If the number of requests exceeds "750", this will not mean that the MaxThreads property value is set to "750", it is unexpectedly best to use multiple instances of the Tomcat cluster. That is, if there is a "1000" request, two Tomcat instances are set to "Maxthreads= 500", and maxthreads=1000 is not set in the case of a single Tomcat instance.

4th Step-Compression

Tomcat has an option to set the compression in the Server.xml configuration file. Compression can be done in connector like the following settings,

 <connector port= 8080   " Protocol="  http/1.1   " connectiontimeout  ="  20000   redirectport  = " 8181  "  Compression= " 500  "   = text/html,text /xml,text/plain,application/octet-stream  /> 

In the previous configuration, compression occurs when the file size is greater than or equal to 500bytes. If the file reaches the size but is not compressed, the setting property compression= "on". Otherwise, the Tomcat default setting is "off". Next we'll look at how to tune the database.

Fifth Step-database Performance tuning

Tomcat performance is degraded while waiting for database queries to be executed. Most applications today use relational databases that may contain named queries. If that is the case, Tomcat will load the named query by default at startup, which may improve performance. Another important thing is to make sure that all database connections are shut down correctly. It is also important to set the correct values for the database connection pool. The value I refer to is the maximum idle number (maxidle) of the resource feature, the maximum number of connections (maxactive), and the maximum value of the connection wait time (maxwait) attribute. Because of configuration dependencies and application requirements, I cannot specify the correct values in this article. You can find the correct value by calling the database performance test.

6th Step –tomcat Native Library

Tomcat's native library is based on the Apache Portable runtime (Apache portable runtime abbreviation APR), providing programmers with superior scalability and performance, helping to fuse native server technology to deliver optimal performance in product operations. For friends who want to know the installation instructions, refer to Tomcat Native library– (APR) installation.

7th Step – Disable DNS queries

When the Web application wants to log the client's information, it also logs the client's IP address or converts it to an IP address through the name server lookup machine name.

DNS queries need to occupy the network and include the process of obtaining the corresponding IP from many remote servers or servers that do not work, which can take a certain amount of time.

Modify the connector element in the Server.xml file, modify the property enablelookups parameter value: enablelookups= "false"

If true, a DNS query can be made by calling Request.getremotehost () to obtain the actual hostname of the remote client and, if False, to return its IP address instead of a DNS query

Reprint: Optimization of Tomcat performance

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.