小言_互联网的博客

JVM 调优命令&工具使用

507人阅读  评论(0)

 

top命令 查看进程占用资源情况

jps 命令 查看 java进程

jstack 命令  关注 WATTING   查看死锁问题

jstat -gc pid  查看 GC 情况

 jinfo  pid 查看 jvm 常用信息

arthas  [推荐]

安装&启动

使用 jmap 命令 [生产环境慎用! 会造成生产环境卡顿!!!!]

使用jvisualvm 进行查看 dump 文件[试验用]

 


 

调优,从规划开始

  • 调优,从业务场景开始,没有业务场景的调优都是耍流氓

  • 无监控(压力测试,能看到结果),不调优

  • 步骤:

    1. 熟悉业务场景(没有最好的垃圾回收器,只有最合适的垃圾回收器)
      1. 响应时间、停顿时间 [CMS G1 ZGC] (需要给用户作响应)
      2. 吞吐量 = 用户时间 /( 用户时间 + GC时间) [PS]
    2. 选择回收器组合
    3. 计算内存需求(经验值 1.5G 16G)
    4. 选定CPU(越高越好)
    5. 设定年代大小、升级年龄
    6. 设定日志参数
      1. -Xloggc:/opt/xxx/logs/xxx-xxx-gc-%t.log -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=20M -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCCause
      2. 或者每天产生一个日志文件
    7. 观察日志情况

 

top命令 查看进程占用资源情况


  
  1. Processes: 393 total, 2 running, 391 sleeping, 1727 threads 10:26:14
  2. Load Avg: 3.60, 3.03, 2.80 CPU usage: 21.24% user, 14.8% sys, 64.67% idle SharedLibs: 217M resident, 60M data, 39M linkedit.
  3. MemRegions: 111000 total, 5331M resident, 158M private, 1972M shared. PhysMem: 16G used ( 2420M wired), 480M unused.
  4. VM: 2005G vsize, 1297M framework vsize, 725056( 0) swapins, 845477( 0) swapouts. Networks: packets: 7278266/ 11G in, 5756934/ 4837M out.
  5. Disks: 1039370/ 15G read, 2239416/ 30G written.
  6. PID COMMAND %CPU TIME #TH #WQ #PORT MEM PURG CMPRS PGRP PPID STATE BOOSTS %CPU_ME %CPU_OTHRS UID
  7. 6738 top 3.9 00: 00.70 1/ 1 0 25 4880K+ 0B 0B 6738 4188 running * 0[ 1] 0.00000 0.00000 0
  8. 6736 CFNetworkAge 0.0 00: 00.10 3 2 43 10M 0B 0B 6736 1 sleeping * 0[ 1] 0.00000 0.00000 0
  9. 6734 ocspd 0.0 00: 00.02 2 1 31 1636K 0B 0B 6734 1 sleeping * 0[ 1] 0.00000 0.00000 0
  10. 6731 backupd 0.0 00: 00.04 2 1 42 1612K 0B 0B 6731 1 sleeping * 0[ 1] 0.00000 0.00000 0
  11. 6730 VTDecoderXPC 0.0 00: 00.55 2 1 50 5288K 0B 0B 6730 1 sleeping 0[ 43] 0.00000 0.00000 501
  12. 6710 CoreServices 0.0 00: 00.20 3 1 164 4288K 0B 0B 6710 1 sleeping * 0[ 1] 0.00000 0.00000 501
  13. 6708 Google Chrom 0.0 00: 00.14 12 1 100 14M 4096B 0B 737 737 sleeping * 0[ 7] 0.00000 0.00000 501
  14. 6707 Google Chrom 0.0 00: 00.52 12 1 142 25M 4096B 0B 737 737 sleeping * 0[ 5] 0.00000 0.00000 501
  15. 6705 Google Chrom 0.0 00: 00.37 12 1 134 18M 4096B 0B 737 737 sleeping * 0[ 6] 0.00000 0.00000 501
  16. 6703 Google Chrom 0.0 00: 04.29 13 1 155 72M 4096B 0B 737 737 sleeping * 0[ 5] 0.00000 0.00000 501

 

jps 命令 查看 java进程


  
  1. MacBook- Pro:~ sysadmin$ jps
  2. 6613 Launcher
  3. 6605
  4. 6733 Jps

 

jstack 命令  关注 WATTING   查看死锁问题


  
  1. MacBook-Pro:~ sysadmin$ jstack 6613
  2. 2020- 02- 10 10: 24: 35
  3. Full thread dump Java HotSpot(TM) 64-Bit Server VM ( 25.161-b12 mixed mode):
  4. "Attach Listener" #13 daemon prio=9 os_prio=31 tid=0x00007fb2d1869000 nid=0xb07 waiting on condition [0x0000000000000000]
  5. java.lang.Thread.State: RUNNABLE
  6. "DestroyJavaVM" #12 prio=5 os_prio=31 tid=0x00007fb2cf2ea000 nid=0x2703 waiting on condition [0x0000000000000000]
  7. java.lang.Thread.State: RUNNABLE
  8. "JPS event loop" #9 prio=5 os_prio=31 tid=0x00007fb2d01de000 nid=0x4703 runnable [0x000070000bc6e000]
  9. java.lang.Thread.State: RUNNABLE
  10. at sun.nio.ch.KQueueArrayWrapper.kevent 0(Native Method)
  11. at sun.nio.ch.KQueueArrayWrapper.poll(KQueueArrayWrapper.java: 198)
  12. at sun.nio.ch.KQueueSelectorImpl.doSelect(KQueueSelectorImpl.java: 117)
  13. at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java: 86)
  14. - locked < 0x00000007b57

 

jstat -gc pid  查看 GC 情况


  
  1. MacBook- Pro:~ sysadmin$ jstat -gc 6613
  2. S0C S1C S0U S1U EC EU OC OU MC MU CCSC CCSU YGC YGCT FGC FGCT GCT
  3. 10752.0 10752.0 0. 0 4405.1 65536.0 26391.4 175104.0 152.0 17152.0 16563.7 2048.0 1933.2 1 0. 007 0 0. 000 0. 007

 

 jinfo  pid 查看 jvm 常用信息

 


  
  1. [root@henghe- 023 sbin]# jinfo 27519
  2. Attaching to process ID 27519, please wait...
  3. Debugger attached successfully.
  4. Server compiler detected.
  5. JVM version is 25.151-b12
  6. Java System Properties:
  7. java.runtime. name = Java(TM) SE Runtime Environment
  8. java.vm.version = 25.151-b12
  9. sun.boot. library.path = /opt/jdk1. 8/jre/lib/amd64
  10. hadoop.root.logger = INFO,RFA
  11. java.vendor.url = http: //java.oracle.com/
  12. java.vm.vendor = Oracle Corporation
  13. path.separator = :
  14. file.encoding.pkg = sun.io
  15. java.vm. name = Java HotSpot(TM) 64-Bit Server VM
  16. sun.os.patch.level = unknown
  17. sun.java.launcher = SUN_STANDARD
  18. user.country = CN
  19. user.dir = /opt/hadoop- 2.7. 4
  20. java.vm.specification. name = Java Virtual Machine Specification
  21. hdfs.audit.logger = INFO,NullAppender
  22. java.runtime.version = 1.8. 0_151-b12
  23. java.awt.graphicsenv = sun.awt.X11GraphicsEnvironment
  24. os.arch = amd64
  25. java.endorsed.dirs = /opt/jdk1. 8/jre/lib/endorsed
  26. line.separator =
  27. java.io.tmpdir = /tmp
  28. hadoop.log. file = hadoop-root-namenode-henghe- 023.log
  29. java.vm.specification.vendor = Oracle Corporation
  30. os. name = Linux
  31. hadoop.id.str = root
  32. sun.jnu.encoding = UTF- 8
  33. java. library.path = /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
  34. hadoop.home.dir = /opt/hadoop- 2.7. 4
  35. java.specification. name = Java Platform API Specification
  36. java. class.version = 52.0
  37. java.net.preferIPv4Stack = true
  38. proc_namenode =
  39. sun.management.compiler = HotSpot 64-Bit Tiered Compilers
  40. os.version = 3.10. 0- 1062.4. 3.el7.x86_64
  41. user.home = /root
  42. user.timezone = Asia/Shanghai
  43. java.awt.printerjob = sun.print.PSPrinterJob
  44. file.encoding = UTF- 8
  45. java.specification.version = 1.8
  46. user. name = root
  47. java. class.path = /opt/hadoop- 2.7. 4/etc/hadoop:************.jar
  48. java.vm.specification.version = 1.8
  49. sun.arch.data.model = 64
  50. sun.java.command = org.apache.hadoop.hdfs.server.namenode.NameNode
  51. java.home = /opt/jdk1. 8/jre
  52. user.language = zh
  53. java.specification.vendor = Oracle Corporation
  54. hadoop.security.logger = INFO,RFAS
  55. awt.toolkit = sun.awt.X11.XToolkit
  56. java.vm.info = mixed mode
  57. hadoop.log.dir = /opt/hadoop- 2.7. 4/logs
  58. java.version = 1.8. 0_151
  59. java.ext.dirs = /opt/jdk1. 8/jre/lib/ext:/usr/java/packages/lib/ext
  60. sun.boot. class.path = /opt/jdk1. 8/jre/lib/resources.jar:/opt/jdk1. 8/jre/lib/rt.jar:/opt/jdk1. 8/jre/lib/sunrsasign.jar:/opt/jdk1. 8/jre/lib/jsse.jar:/opt/jdk1. 8/jre/lib/jce.jar:/opt/jdk1. 8/jre/lib/charsets.jar:/opt/jdk1. 8/jre/lib/jfr.jar:/opt/jdk1. 8/jre/classes
  61. java.vendor = Oracle Corporation
  62. file.separator = /
  63. java.vendor.url.bug = http: //bugreport.sun.com/bugreport/
  64. sun.io.unicode.encoding = UnicodeLittle
  65. sun.cpu.endian = little
  66. hadoop.policy. file = hadoop-policy.xml
  67. sun.cpu.isalist =
  68. VM Flags:
  69. Non- default VM flags: -XX:CICompilerCount= 3 -XX:InitialHeapSize= 9995026432 -XX:MaxHeapSize= 9995026432 -XX:MaxNewSize= 1073741824 -XX:MinHeapDeltaBytes= 524288 -XX:NewSize= 1073741824 -XX:OldSize= 8921284608 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseFastUnorderedTimeStamps -XX:+UseParallelGC
  70. Command line: -Dproc_namenode -Xmx1000m -Djava.net.preferIPv4Stack=true -Dhadoop.log.dir=/opt/hadoop- 2.7. 4/logs -Dhadoop.log. file=hadoop.log -Dhadoop.home.dir=/opt/hadoop- 2.7. 4 -Dhadoop.id.str=root -Dhadoop.root.logger=INFO,console -Dhadoop.policy. file=hadoop-policy.xml -Djava.net.preferIPv4Stack=true -Djava.net.preferIPv4Stack=true -Djava.net.preferIPv4Stack=true -Dhadoop.log.dir=/opt/hadoop- 2.7. 4/logs -Dhadoop.log. file=hadoop-root-namenode-henghe- 023.log -Dhadoop.home.dir=/opt/hadoop- 2.7. 4 -Dhadoop.id.str=root -Dhadoop.root.logger=INFO,RFA -Dhadoop.policy. file=hadoop-policy.xml -Djava.net.preferIPv4Stack=true -Xmx9531m -Xms9531m -Xmn1024m -Dhadoop.security.logger=INFO,RFAS -Dhdfs.audit.logger=INFO,NullAppender -Xmx9531m -Xms9531m -Xmn1024m -Dhadoop.security.logger=INFO,RFAS -Dhdfs.audit.logger=INFO,NullAppender -Xmx9531m -Xms9531m -Xmn1024m -Dhadoop.security.logger=INFO,RFAS -Dhdfs.audit.logger=INFO,NullAppender -Dhadoop.security.logger=INFO,RFAS

 

arthas  [推荐]

Arthas 是Alibaba开源的Java诊断工具.

 

安装&启动


  
  1. curl -O https: //alibaba.github.io/arthas/arthas-boot.jar
  2. java -jar arthas-boot.jar

 


  
  1. [root@henghe -023 data] # java -jar arthas-boot.jar
  2. [INFO] arthas-boot version: 3.1 .7
  3. [INFO] Found existing java process, please choose one and hit RETURN.
  4. * [ 1]: 28443 org.apache.hadoop.hdfs.server.datanode.DataNode
  5. [ 2]: 27519 org.apache.hadoop.hdfs.server.namenode.NameNode
  6. 2
  7. [INFO] arthas home: /root/.arthas/lib/ 3.1 .7/arthas
  8. [INFO] Try to attach process 27519
  9. [INFO] Attach process 27519 success.
  10. [INFO] arthas-client connect 127.0 .0 .1 3658
  11. ,---. ,------. ,--------.,--. ,--. ,---. ,---.
  12. / O \ | .--. ''--. .-- '| '-- ' | / O \ ' .- '
  13. | .-. || '-- '.' | | | .--. || .-. |`. `-.
  14. | | | || |\ \ | | | | | || | | |.- ' |
  15. `--' `-- '`--' '--' `-- ' `--' `-- '`--' `-- '`-----'
  16. wiki https: //alibaba.github.io/arthas
  17. tutorials https: //alibaba.github.io/arthas/arthas-tutorials
  18. version 3.1 .7
  19. pid 28443
  20. time 2020 -02 -10 10: 46: 37
  21. [arthas@ 28443]$

安装/使用参考官方文档: https://alibaba.github.io/arthas/install-detail.html

 


  
  1. [arthas@ 14804]$ dashboard
  2. ID NAME GROUP PRIORITY STATE %CPU TIME INTERRUPT DAEMON
  3. 20 pool -1-thread -12 main 5 WAITING 4 0: 0 false false
  4. 27 pool -1-thread -19 main 5 WAITING 4 0: 0 false false
  5. 10 pool -1-thread -2 main 5 WAITING 3 0: 0 false false
  6. 50 pool -1-thread -42 main 5 WAITING 3 0: 0 false false
  7. 19 pool -1-thread -11 main 5 WAITING 2 0: 0 false false
  8. 22 pool -1-thread -14 main 5 WAITING 2 0: 0 false false
  9. 24 pool -1-thread -16 main 5 WAITING 2 0: 0 false false
  10. 25 pool -1-thread -17 main 5 WAITING 2 0: 0 false false
  11. 28 pool -1-thread -20 main 5 WAITING 2 0: 0 false false
  12. 33 pool -1-thread -25 main 5 WAITING 2 0: 0 false false
  13. 39 pool -1-thread -31 main 5 WAITING 2 0: 0 false false
  14. 41 pool -1-thread -33 main 5 WAITING 2 0: 0 false false
  15. 44 pool -1-thread -36 main 5 WAITING 2 0: 0 false false
  16. 45 pool -1-thread -37 main 5 WAITING 2 0: 0 false false
  17. 54 pool -1-thread -46 main 5 WAITING 2 0: 0 false false
  18. 57 pool -1-thread -49 main 5 WAITING 2 0: 0 false false
  19. 13 pool -1-thread -5 main 5 WAITING 2 0: 0 false false
  20. 58 pool -1-thread -50 main 5 WAITING 2 0: 0 false false
  21. Memory used total max usage GC
  22. heap 63M 89M 89M 71.18% gc.ps_scavenge.count 17
  23. ps_eden_space 5M 11M 11M 51.41% gc.ps_scavenge. time(ms) 232
  24. ps_survivor_space 3M 11M 11M 28.69% gc.ps_marksweep.count 0
  25. ps_old_gen 54M 67M 67M 81.52% gc.ps_marksweep. time(ms) 0
  26. nonheap 19M 20M -1 95.94%
  27. code_cache 4M 4M 240M 1.79%
  28. metaspace 13M 14M -1 97.40%
  29. compressed_class_space 1M 1M 1024M 0.16%
  30. Runtime
  31. os.name Linux
  32. os.version 3.10 .0 -1062.4 .3.el7.x86_64
  33. java.version 1.8 .0_151
  34. java.home /opt/jdk1 .8/jre
  35. systemload.average 0.60
  36. processors 6
  37. uptime 182s
  38. ID NAME GROUP PRIORITY STATE %CPU TIME INTERRUPT DAEMON
  39. 57 pool -1-thread -49 main 5 WAITING 8 0: 0 false false
  40. 10 pool -1-thread -2 main 5 WAITING 5 0: 0 false false
  41. 22 pool -1-thread -14 main 5 TIMED_WAI 3 0: 0 false false
  42. 45 pool -1-thread -37 main 5 WAITING 2 0: 0 false false
  43. 54 pool -1-thread -46 main 5 WAITING 2 0: 0 false false
  44. 16 pool -1-thread -8 main 5 WAITING 2 0: 0 false false
  45. 73 Timer- for-arthas-dashboard -6 system 10 RUNNABLE 1 0: 0 false true
  46. 9 pool -1-thread -1 main 5 WAITING 1 0: 0 false false
  47. 18 pool -1-thread -10 main 5 WAITING 1 0: 0 false false
  48. 19 pool -1-thread -11 main 5 WAITING 1 0: 0 false false
  49. 20 pool -1-thread -12 main 5 WAITING 1 0: 0 false false
  50. 21 pool -1-thread -13 main 5 WAITING 1 0: 0 false false
  51. 23 pool -1-thread -15 main 5 WAITING 1 0: 0 false false
  52. 24 pool -1-thread -16 main 5 WAITING 1 0: 0 false false
  53. 25 pool -1-thread -17 main 5 WAITING 1 0: 0 false false
  54. 26 pool -1-thread -18 main 5 WAITING 1 0: 0 false false
  55. 27 pool -1-thread -19 main 5 WAITING 1 0: 0 false false
  56. 28 pool -1-thread -20 main 5 WAITING 1 0: 0 false false
  57. Memory used total max usage GC
  58. heap 68M 89M 89M 76.85% gc.ps_scavenge.count 17
  59. ps_eden_space 10M 11M 11M 96.60% gc.ps_scavenge. time(ms) 232
  60. ps_survivor_space 3M 11M 11M 28.69% gc.ps_marksweep.count 0
  61. ps_old_gen 54M 67M 67M 81.52% gc.ps_marksweep. time(ms) 0
  62. nonheap 20M 21M -1 95.65%
  63. code_cache 4M 4M 240M 1.87%
  64. metaspace 13M 14M -1 95.72%
  65. compressed_class_space 1M 1M 1024M 0.17%
  66. Runtime
  67. os.name Linux
  68. os.version 3.10 .0 -1062.4 .3.el7.x86_64
  69. java.version 1.8 .0_151
  70. java.home /opt/jdk1 .8/jre
  71. systemload.average 0.55
  72. processors 6
  73. uptime 187s

 

使用 jmap 命令 [生产环境慎用! 会造成生产环境卡顿!!!!]

jmap -histo  $PID   | head -50          查看类对象

示例:


  
  1. [root@henghe- 023 arthas] # jmap -histo 23893 | head -50
  2. num #instances #bytes class name
  3. ----------------------------------------------
  4. 1: 381601 27475272 java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask
  5. 2: 506262 16200384 java.util.concurrent.locks.AbstractQueuedSynchronizer$Node
  6. 3: 381626 15265040 java.math.BigDecimal
  7. 4: 381600 12211200 com.zl.gc.T15_FullGC_Problem01$CardInfo
  8. 5: 381612 9158688 java.util.Date
  9. 6: 381602 9158448 java.util.concurrent.Executors$RunnableAdapter
  10. 7: 381600 6105600 com.zl.gc.T15_FullGC_Problem01$$Lambda$2/ 245257410
  11. 8: 131012 4192384 java.util.HashMap$Node
  12. 9: 2470 4135208 [B
  13. 10: 36531 2892152 [Ljava.lang.Object;
  14. 11: 3 2662808 [Ljava.util.concurrent.RunnableScheduledFuture;
  15. 12: 53790 2048256 [C
  16. 13: 14342 1669984 [I
  17. 14: 53552 1285248 java.lang.String
  18. 15: 2355 1134592 [Ljava.util.HashMap$Node;
  19. 16: 33494 803856 java.util.ArrayList
  20. 17: 31471 755304 io.termd.core.term.Feature
  21. 18: 30539 488624 io.termd.core.term.OpCode$Literal
  22. 19: 26476 423616 io.termd.core.term.Sequence
  23. 20: 2710 306568 java.lang.Class
  24. 21: 5178 207120 java.util.LinkedHashMap$Entry
  25. 22: 1575 138600 java.lang.reflect.Method
  26. 23: 156 102336 io.netty.util.internal.shaded.org.jctools.queues.MpscArrayQueue
  27. 24: 28 98816 [Ljava.nio.channels.SelectionKey;
  28. 25: 1887 90576 java.util.HashMap
  29. 26: 2700 86400 com.taobao.text.Style$Composite
  30. 27: 2647 84704 io.termd.core.term.OpCode$Printf
  31. 28: 4643 74288 io.termd.core.term.OpCode$PushParam
  32. 29: 1629 52128 io.termd.core.term.Device
  33. 30: 1795 43064 [Ljava.lang.Class;
  34. 31: 1314 42048 io.termd.core.term.Capability
  35. 32: 443 40336 [Ljava.lang.String;
  36. 33: 1169 37408 io.termd.core.term.TermInfoBuilder$Entry
  37. 34: 645 36120 java.util.LinkedHashMap
  38. 35: 475 34200 java.lang.reflect.Field
  39. 36: 1397 33528 io.termd.core.term.OpCode$PushConstant
  40. 37: 1327 31848 io.termd.core.term.OpCode$If
  41. 38: 1327 31848 io.termd.core.term.OpCode$Then
  42. 39: 492 27552 java.util.zip.ZipFile$ZipFileInflaterInputStream
  43. 40: 492 27552 java.util.zip.ZipFile$ZipFileInputStream
  44. 41: 48 27264 io.netty.util.internal.shaded.org.jctools.queues.MpscUnboundedArrayQueue
  45. 42: 667 26680 java.util.ArrayList$SubList
  46. 43: 819 26208 java.util.concurrent.ConcurrentHashMap$Node
  47. 44: 612 24480 java.lang.ref.Finalizer
  48. 45: 59 22184 java.lang.Thread
  49. 46: 10 17120 [Lio.netty.buffer.PoolSubpage;
  50. 47: 383 15320 java.lang.ref.SoftReference
  51. [root@henghe- 023 arthas] #

jmap -dump:format=b,file=20200210.hprof   $PID         将内存信息生成文件,进行分析 [jhat, jvisualvm,mat]


  
  1. [root@henghe- 023 arthas]# jmap -dump:format=b, file= 20200210.hprof 23893
  2. Dumping heap to /data/arthas/ 20200210.hprof ...
  3. Heap dump file created

 

使用jvisualvm 进行查看 dump 文件[试验用]

/Library/java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/bin/jvisualvm

 

JVM 日志设置

-Xloggc:/opt/xxx/logs/xxx-xxx-gc-%t.log -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=20M -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCCause

 


  
  1. -Xloggc: /opt/xxx/logs/xxx-gc-%t.log // GC 文件路径
  2. -XX:+UseGCLogFileRotation // 启用 GC日志滚动记录功能
  3. -XX:NumberOfGCLogFiles= 5 // 生成 GC 文件数量
  4. -XX:GCLogFileSize= 20M // 单个 GC 文件大小
  5. -XX:+PrintGCDetails // 打印 GC 详情
  6. -XX:+PrintGCDateStamps // 打印 GC 时间戳
  7. -XX:+PrintGCCause // 打印 GC 产生原因

 


 


 

对象创建流程

 

 

CMS GC 过程
 

初始标记和重新标记要 触发 STW

 

 

 

 

 

 

 

 

 


转载:https://blog.csdn.net/zhanglong_4444/article/details/104242276
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场