jvm tuning case

JVM tuning

JVM Collector

Serial collectors are used by default, and throughput collector is used for a single cpu: command line parameter: -XX:+UseParallel GC. In the Cenozoic, parallel cleanup collection strategy is used, and in the Old Cenozoic, it is the same as the default collector.
Applicability: a. Programs with more than two CPUs, b. Programs with more temporary objects
- XX: Parallel GCThreads collects the number of threads in parallel, preferably concurrent low pause collector: command line parameter: -XX:+UseConcMarkSweepGC. In the Old Cenozoic era, concurrent collection strategy was used. Most of the collection work was done concurrently with applications. When collecting, the pause time of applications was very short. By default, Open - XX:+UseParNewGC will use parallel replication collection in the new generation.
Applicability: a. Programs with more CPUs, b. Programs with more old objects

- XX:+UseCMSCompact AtFull Collection: Open compression for older generations. Performance may be affected, but debris can be eliminated  
- Rapid optimization of the original type of XX:+UseFast Accessor Methods  
- XX: The proportion of Eden & survivor in SurvivorRatio New Area is set to 8  
- XX: Target SurvivorRatio Survival Area needs to do garbage collection ratio, default is 50%. Setting higher can make better use of this area.


Setting jvm parameters

$ java -jar -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=128m -Xms1024m -Xmx1024m -Xmn256m -Xss256k -XX:SurvivorRatio=8 -XX:+UseConcMarkSweepGC newframe-1.0.0.jar




Tuning strategy

There is a balance between the parameters of JVM, in addition, according to the type of business and the actual operating environment, there are different effects. However, some conventional provisions are worth learning from:
1) Heap memory does not exceed 3/4 of physical memory
2) The younger generation does not exceed 3/8 of heap memory.
3) CMSInitiating Occupancy Fraction is not less than 70% in general.


Reference parameters:

xms xmn survivor xmn gc
6G 6G 4 2G CMSInitiatingOccupancyFraction=70
6G 6G 3 1.75G CMSInitiatingOccupancyFraction=70

Tool use

View Thread cpu Occupancy 1

[root@localhost ~]# ps p 6515 -L -o pcpu,pid,tid,time,tname,cmd --sort pcpu  
%CPU   PID   TID     TIME TTY      CMD  
 0.0  6515  6515 00:00:00 pts/1    java -jar xxl-job-admin-2.0.2-SNAPSHOT.jar  
 0.0  6515  6523 00:00:04 pts/1    java -jar xxl-job-admin-2.0.2-SNAPSHOT.jar  
 0.0  6515  6529 00:00:00 pts/1    java -jar xxl-job-admin-2.0.2-SNAPSHOT.jar


View Thread cpu Occupancy 2

[root@localhost ~]# ps -mp 6515 -o THREAD,tid,time  
root      0.3   -    - -         -      -     - 00:00:58  
root      0.0  19    - futex_    -      -  6515 00:00:00  
root      0.0  19    - futex_    -      -  6523 00:00:04  
root      0.0  19    - futex_    -      -  6529 00:00:00  
root      0.0  19    - futex_    -      -  6530 00:00:00


The thread ID found is converted to hexadecimal format

[root@localhost ~]# printf "%x\n" 6523  


Print thread stack information

[root@localhost ~]# jstack 6515 | grep 197b -A 30  
"DestroyJavaVM" #94 prio=5 os_prio=0 tid=0x00007f83f004b000 nid=0x197b waiting on condition [0x0000000000000000]  
   java.lang.Thread.State: RUNNABLE

"http-nio-9988-AsyncTimeout" #92 daemon prio=5 os_prio=0 tid=0x00007f83f0758000 nid=0x19e4 waiting on condition [0x00007f8352bee000]  
   java.lang.Thread.State: TIMED_WAITING (sleeping)  
    at java.lang.Thread.sleep(Native Method)  
    at org.apache.coyote.AbstractProtocol$AsyncTimeout.run(AbstractProtocol.java:1149)  
    at java.lang.Thread.run(Thread.java:748)

"http-nio-9988-Acceptor-0" #91 daemon prio=5 os_prio=0 tid=0x00007f83f0382800 nid=0x19e3 runnable [0x00007f8352cef000]  
   java.lang.Thread.State: RUNNABLE  
    at sun.nio.ch.ServerSocketChannelImpl.accept0(Native Method)  
    at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:422)  
    at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:250)  
    - locked <0x00000006c7c2ada0> (a java.lang.Object)  
    at org.apache.tomcat.util.net.NioEndpoint$Acceptor.run(NioEndpoint.java:482)  
    at java.lang.Thread.run(Thread.java:748)

"http-nio-9988-ClientPoller-1" #90 daemon prio=5 os_prio=0 tid=0x00007f83f11d1000 nid=0x19e2 runnable [0x00007f8352df0000]  
   java.lang.Thread.State: RUNNABLE  
    at sun.nio.ch.EPollArrayWrapper.epollWait(Native Method)  
    at sun.nio.ch.EPollArrayWrapper.poll(EPollArrayWrapper.java:269)  
    at sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:93)  
    at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)  
    - locked <0x00000006c7c2b548> (a sun.nio.ch.Util$3)  
    - locked <0x00000006c7c2b538> (a java.util.Collections$UnmodifiableSet)  
    - locked <0x00000006c7c2b400> (a sun.nio.ch.EPollSelectorImpl)  
    at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)  
    at org.apache.tomcat.util.net.NioEndpoint$Poller.run(NioEndpoint.java:825)  
    at java.lang.Thread.run(Thread.java:748)


Execute jmap to dump the current heap

jmap -dump:live,format=b,file=dump.hprof 6515


Open the dump.hprof file using Visual VM


SpringBoot TOMCAT tuning

Description of tomcat parameters

Maximum number of threads requested by maxThreads customers   
Number of socket threads created during minSpareThreads Tomcat initialization   
Maximum number of free socket threads for maxSpareThreads Tomcat connectors   
If enableLookups is set to true, domain name resolution is supported, and ip address can be resolved as host name.   
RedirectPort forwards customer requests to the redirectPort port based on SSL when security channel is needed   
AceptAccount listens on the maximum number of port queues, and when they are full, customer requests are rejected (not less than maxSpareThreads)   
Connection Timeout connection timeout   
Minimum number of processing threads at minProcessors server creation   
Maximum number of threads processed simultaneously by maxProcessors server   
Unified Coding of URIEncoding URL s


tomcat configures ServerProperties. Java - > application. properties on spring boot

server.tomcat.accesslog.enabled =  
server.tomcat.accesslog.pattern =  
server.tomcat.accesslog.directory =  
server.tomcat.accesslog.prefix =  
server.tomcat.accesslog.suffix =  
server.tomcat.accesslog.rotate =  
server.tomcat.accesslog.renameOnRotate =  
server.tomcat.accesslog.buffered =  
server.tomcat.internalProxies =  
server.tomcat.protocolHeader =  
server.tomcat.protocolHeaderHttpsValue =  
server.tomcat.portHeader =  
server.tomcat.basedir =  
server.tomcat.backgroundProcessorDelay =  
server.tomcat.maxThreads =  
server.tomcat.minSpareThreads =  
server.tomcat.maxHttpPostSize =  
server.tomcat.maxHttpHeaderSize =  
server.tomcat.redirectContextRoot =  
server.tomcat.uriEncoding =  
server.tomcat.maxConnections =  
server.tomcat.acceptCount =  
server.tomcat.additionalTldSkipPatterns =


Configuration of tomcat on application.yml

    min-spare-threads: 20  
    max-threads: 100  
  connection-timeout: 5000


Reference resources:
1.JAVAEE documents: https://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html
2. Deep understanding of JAVA virtual machine (memory model + GC algorithm + JVM tuning): https://www.cnblogs.com/yueshutong/p/9768298.html
3.JVM tuning practices: https://blog.wangqi.love/articles/Java/JVM%E8%B0%83%E4%BC%98%E5%AE%9E%E8%B7%B5.html
4.JVM Interpretation-Performance Tuning Example: https://www.jianshu.com/p/2c4b091deaa3
5. Expansion? First try JVM tuning: https://sq.163yun.com/blog/article/211688346067283968
6. Introduction of JVM tuning parameters, tuning objectives and tuning experience https://blog.csdn.net/jisuanjiguoba/article/details/80176223
7.jvm Optimizing Knowledgeable Series - Monitoring Tools https://juejin.im/post/59e6c1f26fb9a0451c397a8c
8.JVM Live Learning and Flexible Use-Optimizing springboot https://cloud.tencent.com/developer/article/1089954
9. SpringBook Server Comparisons (jetty, tomcat, undertow) https://my.oschina.net/shyloveliyi/blog/2980868

Tags: Tomcat Java jvm snapshot

Posted on Wed, 04 Sep 2019 21:12:11 -0700 by jkkenzie