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

Optimized configuration of tomcat6 (refer to network resource arrangement)

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

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)

I. Optimization of JVM

1.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.

PS: the JVM optimization discussed here is based on the jdk1.6 version of Oracle Sun, and other JDK or lower versions of JDK are not applicable.

Optimization of 2.Tomcat startup line parameters

The startup parameters of Tomcat are located in the tomcat installation directory\ bin directory. Modify the catalina.sh file and add the following parameters after the last line of the comment:

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"

Parameter explanation:

-server

I don't care why, 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, and 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. More load and throughput can be obtained. More. no, no, no. There's more. no, no, no.

-Xms-Xmx

That is, JVM memory is set, it is the best way to set Xms and Xmx to the same value.

PS: 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? When setting this maximum memory XMX value, open a command line first and type the following command:

Java-Xmx2048m-version # check whether the value you set is supported

-Xmn

Set the size of the younger generation to 512m. Whole heap size = young generation size + old generation size + persistent 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 of memory 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 is physical memory 4GB, then 1 / 64 is 64MB, which is the default value of PermSize, which is the initial size of immortal memory, and 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 of memory 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 start to perform concurrent garbage collection (CMS) for the older generation. At this time, the remaining 10% space is 548810% = 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 is enough, so as long as the above formula is satisfied. 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

Second, the optimization of tomcat itself

Optimization in 1.Tomcat container

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.

Open the tomcat installation directory\ conf\ server.xml file and navigate to this line:

Protocol= "org.apache.coyote.http11.Http11NioProtocol"

The configuration of Tomcat apr operation mode is a relatively troublesome one of the three operation modes. According to official documentation, Tomcat apr requires the support of the following three components:

APR library [APR Library]

JNI wrappers for APR used by Tomcat (libtcnative)

OpenSSL libraries [OpenSSL library]

In addition, as with configuring the nio operation mode, you need to change the value of the protocol attribute of the corresponding Connector node to org.apache.coyote.http11.Http11AprProtocol.

Download tomcat-native-1.1.33-src.tar.gz

Tar xzf tomcat-native-1.1.33-src.tar.gzcd tomcat-native-1.1.33-src/jni/native./configure make make install

Edit / etc/profile, add at the end

Export LD_LIBRARY_PATH=/usr/local/apr/lib

Just restart and run tomcat.

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

Database

Wechat

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

12
Report