JVM性能优化详解

JVM相关工具

$JAVA_HOME/bin下

命令说明
jps 查看所有jvm进程
jinfo 查看进程的运行环境参数,主要是jvm命令行参数
jstat 对jvm应用程序的资源和性能进行实时监控
jstack 查看所有线程的运行状态
jmap 查看jvm占用物理内存的状态
jhat +UseParNew
jconsole  
jvisualvm  
jps:Java virutal machine Process Status tool,
    jps [-q] [-mlvV] [<hostid>]
        -q:静默模式;
        -v:显示传递给jvm的命令行参数;
        -m:输出传入main方法的参数;
        -l:输出main类或jar完全限定名称;
        -v:显示通过flag文件传递给jvm的参数;
        [<hostid>]:主机id,默认为localhost;

详细列出当前Java进程信息
[root@tomcat ~]#jps -l -v
21560 org.netbeans.Main -Djdk.home=/usr/local/jdk -Dnetbeans.default_userdir_root=/root/.visualvm -Dnetbeans.dirs=/usr/local/jdk/lib/visualvm/visualvm:/usr/local/jdk/lib/visualvm/profiler: -Dnetbeans.home=/usr/local/jdk/lib/visualvm/platform -Xms24m -Xmx256m -Dsun.jvmstat.perdata.syncWaitMs=10000 -Dsun.java2d.noddraw=true -Dsun.java2d.d3d=false -Dnetbeans.keyring.no.master=true -Dplugin.manager.install.global=false --add-exports=java.desktop/sun.awt=ALL-UNNAMED --add-exports=jdk.jvmstat/sun.jvmstat.monitor.event=ALL-UNNAMED --add-exports=jdk.jvmstat/sun.jvmstat.monitor=ALL-UNNAMED --add-exports=java.desktop/sun.swing=ALL-UNNAMED --add-exports=jdk.attach/sun.tools.attach=ALL-UNNAMED --add-modules=java.activation -XX:+IgnoreUnrecognizedVMOptions -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/root/.visualvm/8u131/var/log/heapdump.hprof
22270 sun.tools.jps.Jps -Denv.class.path=/usr/local/jdk/lib/:/usr/local/jdk/jre/lib/ -Dapplication.home=/usr/local/jdk1.8.0_241 -Xms8m
21407 HelloWorld -Xms256m -Xmx512m
jinfo:输出给定的java进程的所有配置信息;
    jinfo [option] <pid>
        -flags:打印 VM flags
        -sysprops:to print Java system properties
        -flag <name>:to print the value of the named VM flag

先获得一个java进程ID,然后jinfo
[root@tomcat ~]#jps
22357 Jps
21560 Main
21407 HelloWorld

[root@tomcat ~]#jinfo 21407
Attaching to process ID 21407, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.241-b07
Java System Properties:

java.runtime.name = Java(TM) SE Runtime Environment
java.vm.version = 25.241-b07
sun.boot.library.path = /usr/local/jdk1.8.0_241/jre/lib/amd64
java.vendor.url = http://java.oracle.com/
java.vm.vendor = Oracle Corporation
path.separator = :
java.rmi.server.randomIDs = true
file.encoding.pkg = sun.io
java.vm.name = Java HotSpot(TM) 64-Bit Server VM
sun.os.patch.level = unknown
sun.java.launcher = SUN_STANDARD
user.country = US
user.dir = /data
java.vm.specification.name = Java Virtual Machine Specification
java.runtime.version = 1.8.0_241-b07
java.awt.graphicsenv = sun.awt.X11GraphicsEnvironment
os.arch = amd64
java.endorsed.dirs = /usr/local/jdk1.8.0_241/jre/lib/endorsed
java.io.tmpdir = /tmp
line.separator = 

java.vm.specification.vendor = Oracle Corporation
os.name = Linux
sun.jnu.encoding = UTF-8
java.library.path = /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
java.specification.name = Java Platform API Specification
java.class.version = 52.0
sun.management.compiler = HotSpot 64-Bit Tiered Compilers
os.version = 4.18.0-147.el8.x86_64
user.home = /root
user.timezone = Asia/Shanghai
java.awt.printerjob = sun.print.PSPrinterJob
file.encoding = UTF-8
java.specification.version = 1.8
user.name = root
java.class.path = /usr/local/jdk/lib/:/usr/local/jdk/jre/lib/
java.vm.specification.version = 1.8
sun.arch.data.model = 64
sun.java.command = HelloWorld
java.home = /usr/local/jdk1.8.0_241/jre
user.language = en
java.specification.vendor = Oracle Corporation
awt.toolkit = sun.awt.X11.XToolkit
java.vm.info = mixed mode
java.version = 1.8.0_241
java.ext.dirs = /usr/local/jdk1.8.0_241/jre/lib/ext:/usr/java/packages/lib/ext
sun.boot.class.path = /usr/local/jdk1.8.0_241/jre/lib/resources.jar:/usr/local/jdk1.8.0_241/jre/lib/rt.jar:/usr/local/jdk1.8.0_241/jre/lib/sunrsasign.jar:/usr/local/jdk1.8.0_241/jre/lib/jsse.jar:/usr/local/jdk1.8.0_241/jre/lib/jce.jar:/usr/local/jdk1.8.0_241/jre/lib/charsets.jar:/usr/local/jdk1.8.0_241/jre/lib/jfr.jar:/usr/local/jdk1.8.0_241/jre/classes
java.vendor = Oracle Corporation
file.separator = /
java.vendor.url.bug = http://bugreport.sun.com/bugreport/
sun.io.unicode.encoding = UnicodeLittle
sun.cpu.endian = little
sun.cpu.isalist = 

VM Flags:
Non-default VM flags: -XX:CICompilerCount=2 -XX:InitialHeapSize=268435456 -XX:MaxHeapSize=536870912 -XX:MaxNewSize=178913280 -XX:MinHeapDeltaBytes=196608 -XX:NewSize=89456640 -XX:OldSize=178978816 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseFastUnorderedTimeStamps 
Command line:  -Xms256m -Xmx512m

[root@tomcat ~]#jinfo -flags 21407
Attaching to process ID 21407, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.241-b07
Non-default VM flags: -XX:CICompilerCount=2 -XX:InitialHeapSize=268435456 -XX:MaxHeapSize=536870912 -XX:MaxNewSize=178913280 -XX:MinHeapDeltaBytes=196608 -XX:NewSize=89456640 -XX:OldSize=178978816 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseFastUnorderedTimeStamps 
Command line:  -Xms256m -Xmx512m
jstat:输出指定的java进程的统计信息
    jstat -help|-options
    jstat -<option> [-t] [-h<lines>] <vmid> [<interval> [<count>]]
    [<interval> [<count>]]
        interval:时间间隔,单位是毫秒;
        count:显示的次数;

返回可用统计项列表
# jstat -options
    -class:class loader
    -compiler:JIT
    -gc:gc
    -gccapacity:统计堆中各代的容量
    -gccause:
    -gcmetacapacity
    -gcnew:新生代
    -gcnewcapacity
    -gcold:老年代
    -gcoldcapacity
    -gcutil
    -printcompilation

[root@tomcat ~]#jstat -gc 21407
 S0C    S1C    S0U    S1U      EC       EU        OC         OU       MC     MU    CCSC   CCSU   YGC     YGCT    FGC    FGCT     GCT   
8704.0 8704.0 1563.6  0.0   69952.0  58033.0   174784.0     0.0     9344.0 8830.0 1152.0 1013.0      2    0.050   0      0.000    0.050

YGC:新生代的垃圾回收次数;
YGCT:新生代垃圾回收消耗的时长; 
FGC:Full GC的次数;
FGCT:Full GC消耗的时长;
GCT:GC消耗的总时长;

3次,一秒一次
[root@tomcat ~]#jstat -gcnew 21407 1000 3
 S0C    S1C    S0U    S1U   TT MTT  DSS      EC       EU     YGC     YGCT  
8704.0 8704.0 1563.6    0.0 15  15 4352.0  69952.0  62794.3      2    0.050
8704.0 8704.0 1563.6    0.0 15  15 4352.0  69952.0  62794.3      2    0.050
8704.0 8704.0 1563.6    0.0 15  15 4352.0  69952.0  63074.5      2    0.050

程序员常用堆栈情况查看工具

jstack:查看指定的java进程的线程栈的相关信息;
    jstack [-l] <pid>
    jstack -F [-m] [-l] <pid>
        -l:long listings,会显示额外的锁信息,因此,发生死锁时常用此选项;
        -m:混合模式,既输出java堆栈信息,也输出C/C++堆栈信息;
        -F:当使用“jstack -l PID"无响应,可以使用-F强制输出信息;

先获得一个java进程ID,然后jinfo
[root@tomcat ~]#jstack -l 21407
2020-02-15 13:49:56
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.241-b07 mixed mode):

"RMI TCP Connection(4)-10.0.0.101" #16 daemon prio=9 os_prio=0 tid=0x00007f279c29e800 nid=0x5753 runnable [0x00007f279b181000]
   java.lang.Thread.State: RUNNABLE
    at java.net.SocketInputStream.socketRead0(Native Method)
......

jmap:Memory Map, 用于查看堆内存的使用状态;

查看进程堆内存情况
[root@tomcat ~]#jmap -heap 21407
Attaching to process ID 21407, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.241-b07

using thread-local object allocation.
Mark Sweep Compact GC

Heap Configuration:
   MinHeapFreeRatio         = 40
   MaxHeapFreeRatio         = 70
   MaxHeapSize              = 536870912 (512.0MB)
   NewSize                  = 89456640 (85.3125MB)
   MaxNewSize               = 178913280 (170.625MB)
   OldSize                  = 178978816 (170.6875MB)
   NewRatio                 = 2
   SurvivorRatio            = 8
   MetaspaceSize            = 21807104 (20.796875MB)
   CompressedClassSpaceSize = 1073741824 (1024.0MB)
   MaxMetaspaceSize         = 17592186044415 MB
   G1HeapRegionSize         = 0 (0.0MB)

Heap Usage:
New Generation (Eden + 1 Survivor Space):
   capacity = 80543744 (76.8125MB)
   used     = 4893096 (4.666419982910156MB)
   free     = 75650648 (72.14608001708984MB)
   6.075078903707283% used
Eden Space:
   capacity = 71630848 (68.3125MB)
   used     = 3301800 (3.1488418579101562MB)
   free     = 68329048 (65.16365814208984MB)
   4.609466580655306% used
From Space:
   capacity = 8912896 (8.5MB)
   used     = 1591296 (1.517578125MB)
   free     = 7321600 (6.982421875MB)
   17.85386029411765% used
To Space:
   capacity = 8912896 (8.5MB)
   used     = 0 (0.0MB)
   free     = 8912896 (8.5MB)
   0.0% used
tenured generation:
   capacity = 178978816 (170.6875MB)
   used     = 0 (0.0MB)
   free     = 178978816 (170.6875MB)
   0.0% used

4926 interned Strings occupying 389832 bytes.

jhat:Java Heap Analysis Tool
    jmap [option] <pid>

    查看堆空间的详细信息:
        jmap -heap <pid>

    查看堆内存中的对象的数目:
        jmap -histo[:live] <pid>
            live:只统计活动对象;

    保存堆内存数据至文件中,而后使用jvisualvm或jhat进行查看:
        jmap -dump:<dump-options> <pid>
            dump-options:
            live         dump only live objects; if not specified, all objects in the heap are dumped.
            format=b     binary format
            file=<file>  dump heap to <file>
posted @ 2021-05-21 16:00  woaibaobei  阅读(276)  评论(0编辑  收藏  举报