TOMCAT/BIN/SETENV.SH parameter configuration for tomcat boot (in the current directory, automatically called when Tomcat starts)
Cat tomcat/bin/catalina.sh
If [-R "$SETENVPATH/bin/setenv.sh"]; Then
. "$SETENVPATH/bin/setenv.sh"
elif [-R "$CATALINA _home/bin/setenv.sh"]; Then
. "$CATALINA _home/bin/setenv.sh"
Fi
Cat tomcat/bin/setenv.sh
#!/bin/sh
Export catalina_opts= "-XMX350M-XMS350M-XX:PERMSIZE=64M-XX:MAXPERMSIZE=64M-XX:+USEPARALLELGC- Dlog4j.configuration=file:///home/www/config/java/scheduler_log4j.xml "
Export catalina_pid= "$CATALINA _base/logs/catalina.pid"
............
--------------------------------------------------------------------------
1,Apache Tomcat6 Support the characteristics of the Java language NiO (New I/O), using NiO on the server side will have better performance, to enhance the performance of server-side concurrency processing. However, the NIO feature is not turned on in Tomcat6 in the default configuration options. In the Tomcat configuration file Conf/server.xml, locate the following configuration:
<connector port= "8080" protocol= "http/1.1"
connectiontimeout= "20000"
redirectport= "8443"/>
As seen in the startup information of the console, the NIO configuration is not opened by default, and the information at startup is as follows:
2010-2-1 12:59:40 Org.apache.coyote.http11.Http11Protocol Init
Info: Initializing Coyote http/1.1 on http-8080
2010-2-1 12:59:40 Org.apache.catalina.startup.Catalina Load
modified to support the NIO type, configured as follows :
<connector port= "8080" protocol= "org.apache.coyote.http11.Http11NioProtocol "
connectiontimeout= "20000"
redirectport= "8443"/>
For testing, the NIO configuration is opened and the information at startup is as follows:
2010-2-1 13:01:01 Org.apache. Tomcat.util.net.NioSelectorPool Getsharedselector
Info: Using A shared selector for servlet Write/read
2010-2-1 13:01:01 Org.apache.coyote. http11. Http11nioprotocol Init
Info: Initializing Coyote http/1.1 on http-8080
2, Java Virtual machine performance optimization
Tomcat itself cannot run directly on the computer, it needs to rely on a hardware-based operating system and a Java virtual machine. Depending on your needs, you can choose a different operating system and a corresponding JDK version (as long as it complies with the Sun Release Java specification), and we recommend using Sun's published JDK. Make sure that the version you are using is up to date because sun and some other companies have been upgrading Java virtual machines to improve performance. Some reports show that JDK1.4 has improved performance by nearly 10% to 20% compared to JDK1.3.
The memory used for the Java Virtual machine can be set, but the virtual machine will not compensate if the selection is not correct. The size of the memory used by the virtual machine can be changed by the command line.
-xms<size> indicates the size of the JVM initialization heap,-xmx<size> represents the maximum value of the JVM heap. The general recommended heap maximum is set to 80% of the maximum available memory, and the-XMS and-XMX options are set to the same value.
Heap memory allocation
The initial memory allocated by the JVM is specified by-XMS, the default is physical memory 1/64;JVM the maximum allocated memory is specified by-XMX, which defaults to 1/4 of the physical memory. When the default free heap memory is less than 40%, the JVM increases the heap until the maximum limit of-xmx, and when the free heap memory is greater than 70%, the JVM reduces the heap until the minimum limit of-XMS. So the server is generally set-XMS,-xmx equal to avoid resizing the heap after each GC.
Non-heap memory allocation
The JVM uses-xx:permsize to set the non-heap memory initial value, which defaults to 1/64 of the physical memory, and the maximum non-heap memory by Xx:maxpermsize, which by default is 1/4 of physical memory.
JVM Memory Limit (max)
First, the JVM memory is limited to the actual maximum physical memory, assuming that the physical memory is infinitely large, the maximum value of the JVM memory is very much related to the operating system. In short, the 32-bit processor, although the controllable memory space has 4GB, but the specific operating system will give a limit, This limit is generally 2GB-3GB (typically under Windows systems for the 1.5g-2g,linux system 2g-3g), and processors over 64bit are not limited.
Java_opts= "-server-xms2048m-xmx2048m-xss256k-xx:+aggressiveopts-xx:+useparallelgc-xx:+usebiasedlocking"
Configuration example: java_opts= "-server-xx:permsize=128m-xx:maxpermsize=256m-xx:maxnewsize=256m"
java_opts= "$JAVA _opts-xms2048m-xmx2048m-xss128k"
Modify the Apache-tomcat-6.0.18\bin\catalina.bat (catalina.sh) configuration file to the following:
Set java_opts=-xms1024m-xmx1024m-xx:permsize=128m-xx:maxpermsize=256m
This sentence is added to the
REM---------------------------------------------------------------------------
And
REM Guess catalina_home if not defined
The position between, do not add to those if inside go, otherwise not necessarily will take effect.
3. Disable DNS queries
When the Web application wants to log the client's information, it also logs the client's IP address or converts it to an IP address through the name server lookup machine name.
DNS queries need to occupy the network and include the process of obtaining the corresponding IP from many remote servers or servers that do not work, which can take a certain amount of time.
Modify the Enablelookups parameter value in the Server.xml file: enablelookups= "false"
<connector
Port= "8887" maxthreads= "minsparethreads=" maxsparethreads= "75"
Enablelookups= "false" redirectport= "8443" acceptcount= "100"
debug= "0" connectiontimeout= "20000"
Disableuploadtimeout= "true"/>
Add enablelookups= "false" so that no DNS queries are used and there is no delay. Unless you need the full host name of all HTTP clients connected to the server.
Note: The meaning of the enablelookups of the connector is whether the call to Request.getremotehost () will take a DNS query to obtain the real host name of the remote client. True indicates that the query, FALSE, returns the IP address of the client in string format. The default value is: TRUE.
4. Adjust the number of threads
The parameters for performance control through the application's connector (Connector) are the number of threads that are created to process requests.
Tomcat uses the thread pool to speed up the response to processing requests. In the Java thread is the path of the program runtime, which is independent of the other control threads in one program and can run independently of the code snippet. They share the same address space. Multithreading helps programmers write efficient programs that maximize CPU utilization, keeping idle time to a minimum, and thus accepting more requests.
Examples are as follows:
<connector port= "protocol=" http/1.1 "
maxthreads= "600" maximum number of running threads, default value is 200
minsparethreads= "100" initializes the number of threads created
Maxsparethreads= "500" is the maximum number of threads that can be created, and once a thread is created that exceeds this value, Tomcat shuts down no longer requires the socket thread.
Acceptcount= "700" specifies the number of requests that can be placed into the processing queue when all the threads that can be used to process requests are used, and requests that exceed this number will not be processed, and the default value is 100
connectiontimeout= "20000"
Enablelookups= "false"
redirectport= "8443"/>
5. Configuration of Tomcat6 thread pool (Executor thread pool)
The first step is to open the shared thread pool
<executor name= "Tomcatthreadpool" nameprefix= "catalina-exec-"
maxthreads= "minsparethreads=" maxidletime= "600000"/>
Name this is the name of the thread pool, must be unique, this is used in the subsequent configuration.
Nameprefix the name prefix of the thread, which is used to mark the thread name, each thread is prefixed with the thread number, for example
Catalina-exec-1
Catalina-exec-2
MaxThreads the number of threads allowed for the maximum line constructor, the default is 200, large concurrency should be set higher, just limit it, do not occupy resources.
Minsparethreads the minimum number of active threads, the default is 25.
To adjust according to the load situation: too small to affect the speed of the reaction, too large occupation of resources.
MaxIdleTime a thread that exceeds the minimum number of active threads, the idle time exceeds this setting and is closed for 1 minutes by default.
ThreadPriority the level of the thread. Default is Thread.norm_priority
The second step is to specify the use of the shared thread pool in connector
<connector port= "8009" ... maxthreads= "" executor= "Tomcatthreadpool" .../>
Note that once the thread pool is used, other threading properties, such as MaxThreads, will be ignored
6. Set the session expiration time in Tomcat
In \conf\web.xml, specify by parameter:
<session-config>
<session-timeout>180</session-timeout>
</session-config> units are minutes.
Tomcat container tuning Detailed parameters:
Vim $CATALINA _home/conf/server.xml
...
<connector port= "8080" protocol= "http/1.1"
uriencoding= "UTF-8" minsparethreads= "maxsparethreads=" 75 "
Enablelookups= "false" disableuploadtimeout= "true" connectiontimeout= "20000"
Acceptcount= "maxthreads=" 300 "
Useurivalidationhack= "false"
Compression= "on" compressionminsize= "2048"
Compressablemimetype= "Text/html,text/xml,text/javascript,text/css,text/plain"
Redirectport= "8443"
/>
...
Comments
Uriencoding:uri code, here the parameter is UTF-8, very easy to understand, that is, support Chinese domain name
Minsparethreads: Minimum idle thread, which is the smallest thread that tomcat can keep
Maxsparethreads: The maximum idle process, if this parameter is exceeded, is recycled
(These two parameters depend on the actual environment, if there is a concurrent outbreak every day, it is better to set a greater min)
Enablelookups: Whether to turn on domain name resolution, this is generally forbidden, domain name resolution affects performance
Disableuploadtimeout:
ConnectionTimeout: Connection time-out, in MS, depending on program performance
MaxThreads: The maximum number of requests that can be received at the same time, that is, the concurrency
Acceptcount: If the maxthreads is exceeded, the excess part cannot exceed this parameter value, and if it is exceeded, it is rejected
Useurivalidationhack: Set to False to reduce the unnecessary checking of some URLs by Tomcat, thereby reducing the cost of "unknown sensation"
Compression: If compression is enabled, it must be enabled
Compressionminsize: Compressed file size lower limit, per byte
Compressablemimetype: MIME type of compressed file
Redirectport: Redirect (so if 443https, then 8443 Connector should also add the above parameters)
JVM Tuning
Vim $CATALINA _home/bin/catalina.sh
...
Export java_opts= "-SERVER-XMS8192M-XMX8192M-XMN3072M-XSS512K-XX:+AGGRESSIVEOPTS-XX:+USEBIASEDLOCKING-XX: PERMSIZE=256M-XX:MAXPERMSIZE=4096M-XX:+DISABLEEXPLICITGC-XX:MAXTENURINGTHRESHOLD=31-XX:+USECONCMARKSWEEPGC-XX: +useparnewgc-xx:+cmsparallelremarkenabled-xx:+usecmscompactatfullcollection-xx:largepagesizeinbytes=128m-xx:+ Usefastaccessormethods-xx:+usecmsinitiatingoccupancyonly-djava.awt.headless=true "
...
"Comment Information"
JVM tuning is actually a temporary addition to the JAVA_OPTS variable value when the Catalina is started.
-server: Meaning is to change Tomcat to production mode, this look at the online description, not add words, just like the experimental environment. Therefore, the parameter must be added
-XMS:JVM Minimum heap memory
-XMX:JVM Max heap Memory
The difference between these two values is called reserved memory, but it is advisable to set the same, that is, the minimum memory with the maximum memory, the reason is because the memory is collected when the CPU needs to be consumed, if suddenly concurrency comes again, and Tomcat is recovering memory, then it is uncomfortable
-XMN: New eco-memory, this official recommendation is 3/8 of JVM heap memory, heap memory = new memory + old memory + persistent memory
-XSS: Sets the stack size for each thread
-xx:+aggressiveopts: As its name (aggressive), enabling this parameter will allow your JVM to use the latest added optimization technology whenever the JDK version is upgraded
-xx:+usebiasedlocking: Enable an optimized lock, we know in our AppServer, each HTTP request is a thread, and some request a short request long, there will be request queuing phenomenon, and even a thread blocking, This optimized thread lock allows you to automate the optimal provisioning of threading within your appserver.
-xx:permsize: Persistent minimum memory, default to 1/64 of physical memory size
-xx:maxpermsize: Persistent maximum memory, default to 1/4 of physical memory size
-XX:+DISABLEEXPLICITGC: The Display Call "System.GC ()" is not allowed in the program code,
-xx:maxtenuringthreshold: Sets the time that the object stays in the young state, the longer the probability of being recycled, the greater the chance of being recovered, and if it is 0, it goes directly to the old state.
-xx:+useconcmarksweepgc:cms GC, this feature is only available in jdk1.5, which is a later version, which uses the GC estimate trigger and the heap occupancy trigger.
We know that frequent GC will make the surface of the JVM's ups and downs to affect the efficiency of the system, so the use of the CMS GC can increase the number of GC, the response time of each GC is very short, for example, after using the CMS GC after Jprofiler observation, the GC is triggered by a lot of times, The GC takes only a few milliseconds each time.
-XX:+USEPARNEWGC: Young generation uses multi-threaded parallel recovery to accelerate recycling
-xx:+cmsparallelremarkenabled: Minimize Mark's time when using USEPARNEWGC
-xx:+usecmscompactatfullcollection: Prevents Memoryfragmention, organizes live object, and reduces memory fragmentation when using concurrent GC
-xx:largepagesizeinbytes: Specify paging page size for Java heap
-xx:+usefastaccessormethods:get,set method into local code
-xx:+usecmsinitiatingoccupancyonly: Indicates that only after oldgeneration has used the initialized scale concurrent collector start the collection
-djava.awt.headless=true ": This parameter is generally used in the final use, this is the function of all parameters, and sometimes we will use in our Java EE project some charting tools such as: Jfreechart, for the Web page output gif/ JPG streams, in the WINODWS environment, in general, our app server in the output graphics will not encounter any problems, but in the Linux/unix environment often encounter a exception cause you in the WINODWS development environment film display of good but in linux/ UNIX does not show up, so add this parameter to avoid this situation. "Even though I know the result, I don't know where it is."
Tomcat Performance Optimizations