1. Tomcat memory optimization
Tomcat memory optimization is mainly to optimize the tomcat startup parameters, we can set the java_OPTS parameter in the tomcat startup script catalina.sh. JAVA_OPTS parameter description -server enables the server version of jdk; -Xms java virtual machine initialization minimum memory; -Xmx java virtual machine can use the maximum memory; -XX: PermSize memory permanent reserved area -XX:MaxPermSize memory maximum permanent reserved area Server parameter configuration
Now the company's server memory can generally be added to a maximum of 2G, so the following configuration can be adopted:
JAVA_OPTS=’-Xms1024m -Xmx2048m -XX: PermSize=256M -XX:MaxNewSize=256m -XX:MaxPermSize=256m’
After the configuration is complete, you can restart Tomcat, and use the following commands to check whether the configuration takes effect: First, check the Tomcat process number:
sudo lsof -i:9027
We can see that the Tomcat process number is 12222. Check whether the configuration takes effect:
sudo jmap – heap 12222
We can see that parameters such as MaxHeapSize have taken effect.
2. Tomcat concurrency optimization
1. Tomcat connection related parameters
in the Tomcat configuration file server.xml
<Connector port="9027"
protocol="HTTP/1.1"
maxHttpHeaderSize="8192"
minProcessors="100"
maxProcessors="1000"
acceptCount="1000"
redirectPort="8443"
disableUploadTimeout="true"/>
2. Adjust the concurrent processing capability of the connector
(1) Parameter description
maxThreads The maximum number of threads requested by the client minSpareThreads The number of idle socket threads created when Tomcat is initialized In the case of a channel, forward the client request to the SSL-based redirectPort port acceptAccount The maximum number of listening port queues, after which the client request will be rejected (cannot be less than maxSpareThreads) connectionTimeout Connection timeout minProcessors The minimum number of processing threads when the server is created maxProcessors The maximum number of processing threads at the same time Number of processing threads URIEncoding URL uniform encoding
(2) Configuration example in Tomcat
<Connector port="9027"
protocol="HTTP/1.1"
maxHttpHeaderSize="8192"
maxThreads="1000"
minSpareThreads="100"
maxSpareThreads="1000"
minProcessors="100"
maxProcessors="1000"
enableLookups="false"
URIEncoding="utf-8"
acceptCount="1000"
redirectPort="8443"
disableUploadTimeout="true"/>
3. Tomcat cache optimization
(1) Parameter description
compression Turn on the compression function compressionMinSize Enable the compressed output content size, which defaults to 2KB compressableMimeType Compression type connectionTimeout Defines the timeout time for establishing a client connection. If it is -1, it means that the time for establishing a client connection is not limited
(2) Configuration example in Tomcat
<Connector port="9027"
protocol="HTTP/1.1"
maxHttpHeaderSize="8192"
maxThreads="1000"
minSpareThreads="100"
maxSpareThreads="1000"
minProcessors="100"
maxProcessors="1000"
enableLookups="false"
compression="on"
compressionMinSize="2048"
compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain"
connectionTimeout="20000"
URIEncoding="utf-8"
acceptCount="1000"
redirectPort="8443"
disableUploadTimeout="true"/>
4. Reference configuration
(1) Old configuration
Refer to the network to do the following configuration for the server, take it out and share it:
<Connector port="9027"
protocol="HTTP/1.1"
maxHttpHeaderSize="8192"
maxThreads="1000"
minSpareThreads="25"
maxSpareThreads="75"
enableLookups="false"
compression="on"
compressionMinSize="2048"
compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain"
connectionTimeout="20000"
URIEncoding="utf-8"
acceptCount="200"
redirectPort="8443"
disableUploadTimeout="true" />
Later, it was found that the performance bottleneck appeared when the number of visits reached more than 3 million. 2> Changed configuration
<Connector port="9027"
protocol="HTTP/1.1"
maxHttpHeaderSize="8192"
maxThreads="1000"
minSpareThreads="100"
maxSpareThreads="1000"
minProcessors="100"
maxProcessors="1000"
enableLookups="false"
compression="on"
compressionMinSize="2048"
compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain"
connectionTimeout="20000"
URIEncoding="utf-8"
acceptCount="1000"
redirectPort="8443"
disableUploadTimeout="true"/>