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

How to quickly optimize the performance of Tomcat JVM parameters

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to quickly optimize Tomcat JVM parameter performance", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "how to quickly optimize Tomcat JVM parameter performance"!

Tomcat Performance Tuning

Locate the conf directory in the Tomcat root directory and modify the contents of the server.xml file. For this part of the tuning, what I know is nothing more than setting the maximum number of concurrent Tomcat server and the number of threads created during Tomcat initialization, and of course there are other performance tuning settings. The following figure is some parameter values I set according to the performance of my machine. Let me explain it to you in detail:

URIEncoding="UTF-8": Sets Tomcat's character set. This configuration we generally do not set, because we will deal with the conversion of garbled characters in specific projects, directly modifying Tomcat's character set is too rigid.

maxThreads="300": Sets the maximum number of concurrent Tomcat. Tomcat is configured by default for a maximum of 150 requests, which means it can support 150 concurrent requests simultaneously. However, in practice, the maximum number of concurrency has a lot to do with hardware performance and CPU count. Better hardware and higher processors will make Tomcat support more concurrency. If in actual development, when an application has more than 250 concurrency, the cluster of application servers will be considered.

minSpareThreads="50": Sets the number of threads created during Tomcat initialization. The default value is 25.

4. acceptCount="250": When the number of simultaneous connections reaches the value set by maxThreads parameter, you can also receive the number of queued connections. If it exceeds this number, you will directly return to reject the connection. Specifies the number of requests that can be placed in the processing queue when any available threads for processing requests are used, beyond which requests will not be processed. The default value is 100. In practice, if you want to increase Tomcat concurrency, you should increase both acceptCount and maxThreads.

enableLookups="false": Whether to enable domain name reverse lookup, generally set to false to improve processing power, its value is also true, generally rarely used.

6. maxKeepAliveRequests="1": nginx is dynamically transferred to tomcat. nginx cannot keep alive, while tomcat has keepalive enabled by default and will wait for keepalive timeout. If it is not set by default, it will use connectionTimeout. So you have to set tomcat timeout and turn off tomcat keepalive. Otherwise, a lot of tomcat socket timewaits will be generated.

maxKeepAliveRequests="1" can prevent tomcat from generating a large number of TIME_WAIT connections, thus avoiding tomcat's fake death to a certain extent.

JVM performance tuning

Tomcat itself is still running on the JVM, and by adjusting JVM parameters we can make Tomcat have better performance. There are two main aspects of JVM tuning: memory tuning and garbage collection policy tuning.

1. Memory tuning

Find the bin directory under the Tomcat root directory and set the JAVA_OPTS variable in the catalina.sh file, because the later startup parameters will treat JAVA_OPTS as the startup parameter of JVM. Besides Java virtual machine memory structure is a bit complex, I believe many people are very abstract in understanding, it is mainly divided into heap, stack, method area and garbage collection system and other parts, the following is the memory structure diagram I picked from the Internet:

1. -Xmx512m: Sets the maximum available memory size of the heap of the Java virtual machine, in megabytes (m), the whole heap size = young generation size + old generation size + persistent generation size. The size of the permanent generation is generally 64m. The different distribution of the stack will have some influence on the system. As far as possible, the object is reserved in the new generation, and the number of GC in the old generation is reduced (usually the old generation is slower to recover).

In practice, the initial value and maximum value of the heap are usually set equal, which can reduce the number of garbage collections and space expansion when the program runs, thus improving program performance.

- Xms512m: Sets the initial memory size of the Java virtual machine heap, in megabytes (m), which can be set to the same value as-Xmx to avoid JVM reallocation of memory after each garbage collection.

3. -Xmn 170m: Set the memory size of the younger generation in megabytes (m). This value has a great impact on system performance. Sun officially recommends 3/8 of the whole heap. Generally, increasing the memory of the younger generation will also reduce the size of the older generation.

-Xss128k: Sets the stack size for each thread. After JDK 5.0, the stack size of each thread is 1M, and the previous stack size of each thread is 256K. Adjust the memory size required by the threads of the application.

Decreasing this value generates more threads for the same amount of physical memory. However, 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.

-XX:NewRatio=4: Set the ratio of young generation (including Eden and two Survivor zones) to old generation (excluding persistent generation). Set to 4, the ratio of young to old generations is 1:4, and young generations account for 1/5 of the stack.

6. -XX:SurvivorRatio=4: Set the size ratio of Eden region to Survivor region in young generation. Set to 4, the ratio of two Survivor zones to one Eden zone is 2:4, and one Survivor zone accounts for 1/6 of the entire young generation.

7, -XX:MaxPermSize=16m: Set the persistent generation size to 16m, as mentioned above, the persistent generation is generally fixed memory size of 64m.

8. -XX:MaxTenuringThreshold=0: Set the maximum age of garbage.

If set to 0, the young generation object does not pass through the Survivor area and directly enters the old generation. For applications with more older generations, efficiency can be improved.

If you set this value to a large value, young generation objects will replicate multiple times in the Survivor section, increasing the survival time of objects in younger generations and increasing the generalizations that are recycled in younger generations.

Second, garbage collection strategy optimization

Find the bin directory under the Tomcat root directory, and set the JAVA_OPTS variable in the catalina.sh file. We all know that Java virtual machines have default garbage collection mechanisms, but the efficiency of different garbage collection mechanisms is different, which is why we often adjust the garbage collection policy of Java virtual machines accordingly. Here is also a garbage collection policy configured by some of my requirements:

At this point, I believe everyone has a deeper understanding of "how to quickly optimize Tomcat JVM parameter performance," so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

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: 0

*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

Development

Wechat

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

12
Report