Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the optimization methods of Tomcat

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "what are the Tomcat optimization methods?". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Tomcat Connector Protocol Optimization

There are three ways of Tomcat connector: bio, nio and apr. The performance of the three methods is very different. The performance of apr is the best and that of bio is the worst. The Connector used by Tomcat 7 is the Apr protocol enabled by default, but the system needs to install the Apr library, otherwise it will use bio mode.

How to configure nio

Go to the server.xml of tomcat and find connector. Just change the protocol property in it.

How to configure apr

Configuration of apr requires installation of dependencies

Yum-y install openssl-devel yum-y install apr-devel

After installation, go to the official website of tomcat to download native components. Native can be seen as an intermediate part of the interaction between tomcat and apr. The download address is:

Http://tomcat.apache.org/download-native.cgi download the latest version 1.2.10 here, extract it and install it.

Tar-xvzf tomcat-native-1.2.10-src.tar.gz cd tomcat-native-1.2.10-src/native/. / configure

At this point, apr is successfully installed and you enter server.xml. Change the protocol to modify the default protocol= "HTTP/1.1" to protocol= "

Org.apache.coyote.http11.Http11AprProtocol ".

Optimization of Tomcat profile

The configuration file side is our main tomcat optimization area. We place common optimizations directly in the configuration file.

ConnectionTimeout= "30000": network connection timeout, unit: millisecond, set to 0 means never timeout, this setting has hidden dangers. It can usually be set to 30000 milliseconds, which can be modified appropriately according to the actual situation of the test.

EnableLookups= "false": whether to reverse the domain name to return the hostname of the remote host. The value is true or false. If set to false, the IP address is returned directly. In order to improve the processing power, it should be set to false.

DisableUploadTimeout= "false": whether to use the timeout mechanism when uploading.

ConnectionUploadTimeout= "150000": upload timeout. After all, file upload may take more time. This can be adjusted according to your own business needs, so that Servlet has a longer time to complete its execution. It will only take effect if it is used in conjunction with the previous parameter.

AcceptCount= "300": specifies the maximum queue length that can be passed in connection requests when all available threads for processing requests are used. Requests beyond this number will not be processed.

KeepAliveTimeout= "120000": maximum persistent connection hold time (milliseconds), indicating how long Tomcat will keep the connection before the next request comes. The default is to use connectionTimeout time, and-1 is an unlimited timeout.

MaxKeepAliveRequests= "1": indicates the maximum number of requests supported by the connection before the server is shut down. Connections that exceed this number of requests will also be closed. 1 means disabled,-1 means there is no limit to the number, and the default is 100. Generally, it is set between 100 and 200.

Compression= "on": whether to compress the response data with GZIP. Off: forbids compression; on: allows compression (text will be compressed), force: means compression is carried out in all cases. The default value is off. After compressing the data, you can effectively reduce the page size. Generally, you can reduce the size of the page by about 1x3, saving bandwidth.

CompressionMinSize= "2048": indicates the minimum value of the compressed response. The message will be compressed only if the response message size is greater than this value. If compression is enabled, the default value is 2048.

CompressableMimeType= "text/html,text/xml,text/javascript,text/css,text/plain,image/gif,image/jpg,image/png": compression type that specifies which types of files are compressed.

Maximum number of supported requests. Connections that exceed this number of requests will also be closed. 1 means disabled,-1 means there is no limit to the number, and the default is 100. Generally, it is set between 100 and 200. Compression= "on" is an unlimited timeout. Maxkeepaliverequests= "1" time,-1 = "connectiontimeout=" 30000 "for how long to keep the connection. The default is the maximum persistent connection retention time (in milliseconds), which means that before the next request comes, tomcat=" specifies the maximum queue length for incoming connection requests when all the threads that can be used to process requests are used. Requests beyond this number will not be processed. Keepalivetimeout= "120000" upload timeout. After all, file upload may take more time. This can be adjusted according to your own business needs, so that servlet has a longer time to complete its execution. It needs to be used in conjunction with the previous parameter to take effect. Whether to use the timeout mechanism when uploading acceptcount= "300s". Connectionuploadtimeout= "150000" false. Disableuploadtimeout= "false" false, if set to false, returns the ip address directly. In order to improve processing power, it should be set to = "" or = "" whether to reverse check the domain name to return the hostname of the remote host. The value is: true= "" milliseconds. According to the actual situation of the detection, you can modify enablelookups= "false" 3000 = "" to indicate that you will never time out. This setting has hidden dangers. Usually set to = "0 =" network connection timeout (in milliseconds), set to = "" uriencoding= "UTF-8" protocol= "HTTP/1.1" port= "8080" executor= "tomcatThreadPool" >

Optimization of jvm aspect of Tomcat

Find catalina.sh and add the following settings at the beginning of the file: JAVA_OPTS='-Xms256m-Xmx512m'

-Xms: indicates the size of the Java initialization heap, and-Xms is set to the same value as-Xmx to prevent JVM from repeatedly re-applying for memory, resulting in performance ups and downs. The default value is 1 MinHeapFreeRatio 64 of physical memory. By default (the MinHeapFreeRatio parameter can be adjusted), when the free heap memory is less than 40%, JVM will increase the maximum limit of the heap to-Xmx.

-Xmx: represents the maximum Java heap size. When the memory required by the application exceeds the maximum value of the heap, the virtual machine will prompt for a memory overflow and cause the application service to crash, so it is generally recommended that the maximum value of the heap be set to 80% of the maximum available memory. How to know that my JVM can use the maximum value, use the java-Xmx512M-version command to test, and then gradually increase the value of 512.If the execution is normal, the specified memory size is available, otherwise an error message will be printed. The default value is 1max 4 of physical memory. By default (the MinHeapFreeRatio parameter can be adjusted) when the free heap memory is greater than 70%, JVM will reduce the minimum limit of the heap to-Xms.

-Xss: represents the stack size of each Java thread. After JDK 5.0, the stack size of each thread is 1m, compared with 256K before. Adjust according to the memory required by the application thread. Under the same physical memory, reducing this value can generate more threads, but the operating system still has a limit on the number of threads in a process, which cannot be generated indefinitely. The experience value is about 3000-5000. Generally, for small applications, if the stack is not very deep, 128k should be enough. For large applications, 256k or 512K is recommended. Generally, it is not easy to set more than 1m, otherwise out ofmemory is easy to occur. This option has a great impact on performance and requires rigorous testing.

-XX:NewSize: sets the memory size of the new generation.

-XX:MaxNewSize: sets the maximum Cenozoic memory size

-XX:PermSize: sets persistent memory size

-XX:MaxPermSize: set the maximum persistent generation memory size. Permanent generation does not belong to heap memory. Heap memory only contains the new generation and the old generation.

-XX:+AggressiveOpts: function as its name (aggressive), enable this parameter, every time the JDK version is upgraded, your JVM will use the latest optimization technology (if any).

-XX:+UseBiasedLocking: enable an optimized thread lock. We know that in our appserver, each http request is a thread, some requests are short and some are long, there will be queuing of requests, and even thread blocking will occur. This optimized thread lock enables you to automatically optimize thread processing in your appserver.

-XX:+UseConcMarkSweepGC: set the older generation to concurrent collection, that is, CMS gc, which is only available in subsequent versions of jdk1.5. It uses gc estimation trigger and heap occupancy trigger. We know that frequent GC will cause the ups and downs of JVM, thus affecting the efficiency of the system, so after using CMSGC, when the number of GC increases, the response time of each GC is very short. For example, after using CMSGC, after the observation of jprofiler, GC is triggered a lot, while each GC takes only a few milliseconds.

-XX:+UseParNewGC: multi-thread parallel recycling is used for the new generation, so it can be collected quickly. Pay attention to the latest version of JVM. When you use-XX:+UseConcMarkSweepGC,-XX:UseParNewGC will be enabled automatically. Therefore, if the younger generation of parallel GC does not want to be turned on, you can turn it off by setting-XX:-UseParNewGC.

-XX:MaxTenuringThreshold: set the maximum age of garbage. If set to 0, the new generation of objects will enter the old age without going through the Survivor area. For more applications in the old era (applications that require a lot of resident memory), efficiency can be improved. If you set this value to a large value, the new generation object will be replicated many times in the Survivor area, which can increase the survival time of the object in the new generation, increase the probability of being recycled in the new generation, and reduce the frequency of Full GC, which can improve the service stability to some extent. This parameter is only valid in serial GC. The setting of this value is an ideal value based on the local jprofiler monitoring, and cannot be copied as a whole.

-XX:+

UseCMSCompactAtFullCollection: in the case of using concurrent gc, prevent memoryfragmention, defragment live object and reduce memory fragments.

-XX:+UseFastAccessorMethods: use the get,set method to transfer the code to cost, and quickly optimize the original type.

-Djava.awt.headless=true: we usually use this parameter at the end. The function of this full parameter is like this. Sometimes we use some chart tools in our J2EE project, such as: jfreechart, to output streams such as GIF/JPG on web pages. In winodws environment, our app server generally does not encounter any problems when exporting graphics. However, in the linux/unix environment, you will often encounter an exception that causes the picture to display well in the winodws development environment, but not in linux/unix, so add this parameter to avoid this situation.

-Xmn: the memory size of the new generation. Note: the size here is (eden+ 2 survivor space). It is different from the New gen shown in jmap-heap. The whole heap size = Cenozoic generation size + old generation size + permanent generation size. Under the condition of keeping the reactor size unchanged, the size of the old generation will be reduced after increasing the Cenozoic generation. This value has a great impact on system performance, and Sun officially recommends that it be configured as 3Universe 8 of the entire heap.

-XX:NewRatio: the ratio of the younger generation (including Eden and two Survivor regions) to the older generation (excluding the persistent generation).-XX:NewRatio=4 indicates that the ratio of the younger generation to the older generation is 1:4, and when the younger generation accounts for 5pm of the entire stack and Xmn is set, this parameter does not need to be set.

The size ratio of-XX:SurvivorRatio:Eden to Survivor is set to 8, which means that the ratio of two Survivor regions (there are two Survivor regions of the same size by default in the younger generation of JVM heap memory) to one Eden zone is 2:8, that is, one Survivor zone accounts for 1max 10 of the whole young generation size.

-XX:+UseSerialGC: sets the serial collector.

-XX:+UseParallelGC: set to a parallel collector. This configuration is valid only for the younger generation. That is, the younger generation uses parallel collection, while the older generation still uses serial collection.

-XX:+UseParallelOldGC: configure the old generation garbage collection method for parallel collection, and JDK6.0 starts to support parallel collection for the elderly generation.

-XX:OldSize: sets the old memory size allocated by JVM startup, similar to the initial memory size of the new generation-XX:NewSize.

Optimization of character set configuration in Tomcat

The language coding of Tomcat is very slow to configure and has to be set up many times, otherwise Chinese is likely to be garbled. For example, the Chinese character "Zhong" is encoded with UTF-8 to get the 3-byte value% E4%B8%AD, and then the three bytes are submitted to the Tomcat container by GET or POST. If you don't tell Tomcat that my parameters are encoded in UTF-8, then Tomcat thinks you are encoded in ISO-8859-1. And ISO8859-1 (compatible with the standard character set US-ASCII in URI) is compatible with ASCII single-byte encoding and uses all the space within a single byte, so Tomcat thinks that you passed the 3 characters encoded with the ISO-8859-1 character set, and then it uses ISO-8859-1 to decode.

It is not difficult to use "- D =" to set system properties:

-Djavax.servlet.request.encoding=UTF-8

-Djavax.servlet.response.encoding=UTF-8

-Dfile.encoding=UTF-8

-Duser.country=CN

-Duser.language=zh

This is the end of the content of "what are the Tomcat optimization methods". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 248

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report