even untimely, it should be understood. has been in the Java community for nearly 3 years, for some performance tuning topic I have been stuck in the mouth, just for last night saw a performance tuning article, I can't help it .
Tomcat Performance Tuning:
Locate the Conf directory under the Tomcat root directory and modify the contents of the Server.xml file . For this part of the tuning, what I learned is nothing but set the maximum number of Tomcat server concurrency and the number of threads created during tomcat initialization settings, of course, there are some other performance tuning settings, I based on the performance of my machine set some parameter values, give you a detailed explanation of it:
1.uriencoding= "UTF-8" : Sets the character set of Tomcat. This configuration is generally not set, because of the garbled conversion we will be specific in the specific project, the direct modification of the Tomcat character set is too rigid.
2.maxthreads= "+": Sets the maximum number of concurrency for the current tomcat. The maximum number of requests for a tomcat default configuration is 150, which can support 150 concurrent concurrently. In practice, however, the maximum concurrency is very much related to hardware performance and CPU count, and better hardware and higher processors will enable Tomcat to support more concurrent numbers. If you are generally in real development, when an application has more than 250 concurrency, the cluster of application servers will be taken into account.
3.minsparethreads= ": Sets the number of threads created at the current Tomcat initialization, with a default value of 25.
4,acceptcount= "": when the number of simultaneous connections reached the value set by the MaxThreads parameter, you can also receive the number of queued connections, more than this connection will directly return to reject the connection. Specifies the number of requests that can be placed in the processing queue when any number of threads that can be used to process requests is used, and requests that exceed this number will not be processed. The default value is 100. in real-world applications, if you want to increase the concurrency of Tomcat, you should increase the value of both Acceptcount and MaxThreads .
5,enablelookups= "false": whether to turn on the domain name counter-check, generally set to false to improve processing power, its value and true, is rarely used.
6,maxkeepaliverequests= "1":nginx Dynamic transfer to Tomcat,nginx is not keepalive, and the Tomcat side of the default open KeepAlive, Will wait for keepalive timeout, default is not set to use ConnectionTimeout. So you must set the Tomcat timeout and turn off Tomcat's keepalive. Otherwise, a large number of Tomcat socket timewait will be generated. maxkeepaliverequests= "1" prevents Tomcat from producing a large number of time_wait connections, which in some way avoids the ability to feign death.
JVM Performance Tuning:
Tomcat itself is still running on the JVM, and we can give tomcat better performance by adjusting the JVM parameters. There are two main aspects of tuning for the JVM: memory tuning and garbage collection policy tuning.
One: Memory tuning Find the bin directory under the Tomcat root directory, set the java_opts variable in the catalina.sh file , because the subsequent startup parameters will handle the java_opts as the JVM's startup parameters. The memory structure of the Java Virtual machine is a bit complex, I believe many people in the understanding is very abstract, it is mainly divided into heap, stack, method area and garbage collection system, such as a few parts, the following is my from the online grilled memory structure diagram:
Memory tuning this piece, is simply by modifying their respective memory space size, so that the application can be more reasonable use, is I based on the performance of my machine parameters, give you a detailed explanation of the meaning of each parameter it:
1.-XMX512M: Sets the maximum available memory size of the heap for a Java virtual machine, in megabytes (m), the entire heap size = young generation size + old generation size + persistent generation size. The average fixed size for a durable generation is 64m. The different distribution of the heap will have a certain effect on the system. As much as possible to reserve the object in the Cenozoic, reduce the number of GC in the old age (usually old age recovery is relatively slow). In practice, the initial and maximum values of the heap are usually set equal, which can reduce the number of garbage collections and spatial expansion that the program runs, thereby improving program performance.
2.-XMS512M: Sets the initial value memory size of the heap of the Java virtual machine, in megabytes (m), This value can be set to the same as-xmx to avoid the JVM reallocating memory after each garbage collection completes .
3,-xmn170m: Set the young generation memory size, unit: trillion (m), this value on the system performance is large, sun is the official recommended configuration for the entire heap of 3/8. in general, after increasing the memory of younger generations, it will also reduce the size of older generations .
4.-xss128k: Sets the stack size for each thread. after JDK5.0, each thread stack size is 1M, before each thread stack size is 256K. The size of the memory required for the more applied threads to be adjusted. In the same physical memory, reducing this value can generate more threads. However, the operating system of the number of threads within a process is still limited, can not be generated indefinitely, the empirical value of 3000~5000 around.
5.-xx:newratio=4: Set the ratio of the younger generation (including Eden and two survivor zones) to the older generation (except for the persistent generation). Set to 4, the ratio of the young generation to the older generation is 1:4, and the younger generation accounts for 1/5 of the entire stack.
6,-xx:survivorratio=4: Set the size ratio of Eden and survivor in the younger generation. Set to 4, the ratio of two survivor to one Eden area is 2:4, and one survivor area represents 1/6 of the entire young generation.
7,-xx:maxpermsize=16m: Set the persistent generation size of 16m, above also said that the persistent generation of general fixed memory size of 64m.
8,-xx:maxtenuringthreshold=0: Set the maximum age of garbage. If set to 0, then the young generation object does not go through the survivor area, directly into the old generation. For older generations of more applications, can improve efficiency. If this value is set to a larger value, the younger generation objects are duplicated multiple times in the Survivor area, which increases the survival time of the object's younger generations, increasing the introduction of being recycled in the younger generation.
Second: Garbage collection Policy tuning find the bin directory under the Tomcat root directory, and also set the java_opts variable in the catalina.sh file. we all know that Java virtual machines have a default garbage collection mechanism, but the efficiency of different garbage collection mechanisms is different, which is why we often adjust the garbage collection policy of the Java Virtual machine accordingly. Here are some of the garbage collection policies that are configured with some of my needs:
Java Virtual machine garbage collection policy is generally divided into: serial collector, parallel collector, and concurrent collector .
Serial collector:
1,-XX:+USESERIALGC: On behalf of the garbage collection strategy for the serial collector, that is, the entire scanning and copying process using a single-threaded way to apply to single CPU, the new generation of space is small and the time required to suspend the application is not very high, is the client level of the default GC mode, garbage collection methods that are mainly prior to JDK1.5 .
Concurrent Collectors:
1,-XX:+USEPARALLELGC: On behalf of the garbage collection strategy for the parallel collector (throughput priority), that is, the entire scanning and replication process in a multi-threaded way, for multi-CPU, for short time-out requirements of the application, the server level is the default GC mode. This configuration is only valid for younger generations. This configuration only allows the younger generation to use concurrent collection, while older generations still use serial collection.
2.-xx:parallelgcthreads=4: Configure the number of threads for the parallel collector, that is, how many threads are garbage collected together. This value is best configured to be equal to the number of processors .
3,-XX:+USEPARALLELOLDGC: Configure the old Generation garbage collection method for parallel collection. JDK6.0 supports parallel collection of older generations .
4.-XX:MAXGCPAUSEMILLIS=100: Set the maximum time for each young generation of garbage collection, and if this time is not met, the JVM will automatically adjust the younger generation size to meet this value.
5.-xx:+useadaptivesizepolicy: When this option is set, the parallel collector automatically selects the size of the younger generation and the corresponding Survivor area scale to achieve the minimum corresponding time specified by the target system or the collection frequency, etc., which is recommended to be open when using a parallel collector.
Concurrent Collectors:
1.-XX:+USECONCMARKSWEEPGC: Represents the garbage collection policy as a concurrent collector.
OK, so here's my summary of the garbage collection strategy for virtual machines. This is the phrase: optimized learning has been on the road, there is a picture stolen from other blogs, it is said that the above three kinds of GC mechanism is needed to use.
[Note: The tuning parameters in this article are individually configured according to their own needs, and are tested by the test, if necessary, according to their actual situation to configure the parameter size. ]
Performance Tuning summary for Tomcat and Java Virtual machine