加载中

Introduction
Think about this situation, you had developed an application which contains excellent layout, latest features and all the other things which relishes the application. But if it lacks in performance means, No matter what surely the application will be rejected by the customers. Customers always expect their application should have a better performance. If you used Tomcat server for your production, then this article gives you some aspects to improve the performance of the Tomcat Server. Thanks for the resources provided in this ITWorld article. After pondering the net I had understand that the latest release of Tomcat gives the better performance and stability when compared to Old version. So always use the latest version of Tomcat. Now in this article, the following steps are used to improve the performance of the tomcat server,

  1. Increase JVM heap memory
  2. Resolve JRE memory leaks
  3. Thread pool setting
  4. Compression
  5. Database performance tuning
  6. Tomcat Native Library
  7. Other options

简介

考虑一下这种场景,你开发了一个应用,它有十分优秀的布局设计,最新的特性以及其它的优秀特点。但是在性能这方面欠缺,不管这个应用如何都会遭到客户拒绝。客户总是期望它们的应用应该有更好的性能。如果你在产品中使用了Tomcat服务器,那么这篇文章就会给你几方面来提升Tomcat服务器的性能。感谢ITWorld article给本文提供资源。经过沉思我已经知道了和早期版本相比最新的Tomcat提供更好的性能和稳定性。所以一直使用最新的Tomcat版本。现在本文使用下面几步来提高Tomcat服务器的性能。

  1. 增加JVM堆内存大小
  2. 修复JRE内存泄漏
  3. 线程池设置
  4. 压缩
  5. 数据库性能调优
  6. Tomcat本地库
  7. 其它选项

Step 1 – Increase JVM heap memory
If you had used tomcat, definitely you would have heard about “Permgen Space” error. Simply said “OutOfMemoryError”. Mostly this error will occur in the production environment. The reason for this error is tomcat has very small memory for the running process, this error can be resolved by doing some changes in the Tomcat configuration file named “catalina.bat(In windows)/catalina.sh(In Linux)”. The changes to be made is to increase the JVM heap memory. That is , the JVM does not invoke the garbage collector often, so the server can focus more in serving web requests and the requests are completed faster. The file(catalina.sh) to be changed is located at “\tomcat server folder\bin\catalina.sh” and the configuration to be made in this file is given below,

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 – Specifies the initial heap memory
-Xmx – Specifies the maximum heap memory
The changes will gets activated, Once you restart the Tomcat Server. Next we shall see how to handle the memory leak,

第一步  – 提高JVM栈内存Increase JVM heap memory

你使用过tomcat的话,简单的说就是“内存溢出”. 通常情况下,这种问题出现在实际的生产环境中.产生这种问题的原因是tomcat使用较少的内存给进程,通过配置TOmcat的配置文件(Windows 下的catalina.bat或Linux下的catalina.sh)可以解决这种问题.这种解决方法是通过增加JVM的栈内存实现的.也就是说,JVM通常不去调用垃圾回收器,所以服务器可以更多关注处理web请求,并要求尽快完成。要更改文件(catalina.sh) 位于"\tomcat server folder\bin\catalina.sh",下面,给出这个文件的配置信息,

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 – 指定初始化时化的栈内存
-Xmx – 指定最大栈内存
在重启你的Tomcat服务器之后,这些配置的更改才会有效。下面将介绍如何处理JRE内存泄漏.

Step 2 – Resolve JRE memory leaks
Another main reason for the performance lack is memory leak, as I said before always use the latest tomcat server to get better performance and scalability. Now the phrase becomes true. This error can be resolved if we use the latest tomcat server version 6.0.26 and above. Since it contains a listener to handle the JRE and permgen memory leak. The listener used here is,

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

You can find the above listener class configuration in the server.xml file which resides in “tomcat project folder/conf/server.xml”. Next we shall see how to tune the connector attribute “maxThreads”.

第二步 – 解决JRE内存泄露

性能表现不佳的另一个主要原因是内存泄漏,正如我之前说过:始终使用最新的tomcat服务器以获得更好的性能和可伸缩性。现在,这句话变成真的。如果我们使用最新的tomcat版本6.0.26及以上就可以解决这个错误,因为它包含了一个监听器来处理JRE和PermGen的内存泄漏。使用的监听器是

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

你可以在server.xml文件中找到这个监听器的配置,server.xml位置在“tomcat project folder/conf/server.xml”。接下来,我们将看看如何调整连接属性“maxThreads”。

Step 3 – Thread pool setting
Thread pool specifies the number of web request load that comes in, So this part should be handled carefully in order to get the better performance. This can be accomplished by tuning the connector attribute “maxThreads”. The value of the maxThreads should be based on the volume of the traffic. If the value is low, then there will not be enough threads to handle all of the requests, so it undergoes in to the wait state and comes back only when an another request thread gets freed. If we set maxThreads value too high means then the Tomcat startup time will take longer. So its up to us, Put a right value in the 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" />

第三步 – 线程池设置

线程池指定Web请求负载的数量,因此,为获得更好的性能这部分应小心处理。可以通过调整连接器属性“maxThreads”完成设置。maxThreads的值应该根据流量的大小,如果值过低,将有没有足够的线程来处理所有的请求,请求将进入等待状态,只有当一个的处理线程释放后才被处理;如果设置的太大,Tomcat的启动将花费更多时间。因此它取决于我们给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, maxThreads value is given as “250″. This specifies the maximum number of simultaneous requests that can be handled by the server. If not specified, the default value of this attribute “200″. Any further simultaneous requests will receive “connection refused” errors until the another request gets freed. The error looks like the below,

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 results in the above error, Always investigate whether the above error caused by a single request that takes too long. The reason is, Sometimes if the database connection is not released means, it will not allow to process additional request.

在上述配置中,maxThreads值设定为“250”,这指定可以由服务器处理的并发请求的最大数量。如果没有指定,这个属性的默认值为“200”。任何多出的并发请求将收到“拒绝连接”的错误提示,直到另一个处理请求进程被释放。错误看起来如下,

org.apache.tomcat.util.threads.ThreadPool logFull SEVERE: All threads (250) are
currently busy, waiting. Increase maxThreads (250) or check the servlet status

如果应用提示上述错误,务必检查上述错误是否是由于单个请求花费太长时间造成的,这个问题的原因是这样的,有时候如果数据库连接不释放的话,进程将不会处理其它请求。

Note: If the number of request exceeds “750″ means, Instead of setting the value “750″ in the maxThreads attribute, Its better to go for the “Tomcat Clustering” with multiple tomcat instances. That is, In the case of “1000″ request, set “maxThreads=500″ for the two instances of tomcat, Instead of a single Tomcat with maxThreads=1000.

According to my perspection, the exact value can be determined only by testing the application in various environment. Next we shall see how to compress the mime types.

注意: 如果请求的数量超过了“750”,这将不是意味着将maxThreads属性值设置为“750”,它意外着最好使用“Tomcat集群”的多个实例。也就是说,如果有“1000”请求,两个Tomcat实例设置“maxThreads= 500”,而不在单Tomcat实例的情况下设置maxThreads=1000。

根据我的经验,准确值的设定可以通过将应用在在各种环境中测试得出。接下来,我们来看看如何压缩的MIME类型。

Step 4 – Compression
Tomcat has an option to compress the mime-types by doing some configuration in the server.xml file. This compression should be done in the connector like the below,

<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 preceding configuration, the files will be compressed when the number of bytes is >= 500. If the files not to be compressed by the size means, Set the attribute compression=”on”. Otherwise the default the setting in Tomcat is “off”. Next we shall see, How to tune the database.

第4步- 压缩

Tomcat有一个通过在server.xml配置文件中设置压缩的选项。压缩可以在connector像如下设置中完成,

<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8181" compression="500"
compressableMimeType="text/html,text/xml,text/plain,application/octet-stream" />
在前面的配置中,当文件的大小大于等于500bytes时才会压缩。如果当文件达到了大小但是却没有被压缩,那么设置属性compression="on"。否则Tomcat默认设置是“off”。接下来我们将看看如何调优数据库。

Step 5- Database performance tuning
Tomcats performance gets lacked while waiting for the database queries to get executed. Nowadays most of the application uses Relational databases, which may contains “NamedQueries”. If so, by default the tomcat will load the named queries initially, this may increase the performance. Another important thing is, always ensure that all the database connections are closed properly. Also setting the correct value in the database connection pool is very essential. I mean the values in maxIdle, maxActive, and maxWait attributes of the Resource element. Since the configuration depends on the application requirements, I cannot specify the right values in this article. You can find the correct values by invoking the performance testing for the database.

第五步- 数据库性能调优

Tomcat性能在等待数据库查询被执行期间会降低。如今大多数应用程序都是使用可能包含“命名查询”的关系型数据库。如果是那样的话,Tomcat会在启动时默认加载命名查询,这个可能会提升性能。另一件重要事是确保所有数据库连接正确地关闭。给数据库连接池设置正确值也是十分重要的。我所说的值是指Resource要素的最大空闲数(maxIdle),最大连接数(maxActive),最大建立连接等待时间(maxWait)属性的值。因为配置依赖与应用要求,我也不能在本文指定正确的值。你可以通过调用数据库性能测试来找到正确的值。

Step 6 – Tomcat Native Library
Tomcat native library, “Apache Portable Runtime(APR)” provides superior scalability, performance and better integration with the native server technologies and gives optimal performance in the production environment. The installation steps is explained in the article Tomcat Native Library – (APR) Installation.

Step 7 – Other options
Some of the other options are,

  • Enable the web browser cache, so that the static content which resides in the webapps folder loads faster, By doing this the performance is greatly increased.
  • Tomcat server should be automatically restarted, whenever the machine starts.
  • Normally HTTPS request is slow when compared to the HTTP request. But what ever if you want some good security for the application means, we should prefer HTTPS instead of HTTP.

Thats all folks. In this article I have given some stuffs to improve the Tomcat server performance. If you find this article is useful to you or if you have any other options to improve the performance, dont forget to leave your valuable comments. Have a joyous code day.

第6步 – Tomcat原生库

Tomcat的原生库基于Apache可移植运行时(Apache Portable Runtime简称APR),给程序员提供了超强的扩展性和性能,在产品运作中帮助融合原生的服务器技术以展现最佳的性能。想知道安装说明的朋友请参考Tomcat Native Library – (APR) Installation

第7步 – 其他选项

这些选项是:

  • 开启浏览器的缓存,这样读取存放在webapps文件夹里的静态内容会更快,大大推动整体性能。
  • 每当开机时,Tomcat服务器应当自动地重启。
  • 一般情况下HTTPS请求会比HTTP请求慢。如果你想要更好的安全性,即使慢一点我们还是要选择HTTPS。

就这么多啦。在这篇文章里,我教给了大家一些提高Tomcat服务器性能的方法。如果你觉得这篇文章有用,或者你对提高Tomcat服务器性能有别的看法,请不要忘记留下宝贵的评论。祝你今天编程愉快!

返回顶部
顶部