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 is the method of Tomcat performance tuning

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article focuses on "what is the method of Tomcat performance tuning", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what is the method of Tomcat performance tuning"?

All optimization based on JVM (memory)

2.1comparison of JVM between 32-bit operating system and 64-bit operating system

Most of our developers basically use 32-bit Windows systems, which leads to a serious problem: 32-bit windows systems are limited to memory. Let's take a look at a comparison table:

After the above problems are solved, we encounter a new problem, which is the memory limit of JVM in 32-bit system: you can't break through the memory of 2GB, even if you have 8GB-16GB memory on your machine under Win2003 Advanced Server, while your JAVA can only use 2GB memory.

In fact, I have always wanted to recommend that you use Linux or Mac operating system, and install 64-bit, because after all, we are used by developers, not for playing games, and Java comes from Unix and belongs to Unix (Linux is just a Unix running on PC).

So many developers run on win32-bit systems and even deploy Win32-bit systems in production environments, so you need to be skillful in optimizing your Tomcat. On 64-bit operating systems, neither system memory nor JVM is limited by 2GB.

The optimization of Tomcat is divided into two parts:

Optimization parameter in Tomcat startup command line, that is, JVM optimization

Optimization of the parameters of the Tomcat container (this piece is very similar to ApacheHttp Server)

The first thing in this section is to talk about the optimized parameters in the Tomcat startup command line.

Tomcat is the first to run above JVM, because its startup is actually just a java command line, so first we need to tune the startup command line of this JAVA.

It is important to note that:

The JVM optimization discussed here is based on the jdk1.6 version of Oracle Sun, which is not applicable to other JDK or lower versions of JDK.

2.2 Optimization of Tomcat startup line parameters

Tomcat startup parameters are located in the tomcat installation directory in directory, if you are the Linux operating system is the catalina.sh file, if you are the Windows operating system then you need to change the catalina.bat file. When you open the file, the header of the file is usually a pile of comment text wrapped in # #. Find the last paragraph of the note text, such as:

# $Id: catalina.sh 522797 2007-03-27 07 OS specific support fhanik $#-# OS specific support. $var _ must_ be set to either true or false.

Enter an enter and add the following parameters

Startup parameters of tomcat in Linux system

Export JAVA_OPTS= "- server-Xms1400M-Xmx1400M-Xss512k-XX:+AggressiveOpts-XX:+UseBiasedLocking-XX:PermSize=128M-XX:MaxPermSize=256M-XX:+DisableExplicitGC-XX:MaxTenuringThreshold=31-XX:+UseConcMarkSweepGC-XX:+UseParNewGC-XX:+CMSParallelRemarkEnabled-XX:+UseCMSCompactAtFullCollection-XX:LargePageSizeInBytes=128m-XX:+UseFastAccessorMethods-XX:+UseCMSInitiatingOccupancyOnly-Djava.awt.headless=true"

Startup parameters of tomcat in Windows system

Set JAVA_OPTS=-server-Xms1400M-Xmx1400M-Xss512k-XX:+AggressiveOpts-XX:+UseBiasedLocking-XX:PermSize=128M-XX:PermSize=128M-XX:MaxPermSize=256M-XX:MaxTenuringThreshold=31-XX:+UseConcMarkSweepGC-XX:+UseParNewGC-XX:+CMSParallelRemarkEnabled-XX:+UseCMSCompactAtFullCollection-XX:LargePageSizeInBytes=128m-XX:+UseFastAccessorMethods-XX:+UseCMSInitiatingOccupancyOnly-Djava.awt.headless=true

There are a lot of parameters above, some people may have written that there are so many parameters in a tomcat startup command. Of course, these parameters are only on my machine and may not be suitable for you, especially the value (value) after the parameters need to be set according to your own actual situation.

Parameter explanation:

-server

I don't care what your reason is, as long as your tomcat is running in a production environment, this parameter must be added to me.

Because tomcat runs in a mode called java-client by default, server means that your tomcat is running in real production mode, which means that when your tomcat runs in server mode, it will have: larger, higher concurrent processing power, faster and more powerful JVM garbage collection mechanism, and more load and throughput. More. no, no, no. There's more. no, no, no.

Y remember to me, otherwise this-server is not added, it will be spanking.

-Xms-Xmx

That is, JVM memory is set, the two values of Xms and Xmx are set to the same is the best practice, some people say that Xms is the minimum value, Xmx is the maximum value is not very good, this setting is also more user-friendly, scientific. human nature? science? You son of a bitch.

Think about a scenario like this:

With the concurrency of a system getting higher and higher, its memory usage is gradually rising, rising to the highest point can not rise, start to fall, you do not think that this decline is a good thing, by its ups and downs, when the memory fall back, it pays the price is CPU high-speed start to run for garbage collection, at this time serious even cause your system "jam" is that you are in good operation All of a sudden, the web page is like dying over there for a few seconds or even more than ten seconds, because JVM is garbage collection.

So we set these two to the same at the beginning, so that Tomcat makes full use of the efficiency of the system in order to maximize the parameters at startup, which is the same principle as the need to set the number of minpool size and maxpool size in jdbcconnection pool.

How do I know that my JVM can use the maximum value? A pat on the head? no way!

When setting this maximum memory XMX value, open a command line first and type the following command:

Look, it can display the version information of JDK normally, indicating that you can use this value. Isn't it said that 2GB memory can be used up to 32-bit systems? That is, 2048m, let's try it without precaution.

Is that okay? No! Let's not say 2048m, let's be smaller and try 1700m.

Hey, not even 1700m, let alone 2048m, 2048m is just a theoretical value, let's put it this way, I have several machines here, some machines-Xmx1800 is fine, some machines can only reach-Xmx1500m.

So be sure to test this before setting the-Xms and-XMX values, or add it directly to the tomcat startup command line and your tomcat will never get up again. If you want to fly, you will never be able to fly, and you will become a distemper cat.

-Xmn

Set the size of the younger generation to 512m. Whole heap size = young generation size + old generation size + lasting generation size. The permanent generation usually has a fixed size of 64m, so increasing the size of the younger generation will reduce the size of the old 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.

-Xss

Is to set the stack size for each thread. This depends on your program, depending on how much memory a thread needs, how many threads may be running at the same time, and so on. It is generally not easy to set more than 1m, otherwise out ofmemory is easy to appear.

-XX:+AggressiveOpts

Function such as its name (aggressive), enable this parameter, your JVM will use the latest optimization technology (if any) whenever the JDK version is upgraded.

-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 automatically optimizes thread processing within your appserver.

-XX:PermSize=128M-XX:MaxPermSize=256M

JVM uses-XX:PermSize to set the initial value of non-heap memory, which defaults to 1x64 of physical memory

When exporting files with a large amount of data, be sure to set these two values, otherwise there will be a memory overflow error.

The maximum non-heap memory size is set by XX:MaxPermSize, which defaults to 1x4 of physical memory.

So, if it's physical memory 4GB, then 1 / 64 is 64MB, which is the default value of PermSize, which is the initial memory size of the immortal age.

1/4 is 1024MB, which is the default size of MaxPermSize.

-XX:+DisableExplicitGC

The displayed call "System.gc ()" is not allowed in the program code. I have seen two excellent projects call System.gc () manually at the end of each DAO operation, and I think that doing so seems to solve their out ofmemory problems, at the price of a serious reduction in system response time, just like the principle I explained in Xms,Xmx. Calling GC in this way leads to the ups and downs of the JVM of the system, and the performance is not going anywhere.

-XX:+UseParNewGC

Multi-thread parallel recycling is used for the younger generation, so it can be collected quickly.

-XX:+UseConcMarkSweepGC

Known as CMS gc, this feature is only available in jdk1.5, a later version, and uses gc estimate 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 CMS GC, when the number of GC increases, the response time of each GC is very short. For example, after using CMS GC, after the observation of jprofiler, GC is triggered a lot, while each GC takes only a few milliseconds.

-XX:MaxTenuringThreshold

Set the maximum age of garbage. If set to 0, the younger generation will enter the older generation without going through the Survivor area. For more applications of the older generation, it can improve the efficiency. If you set this value to a large value, the younger generation object will be replicated multiple times in the Survivor area, which can increase the survival time of the younger generation and increase the probability of being recycled in the younger generation.

The setting of this value is based on the local jprofiler monitoring after an ideal value, can not be generalized to copy the original.

-XX:+CMSParallelRemarkEnabled

Minimize mark time when using UseParNewGC

-XX:+UseCMSCompactAtFullCollection

In the case of using concurrent gc, prevent memoryfragmention, and defragment the live object to reduce memory fragments.

-XX:LargePageSizeInBytes

Specify the paging page size for Java heap

-XX:+UseFastAccessorMethods

Code for transferring get,set method to cost

-XX:+UseCMSInitiatingOccupancyOnly

Indicates that concurrent collector starts the collection only after oldgeneration has used the initialization ratio

-XX:CMSInitiatingOccupancyFraction=70

CMSInitiatingOccupancyFraction, this parameter setting has great skill, basically satisfy (Xmx-Xmn) * (100-CMSInitiatingOccupancyFraction) / 100 > = Xmn will not appear promotion failed. In my application, Xmx is 6000 megabytes, so Xmx-Xmn is 5488 megabytes, that is, the older generation has 5488 megabytes. CMSInitiatingOccupancyFraction=90 said that next year, when the elder generation is full of 5488 megabytes, it will begin to perform concurrent garbage collection (CMS) for the older generation. At this time, the remaining 10% space is 5488 trillion 10% = 548 megabytes, so even if all the objects in the Xmn (that is, the younger generation is 512 trillion) are moved to the older generation, 548 megabytes of space is enough. So as long as the above formula is full, there will be no promotion failed during garbage collection.

So the setting of this parameter must be associated with Xmn.

-Djava.awt.headless=true

We usually use this parameter at the end, and 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 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.

The above configuration can basically achieve:

Faster system response time

The speed of JVM recovery increases without affecting the response rate of the system.

Maximizing JVM memory utilization

Minimize thread blocking

2.3 Optimization in Tomcat containers

Earlier, we optimized the commands when Tomcat started, increasing the number of available JVM of the system, garbage collection efficiency and thread blocking, and increasing the response efficiency of the system. There is another very important indicator that we did not optimize, that is, throughput.

Remember what we said in the third day of study, the system itself can handle 1000, you do not optimize and configure so that it can only handle 25 by default. So let's look at the optimizations in the Tomcat container next.

Open the tomcat installation directory confserver.xml file and navigate to this line:

What a freshman.

It doesn't matter. Explain it one by one.

URIEncoding= "UTF-8"

It makes it convenient for tomcat to parse the url of files with Chinese names, unlike apache with a mod_encoding and manual compilation.

MaxSpareThreads

MaxSpareThreads means that if there are more threads in the idle state than the set number, those threads are aborted, reducing the total number of threads in the pool.

MinSpareThreads

Minimum number of spare threads, the number of threads initialized when tomcat starts.

EnableLookups

This effect is the same as HostnameLookups in Apache, which is set to off.

ConnectionTimeout

ConnectionTimeout is the number of milliseconds of network connection timeout.

MaxThreads

MaxThreads Tomcat uses threads to process each request received. This value represents the maximum number of threads that Tomcat can create, that is, the maximum number of concurrency.

AcceptCount

AcceptCount means that when the number of threads reaches maxThreads, subsequent requests will be put into a waiting queue. The acceptCount is the size of the queue. If the queue is full, refuse connection it directly.

MaxProcessors and minProcessors

In Java, a thread is the path of a program when it runs, and it is a code segment that is independent of other control threads and can run independently in a program. They share the same address space. Multithreading helps programmers write efficient programs that maximize the utilization of CPU, keep idle time to a minimum, and accept more requests.

Usually the Windows is about 1000 and the Linux is about 2000.

UseURIValidationHack

Let's take a look at a piece of source code in tomcat:

Security if (connector.getUseURIValidationHack ()) {String uri = validate (request.getRequestURI ()); if (uri = = null) {res.setStatus; res.setMessage ("Invalid URI"); throw new IOException ("Invalid URI");} else {req.requestURI (). SetString (uri); / / Redoing the URI decoding req.decodedURI (). Duplicate (req.requestURI ()); req.getURLDecoder (). Convert (req.decodedURI (), true);}}

You can see that if you set useURIValidationHack to "false", you can reduce its unnecessary checks for some url and save overhead.

EnableLookups= "false"

To eliminate the performance impact of the DNS query, we can close the DNS query by modifying the enableLookups parameter value in the server.xml file.

DisableUploadTimeout

Similar to keeyalive in Apache

Configure the gzip compression (HTTP compression) feature for Tomcat

Compression= "on" compressionMinSize= "2048" compressableMimeType= "text/html,text/xml,text/javascript,text/css,text/plain"

HTTP compression can greatly improve the speed of browsing the website. its principle is that after the client requests the web page, the web page file is compressed from the server, and then downloaded to the client, and the browser of the client is responsible for decompressing and browsing. Compared with the normal browsing process HTML,CSS,Javascript, Text, it can save about 40% of the traffic. More importantly, it can also compress dynamically generated web pages, including CGI, PHP, JSP, ASP, Servlet,SHTML and so on, with amazing compression efficiency.

1) compression= "on" turns on compression function

2) compressionMinSize= "2048" enables compressed output content size, which defaults to 2KB.

3) noCompressionUserAgents= "gozilla, traviata" does not enable compression for the following browsers

4) compressableMimeType= "text/html,text/xml" compression type

Finally, don't forget to add the same configuration to port 8443, because if we follow the https protocol, we will use the configuration of port 8443, right?

To take a real example: in the last project, after four rounds of performance testing, the first round identified the problem, the second round was apache+tomcat/weblogic optimization, the third round was cluster optimization, and the fourth round was sql and codes optimization. Well, all the Tomcat optimizations have been added. Combined with the performance optimization of Apache in the third day, our architecture can be "galloping". Of course, any steps about database optimization are mentioned here, but with these two steps alone, our system has been greatly improved.

By the time we reached the second round, how many times had our performance improved?

The first column on the left is the first round of stress test reports without any tuning.

The column on the right is the stress test report optimized by apache and tomcat.

Let's see, how many times has this increased? This is just an improvement without changing the code, and now you see how important it is to tune an apache and tomcat properly? If you add the following code, SQL tuning, database tuning. So I had an extreme example of an 80-fold improvement in single transaction performance (both throughput and response time) in my last project.

At this point, I believe that you have a deeper understanding of "what is the method of Tomcat performance tuning?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow 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

Servers

Wechat

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

12
Report