tomcat server optimization
1. JDK memory optimization
Configure relevant parameters to optimize tomcat performance according to the physical content of the server. When the memory required by an application exceeds the maximum heap value, the virtual machine will prompt a memory overflow and cause the application service to crash. Therefore, it is generally recommended that the maximum heap size be set to 80% of the maximum available memory. The default memory that Tomcat can use is 128MB. In larger application projects, this memory is not enough and needs to be increased.
The memory that Tomcat can use by default is 128MB. Under Windows, in the file /bin/catalina.bat , under Unix, add the following settings in front of the file /bin/catalina.sh: JAVA_OPTS='-Xms [initial memory size] -Xmx [maximum memory that can be used] -XX:PermSize=64M -XX:MaxPermSize=128m ' Several parameter values need to be increased. For example: JAVA_OPTS='-Xms256m -Xmx512m' means the initial memory is 256MB and the maximum memory that can be used is 512MB.
Detailed explanation of parameters
-server 启用jdk 的 server 版; -Xms java虚拟机初始化时的最小内存; -Xmx java虚拟机可使用的最大内存; -XX:PermSize 内存永久保留区域 -XX:MaxPermSize 内存最大永久保留区域 -Xmn jvm最小内存
32G memory configuration example:
JAVA_OPTS="$JAVA_OPTS -Xms10g -Xmx10g -XX:PermSize=1g -XX:MaxPermSize=2g -Xshare:off -Xmn1024m
2. Tomcat thread optimization
In the configuration in the tomcat configuration file server.xml, the parameters related to the number of connections are:
maxThreads: Tomcat uses threads to Handle every request received. This value represents the maximum number of threads that Tomcat can create. Default value is 150.
acceptCount: Specifies the number of requests that can be placed in the processing queue when all available threads for processing requests are used. Requests exceeding this number will not be processed. Default value is 10.
minSpareThreads: The number of threads created when Tomcat is initialized. Default value is 25.
maxSpareThreads: Once the number of created threads exceeds this value, Tomcat will close socket threads that are no longer needed. Default value is 75.
enableLookups: Whether to reversely check the domain name, the default value is true. In order to improve processing capabilities, it should be set to false
connnectionTimeout: Network connection timeout, default value 60000, unit: milliseconds. Setting it to 0 means it will never time out, which is a dangerous setting. Usually it can be set to 30000 milliseconds.
maxKeepAliveRequests: Number of keep requests, default value is 100. bufferSize: Input stream buffer size, default value 2048 bytes.
compression: Compression transmission, value on/off/force, default value off. The parameters related to the maximum number of connections are maxThreads and acceptCount. If you want to increase the number of concurrent connections, you should increase these two parameters at the same time.
32G memory configuration example:
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" maxThreads="1000" minSpareThreads="60" maxSpareThreads="600" acceptCount="120" redirectPort="8443" URIEncoding="utf-8"/>
Using visualvm performance monitoring
1. What is VisualVM
FastDFS is a monitoring tool that comes with jdk. It provides a visual interface for viewing details of Java technology-based programs running on the Java Virtual Machine. VisualVM organizes the JVM software-related data retrieved by the Java Development Kit (JDK) tools and provides that information in a way that allows you to quickly view data about multiple Java applications. You can view relevant data of local applications and applications running on the remote host
2. How to install
There is a jvisualvm.exe file in the jkd bin directory and you can use it by double-clicking it
3. How to use jvisualvm
1 , Configure JMX to manage tomcat:
set JAVA_OPTS=-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9008 -Dcom.sun.management.jmxremote.authenticate=false - Dcom.sun.management.jmxremote.ssl=false
2. Restart tomcat
3. Double-click jvisualvm.exe to add the server IP address and add the jmx port that needs to be monitored
The effect is as follows: