栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【JVM系列】JVM调优

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【JVM系列】JVM调优

talk is cheap, show me the dry cargo.

JVM调优工具详解
Softwareversion
OpenJDK1.8
Jps常用命令(Java Virtual Machine Process Status Tool)
  • jps -help

    root@CN02:/home/demo-server# jps -help
    usage: jps [-help]
           jps [-q] [-mlvV] []
    
    Definitions:
        :      [:]
    
  • jps -mlv

    root@CN02:/home/demo-server# jps -mlv
    16082 demo-uat.jar --spring.profiles.active=uat -XX:+UseConcMarkSweepGC -Xmn512m -Xms768m -Xmx768m -Duser.timezone=GMT+08
    14339 sun.tools.jps.Jps -mlv -Dapplication.home=/usr/lib/jvm/java-8-openjdk-amd64 -Xms8m
    15509 demo-test.jar --spring.profiles.active=test -XX:+UseConcMarkSweepGC -Xmn512m -Xms768m -Xmx768m -Duser.timezone=GMT+08
    
Jstat常用命令(JVM统计监测工具)
  • jstat -help

    root@CN02:/home/demo-server# jstat -help
    Usage: jstat -help|-options
           jstat -
  • jstat -options

    root@CN02:/home/demo-server# jstat -options
    -class
    -compiler
    -gc
    -gccapacity
    -gccause
    -gcmetacapacity
    -gcnew
    -gcnewcapacity
    -gcold
    -gcoldcapacity
    -gcutil
    -printcompilation
    
  • jstat -gcutil [ []]

    root@CN02:/home/demo-server# jstat -gcutil 15509 1000 5
      S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT     GCT   
      0.00   0.00   1.73  14.61  92.80  90.76     16    1.117     9    0.888    2.006
      0.00   0.00   1.73  14.61  92.80  90.76     16    1.117     9    0.888    2.006
      0.00   0.00   1.73  14.61  92.80  90.76     16    1.117     9    0.888    2.006
      0.00   0.00   1.73  14.61  92.80  90.76     16    1.117     9    0.888    2.006
      0.00   0.00   1.73  14.61  92.80  90.76     16    1.117     9    0.888    2.006
    
Jmap常用命令(Memory Map)
  • jmap -help

    root@CN02:/home/demo-server# jmap -help
    Usage:
        jmap [option] 
            (to connect to running process)
        jmap [option] 
            (to connect to a core file)
        jmap [option] [server_id@]
            (to connect to remote debug server)
    
    where 
  • jmap -histo:live | more

    root@CN02:/home/demo-server# jmap -histo:live 15509 | more
    
     num     #instances         #bytes  class name
    ----------------------------------------------
       1:        112649       10985672  [C
       2:         31705        2790040  java.lang.reflect.Method
       3:        112092        2690208  java.lang.String
       4:         73094        2339008  java.util.concurrent.ConcurrentHashMap$Node
       5:         17300        1911192  java.lang.Class
       6:         26357        1535776  [Ljava.lang.Object;
       7:          6531        1173184  [B
       8:          8364        1068264  [I
       9:         25101        1004040  java.util.LinkedHashMap$Entry
      10:          9878         804744  [Ljava.util.HashMap$Node;
      11:         39438         631008  java.lang.Object
      12:         18762         600384  java.util.HashMap$Node
      13:           278         595344  [Ljava.util.concurrent.ConcurrentHashMap$Node;
      14:         10367         580552  java.util.LinkedHashMap
    

    class name列是对象类型,说明如下:

    B  byte
    C  char
    D  double
    F  float
    I  int
    J  long
    Z  boolean
    [  数组,如[I表示int[]
    [L+类名 其他对象
    
  • jmap -dump:live,format=b,file=heap.bin

    root@CN02:/home/demo-server# jmap -dump:live,format=b,file=heap.bin 15509
    Dumping heap to /home/demo-server/heap.bin ...
    Heap dump file created
    

    dump出来的文件可以用jhat命令、MAT、VisualVM等工具查看。

Jhat常用命令(Java Heap Analysis Tool)
  • jhat -help
    root@CN02:/home/demo-server# jhat -help
    Usage:  jhat [-stack ] [-refs ] [-port ] [-baseline ] [-debug ] [-version] [-h|-help] 
    
            -J          Pass  directly to the runtime system. For
                              example, -J-mx512m to use a maximum heap size of 512MB
            -stack false:     Turn off tracking object allocation call stack.
            -refs false:      Turn off tracking of references to objects
            -port :     Set the port for the HTTP server.  Defaults to 7000
            -exclude :  Specify a file that lists data members that should
                              be excluded from the reachableFrom query.
            -baseline : Specify a baseline object dump.  Objects in
                              both heap dumps with the same ID and same class will
                              be marked as not being "new".
            -debug :     Set debug level.
                                0:  No debug output
                                1:  Debug hprof file parsing
                                2:  Debug hprof file parsing, no server
            -version          Report version number
            -h|-help          Print this help and exit
                        The file to read
    
    For a dump file that contains multiple heap dumps,
    you may specify which dump in the file
    by appending "#" to the file name, i.e. "foo.hprof#3".
    
    All boolean options default to "true"
    
  • jhat -J-Xmx512m heap.bin
    root@CN02:/home/demo-server# jhat -J-Xmx512m heap.bin
    Reading from heap.bin...
    Dump file created Tue Jun 25 01:53:08 CST 2022
    Snapshot read, resolving...
    Resolving 2779596 objects...
    Chasing references, expect 555 dots................................................................................................................
    ...................................................................................................................................................
    ...................................................................................................................................................
    ...................................................................................................................................................
    ..
    Eliminating duplicate references...................................................................................................................
    ...................................................................................................................................................
    ...................................................................................................................................................
    ..................................................................................................................................................
    Snapshot resolved.
    Started HTTP server on port 7000
    Server is ready.
    
    浏览器访问:http://localhost:7000
Jstack常用命令
  • jstack -help
    root@CN02:/home/demo-server# jstack -help
    Usage:
        jstack [-l] 
            (to connect to running process)
        jstack -F [-m] [-l] 
            (to connect to a hung process)
        jstack [-m] [-l]  
            (to connect to a core file)
        jstack [-m] [-l] [server_id@]
            (to connect to a remote debug server)
    
    Options:
        -F  to force a thread dump. Use when jstack  does not respond (process is hung)
        -m  to print both java and native frames (mixed mode)
        -l  long listing. Prints additional information about locks
        -h or -help to print this help message
    
Jinfo常用命令
  • jinfo -help
    root@CN02:/home/demo-server# jinfo -help
    Usage:
        jinfo [option] 
            (to connect to running process)
        jinfo [option] 
            (to connect to a core file)
        jinfo [option] [server_id@]
            (to connect to remote debug server)
    
    where 
Jcmd常用命令
  • jcmd -help
    root@CN02:/home/demo-server# jcmd -help
    Usage: jcmd  
       or: jcmd -l                                                    
       or: jcmd -h                                                    
                                                                      
      command must be a valid jcmd command for the selected jvm.      
      Use the command "help" to see which commands are available.   
      If the pid is 0, commands will be sent to all Java processes.   
      The main class argument will be used to match (either partially 
      or fully) the class used to start Java.                         
      If no options are given, lists Java processes (same as -p).     
                                                                      
      PerfCounter.print display the counters exposed by this process  
      -f  read and execute commands from the file                     
      -l  list JVM processes on the local machine                     
      -h  this help
    
  • jcmd -l
    root@CN02:/home/demo-server# jcmd -l
    16082 demo-uat.jar --spring.profiles.active=uat
    16420 sun.tools.jcmd.JCmd -l
    15509 demo-test.jar --spring.profiles.active=test
    
  • jcmd help
    root@CN02:/home/demo-server# jcmd 15509 help
    15509:
    The following commands are available:
    VM.unlock_commercial_features
    JFR.configure
    JFR.stop
    JFR.start
    JFR.dump
    JFR.check
    VM.native_memory
    ManagementAgent.stop
    ManagementAgent.start_local
    ManagementAgent.start
    VM.classloader_stats
    GC.rotate_log
    Thread.print
    GC.class_stats
    GC.class_histogram
    GC.heap_dump
    GC.finalizer_info
    GC.heap_info
    GC.run_finalization
    GC.run
    VM.uptime
    VM.dynlibs
    VM.flags
    VM.system_properties
    VM.command_line
    VM.version
    help
    
    For more information about a specific command use 'help '.
    
  • jcmd GC.heap_info
    root@CN02:/home/demo-server# jcmd 15509 GC.heap_info
    15509:
     par new generation   total 471872K, used 22827K [0x00000000d0000000, 0x00000000f0000000, 0x00000000f0000000)
      eden space 419456K,   5% used [0x00000000d0000000, 0x00000000d164ae48, 0x00000000e99a0000)
      from space 52416K,   0% used [0x00000000e99a0000, 0x00000000e99a0000, 0x00000000eccd0000)
      to   space 52416K,   0% used [0x00000000eccd0000, 0x00000000eccd0000, 0x00000000f0000000)
     concurrent mark-sweep generation total 262144K, used 38326K [0x00000000f0000000, 0x0000000100000000, 0x0000000100000000)
     Metaspace       used 86305K, capacity 92137K, committed 92944K, reserved 1130496K
      class space    used 10985K, capacity 11915K, committed 12100K, reserved 1048576K
    
Jconsole工具详解 Jvisualvm工具详解

https://github.com/oracle/visualvm

VisualVM is an All-in-One Java Troubleshooting Tool

MAT工具详解(Memory Analyzer Tool)

http://www.eclipse.org/mat/downloads.php

Arthas工具详解

源码:https://github.com/alibaba/arthas
官网:https://arthas.aliyun.com/zh-cn/

GC日志分析 GCEasy日志分析工具使用

https://www.gceasy.io/

Universal
GC Log Analyzer
Industry’s first machine learning guided Garbage collection log analysis tool. GCeasy has in-built intelligence to auto-detect problems in the JVM & Android GC logs and recommend solutions to it.

  • Solve Memory & GC problems in seconds
  • Get JVM Heap settings recommendations
  • Machine Learning Algorithms
  • Trusted by 4,000+ enterprises
GCViewer日志分析工具使用

https://github.com/chewiebug/GCViewer

Fork of tagtraum industries’ GCViewer. Tagtraum stopped development in 2008, I aim to improve support for Sun’s / Oracle’s java 1.6+ garbage collector logs (including G1 collector)

调优实战
  • 了解JDK版本信息?
    OpenJDK还是OracleJDK,JDK6、JDK8还是JDK11等等,第一时间掌握不同版本对JVM调优方式的差异。
    
  • 了解服务器内存、CPU及磁盘空间等信息?
    掌握对JVM可分配的物理内存大小
    
  • 了解服务的业务类型?
    根据服务的内存使用特点选择更适合的垃圾收集器
    
  • 了解服务访问流量?
    分析单位时间内年轻代、老年代内存变化曲线
    
  • 了解服务启动命令?
    掌握内存分配、GC垃圾收集器等情况
    
  • 是否有近期的GC日志?
    分析系统当前JVM Throughput、FGC等指标,用于调优后进行指标对比。
    
亿级电商网站 jvm 参数调优实战案例
java -Xms3g -Xmx3g -Xmn2g -Xss1M -XX:MetaspaceSize=512M -XX:MaxmetaspaceSize=512M -jar order.jar
单机几十万并发的系统 jvm 如何优化

如:RocktMQ、Kafuka

java -Xms64g -Xmx64g -Xss1M -XX:+UseG1GC -XX:MaxGCPauseMillis=100 
-XX:MetaspaceSize=512M -XX:MaxmetaspaceSize=512M -jar RocktMQ.jar
参考
  • 5 款强大的 JVM 性能调优工具 ,好用爆了
  • JVM性能调优工具2之jcmd详解(覆盖全网最全的jcmd命令与说明文档)
  • 【Java线程与内存分析工具】VisualVM与MAT简明教程
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/999431.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号