Tomcat commonly used optimization methods

Tomcat optimizes the configuration parameters to
optimize the memory, mainly in the bin/catalina.bat/sh configuration file. On linux, add in catalina.sh:

JAVA_OPTS="-server -Xms1G -Xmx2G -Xss256K -Djava.awt.headless=true -Dfile.encoding=utf-8 -XX:MaxPermSize=256m -XX:PermSize=128M -XX:MaxPermSize=256M"
其中:

• -server: enable jdk server version.
• -Xms: The minimum heap memory when the virtual machine is initialized.
• -Xmx: The maximum heap memory that the virtual machine can use. #-Xms and -Xmx are set to the same value to avoid JVM performance fluctuations caused by frequent GC
• -XX:PermSize: Set the initial value of non-heap memory, the default is 1/64 of physical memory.
• -XX:MaxNewSize: The maximum value of the new generation in the entire heap memory.
• -XX:MaxPermSize: Perm (commonly known as the method area) occupies the maximum value of the entire heap memory, also known as the largest permanent reserved area of ​​the memory.
1) Error message: java.lang.OutOfMemoryError: Java heap space

The memory that Tomcat can use by default is 128MB. In larger application projects, this memory is not enough, which may cause the system to fail to run. Common problems are reporting Tomcat memory overflow error, Outof Memory (insufficient memory) exception, which causes the client to display a 500 error, generally adjust Tomcat -Xms and -Xmx to solve the problem, usually set -Xms and -Xmx In the same way, the maximum value of the heap is set to 80% of the maximum value of the physical available memory.

set JAVA_OPTS=-Xms512m-Xmx512m
2) Error prompt: java.lang.OutOfMemoryError: PermGenspace

The full name of PermGenspace is Permanent Generationspace, which refers to the permanent storage area of ​​the memory. This memory is mainly used by the JVM to store Class and Meta information. When the Class is loaded by the Loader, it will be placed in PermGenspace. It and the storage class instance (Instance) The Heap area is different. GC (Garbage Collection) will not clean up PermGenspace during the main program runtime, so if your application has a very CLASS, PermGen space error is likely to occur. This error is common in web servers against JSP When doing precompile. If you use a large number of third-party jars under your WEB APP, the size of which exceeds the default size of jvm (4M), then this error message will be generated. Solution:

setJAVA_OPTS=-XX:PermSize=128M
3) When using -Xms and -Xmx to adjust the heap size of tomcat, you also need to consider the garbage collection mechanism. If the system spends a lot of time collecting garbage, please reduce the heap size. A complete garbage collection should not exceed 3-5 seconds. If garbage collection becomes a bottleneck, you need to specify the generation size, check the detailed output of garbage collection, and study the impact of garbage collection parameters on performance. Generally speaking, you should use 80% of the physical memory as the heap size. When adding processors, remember to increase memory, because allocation can be done in parallel, and garbage collection is not parallel.

2. Optimization of the number of connections:
#Optimize the number of connections, which is mainly modified in the conf/server.xml configuration file.

2.1. Optimize the number of threads
Find Connector port="8080" protocol="HTTP/1.1", increase the attributes of maxThreads and acceptCount (make acceptCount greater than or equal to maxThreads), as follows:

<Connector port=“8080” protocol="HTTP/1.1"connectionTimeout=“20000” redirectPort="8443"acceptCount=“500” maxThreads=“400” />
其中:

• maxThreads: the maximum number of threads that tomcat can use for request processing, the default is 200
• minSpareThreads: the initial number of tomcat threads, that is, the minimum number of idle threads
• maxSpareThreads: the maximum number of tomcat idle threads, the excess will be closed
• acceptCount: when all can be used When the number of threads for processing requests are used, the number of requests that can be placed in the processing queue, and requests exceeding this number will not be processed. The default is 100.
2.2. Use the thread pool
to add an executor node in server.xml, and then configure the connector The executor properties are as follows:

<Executor name=“tomcatThreadPool” namePrefix="req-exec-"maxThreads=“1000” minSpareThreads="50"maxIdleTime=“60000”/>
<Connector port=“8080” protocol="HTTP/1.1"executor=“tomcatThreadPool”/>
其中:

• namePrefix: the naming prefix of
threads in the thread pool
• maxThreads: the maximum number of threads in the thread pool • minSpareThreads: the minimum number of idle threads in the thread pool
• maxIdleTime: When the minimum number of idle threads is exceeded, multiple threads will wait for this length of time and then shut down
• threadPriority: thread priority.
Note: When tomcat has a large number of concurrent users, a single jvm process may indeed open too many file handles. At this time, a java.net.SocketException: Too many open files error will be reported. You can use the following steps to check:

• ps -ef |grep tomcat View the process ID of tomcat, record the ID number, assuming that the process ID is 10001
• lsof -p 10001|wc -l View the number of file operations with the current process id 10001
• Use the command: ulimit -a to view each The maximum number of files allowed to be opened by a user
3. Tomcat Connector three operating modes (BIO, NIO, APR)
3.1. Comparison of the three modes:
1) BIO: One thread processes one request. Disadvantages: When the amount of concurrency is high, there are more threads, which wastes resources. Tomcat7 or below uses this method by default in Linux systems.

2) NIO: Using Java's asynchronous IO processing, a large number of requests can be processed through a small number of threads. Tomcat8 uses this method by default in Linux systems. Tomcat7 must modify the Connector configuration to start (conf/server.xml configuration file):

<Connector port="8080"protocol="org.apache.coyote.http11.Http11NioProtocol" connectionTimeout="20000"redirectPort="8443"/>
3) APR (Apache Portable Runtime): Solve the problem of io blocking from the operating system level. If Linux is installed with apr and native, Tomcat will support apr when it is started directly.

3.2, apr mode
install apr and tomcat-native

yum -y install apr apr-devel
enter the tomcat/bin directory, for example:

cd /opt/local/tomcat/bin/
tar xzfv tomcat-native.tar.gz
cd tomcat-native-1.1.32-src/jni/native
./configure --with-apr=/usr/bin/apr-1 -config
make && make install
#Note that the latest version of tomcat comes with tomcat-native.war.gz, but its version is too high compared to the apr installed by yum, and an error will be reported during configure.

Solution: yum remove apr apr-devel –y, uninstall apr and apr-devel installed by yum, download the latest version of apr source package, compile and install; or download the lower version of tomcat-native to compile and install

After the installation is successful, you also need to set environment variables for tomcat by adding one line to the catalina.sh file:

CATALINA_OPTS="-Djava.library.path=/usr/local/apr/lib"
#apr download address: http://apr.apache.org/download.cgi

#tomcat-native download address: http://tomcat.apache.org/download-native.cgi

Modify the conf/server.xml corresponding to the 8080 end

protocol=“org.apache.coyote.http11.Http11AprProtocol”


PS: After startup, check the log and display the following to indicate that the apr mode is turned on

Sep 19, 2016 3:46:21 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler [“http-apr-8081”]

Guess you like

Origin blog.csdn.net/weixin_45942735/article/details/104396122