Tomcat Production server Performance optimization

Source: Internet
Author: User

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
Lesus
Translated over 1 years ago

5 Person Top

Top translation of good Oh!

Other translation versions (1)

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 (Catalina.bat under Windows or catalina.sh under Linux) can solve this problem. This workaround is implemented by increasing the JVM's stack memory. That is, the JVM usually does not call the garbage collector, so the server can focus more on processing the Web request and require it to be completed 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 .

MtrS
Translated over 1 years ago

2 Person top

Top translation of good Oh!

Other translation versions (1)

Step Two – Resolve JRE memory leak

Another major cause of poor performance is a memory leak, 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 the Tomcat project Folder/conf/server.xml. Next, we'll look at how to adjust the Connection property "MaxThreads".

garfielt
translated 1 years ago

2 Human top

 

top   Good translation!

other translations (1)

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.

?
12345 <connector port= "8080" address= "localhost" maxthreads= maxhttpheadersize= "8192" emptysessionpath= "true" protocol= "http/1.1" enablelookups= "false" redirectport= "8181" acceptcount= "+" connectiontimeout= "20000" disableuploadtimeout= "true" />
garfielt
translated 1 years ago

1 people top

 

top   Good translation!

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,

?
12 org.apache.tomcat.util.threads.threadpool logfull severe:all Threads ( Code class= "CSS value" >250 ) are currently busy, Waiting. Increase MaxThreads ( 250 ) or check the servlet status

If 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 if the database connection is not released, the process will not Other requests will be processed.

garfielt
translated 1 years ago

1 Human top

 

top   Good translation!

Note:   If the number of requests exceeds 750, This will not mean setting the MaxThreads property value to "750", which unexpectedly best uses 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.  

Based on my experience, the exact values can be set by testing the application in various environments. Next, let's look at how to compress the MIME type.  
garfielt
translated 1 years ago

1 people top

 

top   Good translation!

other translations (1)

4th Step-Compact

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

?
1234 <connector port= "8080" protocol= "http/1.1" connectiontimeout= "20000" redirectport= "8181" compression= "$" compressablemimetype= "Text/html,text/xml,text/plain,application/octet-stream" />
In the previous configuration, compression occurs when the size of the file 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.
Lesus
Translated over 1 years ago

1 Person top

Top translation of good Oh!

Other translation versions (1)

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.

Lesus
Translated over 1 years ago

1 Person top

Top translation of good Oh!

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 – Other options

These options are:

    • Open the browser's cache so that reading static content stored in the WebApps folder will be faster, greatly boosting overall performance.
    • The tomcat server should restart automatically whenever it is powered on.
    • In general, HTTPS requests are slower than HTTP requests. If you want better security, even slower we still have to choose HTTPS.

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 if you have a different view on how to improve the performance of your Tomcat server, please don't forget to leave a valuable comment. I wish you a happy programming today!

Optimizing Tomcat Parameters

Here, as an example of TOMCAT7 parameter configuration, the Conf/server.xml file needs to be modified, mainly to optimize the connection configuration and close the client DNS query.

  1. <Connector port="8080"
  2. protocol="Org.apache.coyote.http11.Http11NioProtocol"
  3. connectiontimeout="20000"
  4. redirectport="8443"
  5. maxthreads="
  6. minsparethreads="
  7. acceptcount="
  8. disableuploadtimeout="true"
  9. enablelookups="false"
  10. uriencoding="UTF-8" />
5. Use Apr Library

The bio model used by Tomcat by default has a significant degradation in performance under hundreds of concurrency. Tomcat comes with a model of NIO, and you can also invoke the APR library to achieve OS level control.

The NIO model is built-in and is easy to invoke, requiring only the protocol in the above configuration file to be modified to Org.apache.coyote.http11.Http11NioProtocol, and the restart will take effect. The above configuration I have changed, the default is http/1.1.

Apr requires the installation of third-party libraries, which can significantly improve performance at high concurrency. Specific installation methods can refer to http://www.cnblogs.com/huangjingzhou/articles/2097241.html. Reboot will take effect when the installation is complete. If you use the default protocal is APR, but it is better to change the protocol to Org.apache.coyote.http11.Http11AprProtocol, will be more explicit .

In the official Find a table detailing the differences in these three ways:

  1. Java Blocking Connector java Nio Blocking Connector apr/native Connector
  2. BIO NIO APR
  3. Classname Ajpprotocol Ajpnioprotocol Ajpaprprotocol
  4. Tomcat Version 3.x onwards 7.x onwards 5.5.x onwards
  5. Support Polling NO Yes Yes
  6. Polling Size N/a maxconnections maxconnections
  7. Read Request Headers Blocking Sim Blocking Blocking
  8. Read Request Body Blocking Sim Blocking Blocking
  9. Write Response Blocking Sim Blocking Blocking
  10. Wait for next Request Blocking Non Blocking Non Blocking
  11. Max Connections maxconnections maxconnections maxconnections
6. Optimize your network

Joel has also made it clear that optimizing NIC drivers can improve performance, which is especially important when working in a clustered environment. Since we have a Linux server, optimizing kernel parameters is also a very important task. Give a reference to the optimization parameters:

  1. 1. Modify the/etc/sysctl.cnf file and append the following at the end:
  2. Net.core.netdev_max_backlog = 32768
  3. Net.core.somaxconn = 32768
  4. Net.core.wmem_default = 8388608
  5. Net.core.rmem_default = 8388608
  6. Net.core.rmem_max = 16777216
  7. Net.core.wmem_max = 16777216
  8. Net.ipv4.ip_local_port_range = 1024x768 65000
  9. Net.ipv4.route.gc_timeout =
  10. Net.ipv4.tcp_fin_timeout =
  11. Net.ipv4.tcp_keepalive_time =
  12. Net.ipv4.tcp_timestamps = 0
  13. Net.ipv4.tcp_synack_retries = 2
  14. Net.ipv4.tcp_syn_retries = 2
  15. Net.ipv4.tcp_tw_recycle = 1
  16. Net.ipv4.tcp_tw_reuse = 1
  17. Net.ipv4.tcp_mem = 94500000 915000000 927000000
  18. Net.ipv4.tcp_max_orphans = 3276800
  19. Net.ipv4.tcp_max_syn_backlog = 65536
  20. 2. Save exit, execute sysctl-p effective
7. Let the test speak

The most taboo is to optimize the system is not testing, sometimes inappropriate optimization will make performance lower. All of the above optimization methods have to be tested locally after the performance test and then constantly adjust the parameters, so that the ultimate optimization results can be achieved.

Added test results for bio, Nio, Apr modes:

For these modes, I used the AB command to simulate 1000 concurrent test 10000 words, the test results are quite unexpected, in order to confirm the results, I have repeatedly tested more than 10 times, and on two servers have been tested again. The results showed that the difference between bio and NiO was very weak, no wonder it was bio by default. However, with APR, the speed of connection establishment will be 50%~100%. Calling the operating system layer directly is very fast, it is highly recommended Apr way!

Tomcat Production server Performance optimization

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.