当前位置:网站首页>JVM tuning and finishing

JVM tuning and finishing

2022-08-11 06:33:00 cjx__

JVM 收集器

  1. The serial collector is used by default, 单个cpu时适用

  2. Throughput collector(throughput collector):命令行参数:-XX:+UseParallelGC.Use a parallel cleanup collection strategy in the young generation,Same as default collector in old generation.
    适用:a、拥有2个以上cpu, b、Programs with many temporary objects
    -XX:ParallelGCThreads Number of parallel collection threads,最好和cpu数量相当

  3. 并发收集器(concurrent low pause collector):命令行参数:-XX:+UseConcMarkSweepGC.Use concurrent collection strategy in old generation,Most of the collection work happens concurrently with the application,when collecting,The app's pause time is short.Open by default -XX:+UseParNewGC,A parallel copy collection is used in the young generation.
    适用:a、拥有多个cpu, b、Programs with more old objects

-XX:+UseCMSCompactAtFullCollection:打开对年老代的压缩.可能会影响性能,但是可以消除碎片
-XX:+UseFastAccessorMethods 原始类型的快速优化
-XX:SurvivorRatio 新生区中,eden&survivor的比例,设置为8
-XX:TargetSurvivorRatio The proportion of the survival area that needs to be garbage collected,默认为50%,Set higher to make better use of this area
  • 设置jvm参数
$ java -jar -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=128m -Xms1024m -Xmx1024m -Xmn256m -Xss256k -XX:SurvivorRatio=8 -XX:+UseConcMarkSweepGC newframe-1.0.0.jar
$JAVA_ARGS
.=
"
-Dresin.home=$SERVER_ROOT
-server
-Xmx3000M
-Xms3000M
-Xmn600M
-XX:PermSize=500M
-XX:MaxPermSize=500M
-Xss256K
-XX:+DisableExplicitGC
-XX:SurvivorRatio=1
-XX:+UseConcMarkSweepGC
-XX:+UseParNewGC
-XX:+CMSParallelRemarkEnabled
-XX:+UseCMSCompactAtFullCollection
-XX:CMSFullGCsBeforeCompaction=0
-XX:+CMSClassUnloadingEnabled
-XX:LargePageSizeInBytes=128M
-XX:+UseFastAccessorMethods
-XX:+UseCMSInitiatingOccupancyOnly
-XX:CMSInitiatingOccupancyFraction=70
-XX:SoftRefLRUPolicyMSPerMB=0
-XX:+PrintClassHistogram
-XX:+PrintGCDetails
-XX:+PrintGCTimeStamps
-XX:+PrintHeapAtGC
-Xloggc:log/gc.log
";

调优策略

JVMThere is a balance between the various parameters,In addition, there are different effects according to the business type and the actual operating environment.But some conventions are still worth learning from:
1)Heap memory does not exceed physical memory3/4 
2)The size of the young generation does not exceed the heap memory3/8 
3)CMSInitiatingOccupancyFraction一般不低于70% 

(Xmx-Xmn)*(1-CMSInitiatingOccupancyFraction/100)>=(Xmn-Xmn/(SurvivorRatior+2))
  • 参考参数:
xmsxmnsurvivorxmngc
6G6G42GCMSInitiatingOccupancyFraction=70
6G6G31.75GCMSInitiatingOccupancyFraction=70

工具使用

  • 查看线程cpu占用1
[[email protected] ~]# 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
  • 查看线程cpu占用2
[[email protected] ~]# ps -mp 6515 -o THREAD,tid,time
USER     %CPU PRI SCNT WCHAN  USER SYSTEM   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
  • 查看gc
root:/# jstat -gcutil 1 1000 5
  S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT     GCT
  0.00  18.57  22.58  42.71  93.54  90.85  22329  318.467     8    2.910  321.377
  0.00  18.57  41.78  42.71  93.54  90.85  22329  318.467     8    2.910  321.377
  0.00  18.57  62.83  42.71  93.54  90.85  22329  318.467     8    2.910  321.377
  • 查看tcp连接数量
root:/# netstat -nat | awk '{print $6}' | sort | uniq -c | sort -n
      1 Foreign
      1 LISTEN
      1 established)
      2 CLOSE_WAIT
     16 TIME_WAIT
     88 ESTABLISHED
  • 找到的线程ID转换为16进制格式
[[email protected] ~]# printf "%x\n" 6523
197b
  • 打印线程的堆栈信息
[[email protected] ~]# 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)
  • Analyze the thread's stack information
jstack 6515 > task.info
grep java.lang.Thread.State task.info |awk '{print $2$3$4$5}' |sort| uniq -c
  • 执行jmapput the current heapdump下来
jmap -dump:live,format=b,file=dump.hprof 6515
  • 将dump.hprof文件使用VisualVM来打开
C:\Users\Administrator>jvisualvm

The launcher has determined that the parent process has a console and will reuse it for its own console output.
Closing the console will result in termination of the running program.
Use '--console suppress' to suppress console output.
Use '--console new' to create a separate console window.

在这里插入图片描述

SpringBoot TOMCAT调优

  • tomcat参数说明
maxThreads 客户请求最大线程数 
minSpareThreads Tomcat初始化时创建的 socket 线程数 
maxSpareThreads Tomcat连接器的最大空闲 socket 线程数 
enableLookups 若设为true, 则支持域名解析,可把 ip 地址解析为主机名 
redirectPort 在需要基于安全通道的场合,把客户请求转发到基于SSL 的 redirectPort 端口 
acceptAccount 监听端口队列最大数,满了之后客户请求会被拒绝(不能小于maxSpareThreads ) 
connectionTimeout 连接超时 
minProcessors 服务器创建时的最小处理线程数 
maxProcessors 服务器同时最大处理线程数 
URIEncoding URL统一编码
  • tomcat在springboot上的配置ServerProperties.java --> application.properties
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.requestAttributesEnabled=
server.tomcat.accesslog.buffered =
server.tomcat.internalProxies =
server.tomcat.protocolHeader =
server.tomcat.protocolHeaderHttpsValue =
server.tomcat.portHeader =
server.tomcat.remoteIpHeader=
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 =
  • tomcat在application.yml上的配置
server:
  tomcat:
    min-spare-threads: 20
    max-threads: 100
  connection-timeout: 5000

其它方法

  • 查看CPU信息 cat /proc/cpuinfo
  • 查看内存信息 cat /proc/meminfo
  • 查看Java线程数 ps -eLf | grep java -c
  • 查看linux系统里打开文件描述符的最大值 ulimit -u
  • found in the logTOP10的异常:grep ‘Exception’ /home/admin/logs/XX.log |awk -F':|,’ ‘{print $2}’|sort |uniq -c |sort -nr|head -10,You can use it after you find it-A 2 -B 2,Look at the front of the positioning log2line and the next two lines.
原网站

版权声明
本文为[cjx__]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/223/202208110515158829.html