java tomcat jvm optimization

 

tomcat actuator (thread pool) optimization

tomcat is not enabled by default thread pool, in tomcat each user request is a thread, so we can use the thread pool to improve performance. tomcat front section has a thread scheduling, sends the user's request in the thread pool, after a certain time of the user's request thread pool task becomes worker.

1, open the thread pool: Open the server.xml configuration on the thread pool

<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
        maxThreads="600" minSpareThreads="10"/>

Important parameters:

name : the name of the shared thread pool. This is the Connector to share a thread pool to be referenced by name, the name must be unique. Default value: None;

namePrefix : On the JVM, each running thread can have a name string. This attribute sets a name prefix string for each thread in the thread pool, Tomcat will be appended to the number of threads of the prefix. Default value: tomcat-exec-;

maxThreads : The thread pool can accommodate the maximum number of threads. Default: 200;

maxIdleTime : tomcat before closing an idle thread, allowing the thread idle time duration (in milliseconds). Only the current number of active threads is greater than the value minSpareThread, will shut down idle threads. Default: 60,000 (one minute).

minSpareThreads : Tomcat should not always on the minimum number of active threads. Default: 25.

2, reference the thread pool

  <Connector executor="tomcatThreadPool"
               port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol"
               connectionTimeout="20000"
               redirectPort="8443" 
               />

 PS: Connector of an optimal assignment: In general we do not use open thread pool Executor, we disposed directly on Connector

Copy the code
 <Connector 
               port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol"
               maxThreads="1000" 
               minSpareThreads="100"
               acceptCount="1000"
               maxConnections="1000"
               connectionTimeout="20000"
               maxHttpHeaderSize="8192"
               tcpNoDelay="true"
               compression="on"
               disableUploadTimeout="true"  
               redirectPort="8443" 
               enableLookups="false"
               URIEncoding="UTF-8"
               />
Copy the code

maxThreads: The maximum number of threads
minSpareThreads: the minimum number of threads
acceptCount: the maximum queue length acceptable
maxConnections: The maximum number of connections
connectionTimeout: ms timeout for
maxHttpHeaderSize: request header maximum
tcpNoDelay: If true, the server sets TCP_NO_DELAY socket option, in most cases under improves performance. This is set to default to true
compression: Compression GZIP on whether to turn off open closed forde: in all cases compression
disableUploadTimeout: Upload time limit
enableLookups: Close reverse DNS lookup, DNS reverse lookup is very time-consuming

tomcat optimization of disabled AJP connector to achieve static and dynamic separation

AJP: is a packet-oriented protocol. web server and servlet container interact through TCP link, in order to save the cost of expensive SOCKET created, WEB server will try to maintain a permanent link to TCP Servlet container, and multiple requests and responses cycle will be reused link.

1 web client to access resources tomcat index.jsp server is directly accessible, if we visit is a static resource, it is inconvenient to handle the tomcat these static resources will be returned to the Apache server, as returned by them to the user, so the tomcat server nginx server relative to less efficient in dealing with static resources. So our web server is generally Nginx + tomcat, nginx handles static resources, so AJP protocol we use nginx + can turn it off when tomcat architecture to optimize efficiency.

 Note in the tomcat server.xml <Connector port = "8009" protocol = "AJP / 1.3" redirectPort = "8443" /> to.

 

 Four, tomcat in JVM parameter optimization

Jvm to optimize the parameters of our main optimization of heap memory, heap memory is divided into three large pieces of the young generation, old time, permanent generations.

How to allocate the size of these three areas:

If our needs often need to create objects, and will be recycled after use immediately, such a scenario we can allocate more space to the young generation. For example: External provide a query interface, return json data, this interface is frequently invoked, we can use this service to transfer large space for the young generation.

Old's space can be set to be larger when more static variables.

1, the size of the heap memory

-Xms: JVM startup initialization memory

-Xmx: JVM maximum heap memory, after -Xmx JVM startup parameter specifies the allocation of memory space as a heap, but not necessarily all use, JVM will be adjusted according to the JVM memory for real -Xmx parameter.

A difference between -Xmx, -Xms Virtual space is the size of three.

2, the young generation

-xx: NewRatio = 8 years old and represents the ratio of the young generation is 8: 1

-XX: SurvivorRatio = 32 represents the ratio of eden survivor is 32: 1

-Xmn indicates the size of the young generation set

3, permanent generations

-XX:PermSize=16m -XX:MaxPermSize=64M

4, Thread Stack (thread area)

-XX:Xss=128k 


Modify the tomcat bin directory catalina.bat or catalina.sh file, read as follows

windows 下 catalina.bat

rem ---------------------------------------------------------------------------
set JAVA_OPTS=-Dfile.encoding=UTF-8 -server -Xms1024m -Xmx2048m -XX:NewSize=512m -XX:MaxNewSize=1024m -XX:PermSize=256m -XX:MaxPermSize=512m -XX:MaxTenuringThreshold=10 -XX:NewRatio=2 -XX:+DisableExplicitGC
setlocal

Linux under catalina.sh

# -----------------------------------------------------------------------------
JAVA_OPTS="-Dfile.encoding=UTF-8 -server -Xms1024m -Xmx2048m -XX:NewSize=512m -XX:MaxNewSize=1024m -XX:PermSize=256m -XX:MaxPermSize=512m -XX:MaxTenuringThreshold=10 -XX:NewRatio=2 -XX:+DisableExplicitGC"
# OS specific support.  $var _must_ be set to either true or false.

Parameter Description:

Copy the code
# -Dfile.encoding: the default file encoding
# -Xms512m disposed opposite JVM minimum memory is 512m, this value can be set in order to avoid the same -Xmx JVM memory is reallocated after each garbage collection is completed .
# -Xmx1024 Set JVM maximum available memory
# -XX: NewSize set the size of the young generation
# -XX: MaxNewSize set the maximum memory size of the young generation
# -XX: PermSize set up a permanent behalf size
# -XX: MaxPermSize set the maximum permanent generation of memory
# -XX: NewRatio = 2 to set the young generation and the old age ratio of 2: represents the ratio of the young generation and the old age is 1: 2
# -XX: MaxTenuringThreshold maximum age of this garbage, the default is 15. 0: that the young generation does not pass directly into the region Survivor years old, for the application of more years old, is set to 0 can improve efficiency. If this value is large represents the young generation of objects in multiple copies Survivor areas, in order to increase the survival time of the object in the young generation, to increase the probability of the young generation to be recovered.
#XX: + DisableExplicitGC application will ignore calls received GC code. And System.GC () is an air conditioner.
Copy the code

 

 

 

https://www.cnblogs.com/jalja/p/6117665.html

https://blog.csdn.net/shanhuhau/article/details/53911384

https://blog.csdn.net/dandan2zhuzhu/article/details/48440159

Guess you like

Origin www.cnblogs.com/youxin/p/12275371.html