preface

Tomcat server is an open source lightweight Web application server. It is widely used in small and medium-sized systems and small concurrency situations. It is the first choice for developing and debugging Servlet and JSP programs. As you are already familiar with Tomcat, this article introduces common Tomcat optimizations. So why optimize Tomcat? Because Tomcat default parameters are designed for development environments, not production environments, especially memory and thread configurations, they are low by default and can easily become performance bottlenecks. I believe that after reading this blog, there will be a certain harvest.

Tomcat connector protocol optimization

There are three modes of Tomcat connector: BIO, NIO, and APR. The performance of the three modes varies greatly, with APR being the best and BIO the worst. Tomcat 7 uses the Apr protocol by default, but requires the system to install the Apr library, otherwise the BIO mode will be used.

How to configure NIO

Go to server.xml for Tomcat and find connector. Change the protocol property.

Apr configuration requires installation dependencies

yum -y install openssl-devel

yum -y install apr-devel

After installation, go to tomcat’s official website to download native components. Native can be regarded as the intermediate link between Tomcat and APR. The download address is:

Tomcat.apache.org/download-na…

Download the latest version 1.2.10 here, unzip it and install it

The tar – XVZF tomcat native – 1.2.10 – SRC. Tar. Gz

CD tomcat native – 1.2.10 – SRC/native /

./configure

With apr installed successfully, enter server.xml. Change protocol Change the default protocol=”HTTP/1.1″ to protocol=”

Org. Apache. Coyote. Http11. Http11AprProtocol “.

Optimization of Tomcat configuration files

The configuration file side is where our main Tomcat optimizations are. We place common optimizations directly in the configuration file.

1. ConnectionTimeout =”30000″ : indicates that the network connection times out, in milliseconds. If the value is set to 0, it never times out. The value can be set to 30000 milliseconds. Change the value as required

EnableLookups =”false” : specifies whether to reverse search the domain name to return the host name of the remote host. The value can be true or false. If this parameter is set to false, the remote host directly returns the IP address.

DisableUploadTimeout =”false” : indicates whether to use the timeout mechanism during upload.

4. ConnectionUploadTimeout = “150000” : upload timeout, file upload might need to consume more time after all, the need to adjust according to your own business, in order to make the Servlet has a longer time to complete its execution, need to cooperate to use with a parameter on will only take effect.

5. AcceptCount =”300″ : Specifies the maximum queue length for incoming connection requests when all available threads are used. Requests exceeding this number will not be processed.

6. KeepAliveTimeout =”120000″ : indicates the maximum hold time (ms) for a long connection. It indicates how long Tomcat will hold the connection before the next request.

MaxKeepAliveRequests =”1″ : Indicates the maximum number of requests that the connection supported before the server was shut down. If the number of requests exceeds this threshold, connections will be closed. 1 indicates that the number of requests is disabled. -1 indicates that the number of requests is not limited.

8.compression=”on” : whether to perform GZIP compression on the response data, off: compression is forbidden; On: enables compression (text will be compressed). Force: enables compression in all cases. The default value is off. After data compression, the page size can be reduced by 1/3 to save bandwidth.

9.compressionMinSize=”2048″ : indicates the minimum value for compressed responses. Packets are compressed only when the size of response packets is larger than this value. If the compression function is enabled, the default value is 2048.

10. com pressableMimeType = “text/HTML, text/XML, text/javascript, text/CSS, text/plain, image/GIF image/JPG, image/PNG” : Compression type: specifies which types of files are compressed.

<Connector executor=”tomcatThreadPool”

Port = “8080” protocol = “HTTP / 1.1”

URIEncoding=”UTF-8″

connectionTimeout=”30000″

// The network connection times out, in milliseconds. If the value is set to 0, it will never time out. The value can be set to 30000 milliseconds. Change the value as required

enableLookups=”false”

The value can be true or false. If the value is set to false, the IP address is directly returned. To improve processing capability, set the value to false.

disableUploadTimeout=”false”

// Whether to use the timeout mechanism when uploading.

connectionUploadTimeout=”150000″

// Upload timeout, after all file uploads may take more time, this is adjusted according to your own business needs, so that the Servlet has a longer time to complete its execution, need to be used in conjunction with the previous parameter to effect.

acceptCount=”300″

// Specifies the maximum queue length of connection requests that can be passed in when all available threads for processing requests are used. Any more than this number will not be processed. The default is 100.

keepAliveTimeout=”120000″

// Maximum long connection hold time (ms), which indicates how long Tomcat will hold the connection until the next request comes. The default is connectionTimeout. -1 indicates an unlimited timeout.

maxKeepAliveRequests=”1″

// Indicates the maximum number of requests that this connection can support before the server is shut down. If the number of requests exceeds this threshold, connections will be closed. 1 indicates that the number of requests is disabled. -1 indicates that the number of requests is not limited.

compression=”on”

// Whether to GZIP the response data. Off: indicates that compression is disabled. On: enables compression (text will be compressed). Force: enables compression in all cases. The default value is off. After data compression, the page size can be reduced by 1/3 to save bandwidth.

compressionMinSize=”2048″

// Indicates the minimum value of compressed response. Packets are compressed only when the size of response packets is larger than this value. If the compression function 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, which specifies which types of files are compressed.

redirectPort=”8443″ />

JVM optimizations for Tomcat

JAVA_OPTS= ‘-xMS256m -XMx512m’

– Xms: -xms = -xmx; -xms = -xmx; -xms = -xmx; -xms = -xmx; -xms = -xmx The JVM increases the heap to the maximum limit of -xmx.

-Xmx: indicates the maximum size of the Java heap. If the memory required by an application exceeds the maximum size of the heap, the VM will be prompted to overflow the memory and the application service will crash. Therefore, you are advised to set the maximum size of the heap to 80% of the maximum available memory. How do I know that my JVM can use the maximum value? Use the Java -xmx512m -version command to test and gradually increase the value of 512. If it works, the specified memory size is available, otherwise an error message will be printed. By default (the MinHeapFreeRatio parameter can be adjusted) when the free heap is greater than 70%, the JVM reduces the heap to the minimum limit of -xms.

-xSS: indicates the stack size of each Java thread. After JDK 5.0, the stack size of each thread is 1M. Before JDK 5.0, the stack size of each thread is 256K. In the same physical memory, reducing the value can generate more threads. However, the operating system has a limit on the number of threads in a process, which cannot be generated indefinitely. The empirical value is about 3000 to 5000. For small applications, if the stack is not very deep, 128k is sufficient. For large applications, 256k or 512K is recommended. It is not easy to set the stack to more than 1M, otherwise out ofmemory may occur. This option has a significant impact on performance and requires rigorous testing.

-xx :NewSize: Sets the memory size of the new generation.

-xx :MaxNewSize: Sets the maximum memory size of the new generation

-xx :PermSize: Sets the persistent generation memory size

-xx :MaxPermSize: sets the maximum size of the persistent generation memory. The permanent generation does not belong to the heap memory, and the heap memory contains only the new generation and the old generation.

-xx :+AggressiveOpts: As its name suggests, enable this parameter, and your JVM uses the latest optimization technology (if any) whenever a JDK version is updated.

– XX: + UseBiasedLocking: With an optimized thread lock, we know that in our AppServer, each HTTP request is a thread, some requests are short, some requests are long, there will be queuing, and even thread blocking. This optimized thread lock will automatically optimize the allocation of thread processing within your AppServer.

-xx :+UseConcMarkSweepGC: sets concurrent collection for the old generation, that is, CMS GC. This feature is only available in later VERSIONS of JDK1.5. It uses GC estimation trigger and HEAP occupancy trigger. We know that frequent GC can cause a lot of ups and downs in the JVM, which can affect the efficiency of the system. Therefore, using CMSGC can increase the number of GC, but the response time of each GC is very short. For example, using CMSGC through Jprofiler observation, GC is triggered many times, and each GC takes only a few milliseconds.

-xx :+UseParNewGC: enables multi-threaded concurrent collection for new generations. This allows fast collection. Note that in recent JVM versions, -xx :+UseConcMarkSweepGC is automatically enabled. Therefore, if the younger generation’s parallel GC does not want to be enabled, it can be set

-xx: -useparnewGC to turn it off.

-xx :MaxTenuringThreshold: sets the maximum garbage age. If set to 0, the new generation object passes through the Survivor zone and enters the old age directly. For older applications (applications that require a large amount of resident memory), efficiency can be improved. If this value is set to a large value, the new generation object will be duplicated multiple times in the Survivor region, which increases the lifetime of the object in the new generation, increases the probability of being collected in the new generation, and reduces the frequency of Full GC, which can improve the service stability to some extent. This parameter is valid only for serial GC and is set to an ideal value based on local Jprofiler monitoring and cannot be copied.

– XX: + UseCMSCompactAtFullCollection: in the case of using concurrent gc, prevent memoryfragmention, to live object to sort, reduce the memory fragments.

-xx :+UseFastAccessorMethods: Use get, set methods to convert native code, primitive type fast optimization. -djava.awt. headless=true: This parameter is usually used last. This parameter is used for some purposes in our J2EE projects. Jfreechart is used to output GIF/JPG streams in web pages. Under the WinodWS environment, our app Server will not encounter any problems in the output of graphics. However, in Linux/Unix, there is often an exception that causes your image to display well in Windows development but not in Linux/Unix, so this parameter is used to avoid this.

-xmn: memory size of the new generation. Note that the size is (Eden + 2 survivor space). This is different from the New Gen shown in Jmap-heap. Total heap size = New generation size + old generation size + permanent generation size. Under the condition of keeping the heap size unchanged, the size of old generation will be reduced after the Cenozoic generation is increased. This value has a significant impact on system performance. Sun officially recommends setting it to 3/8 of the entire heap.

– XX: NewRatio option: -xx :NewRatio=4 indicates that the ratio of the young generation to the old generation is 1:4, and the young generation accounts for 1/5 of the whole stack. Under the condition that Xms=Xmx and Xmn is set, This parameter does not need to be set.

– XX: SurvivorRatio: If the size ratio of Eden zone to Survivor zone is set to 8, it means that the ratio of 2 Survivor zones (default 2 Survivor zones of equal size in JVM heap memory young generation) and 1 Eden zone is 2:8. That is, one Survivor zone is 1/10 of the size of the entire young generation.

-xx :+UseSerialGC: Sets the serial collector.

-xx :+UseParallelGC: Set to parallel collector. This configuration is valid only for the young generation. That is, the young generation uses parallel collection, while the old generation still uses serial collection.

-xx :+UseParallelOldGC: Set the garbage collection mode of the aged generation to parallel collection. In JDK6.0, parallel collection of the aged generation is supported.

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

Tomcat character set configuration optimization

The language code of Tomcat is very slow to configure and needs to be set several times. Otherwise, Chinese characters are likely to be garbled. If you don’t tell Tomcat that my parameters are encoded in UTF-8, Tomcat then assumes that you are using ISO-8859-1, and that ISO8859-1 is an ASCII compatible single byte encoding and uses all the space within that single byte. So Tomcat thinks you passed three characters encoded in the ISO-8859-1 character set, and it decodes them with ISO-8859-1.

-d < name >=< value > 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