名称 | 主要功能 |
jps | JVM Process Status Tool,显示指定系统内所有HotSpot虚拟机进程 |
jstat | JVM Statistics Minitoring Tool,用于收集HotSpot虚拟机各方面的运行数据 |
jinfo | Configuration Info for Java,显示虚拟机配置信息 |
jmap | Memory Map for Java,生成虚拟机的内存转储快照(heapdump)文件 |
jhat | JVM Heap Dump Browser,用于分析heapdump文件,它会建立一个HTTP/HTML服务器,让用户可以在浏览器上查看分析结果 |
jstack | Stack Trace for Java,显示虚拟机的线程快照 |
1 : jps -mlv 查看 java 进程号,jar 包 启动参数
2 : jstat 显示本地或远程虚拟机进程中的类装载、内存、垃圾收集、JIT编译等运行数据
2.1 jstat -class 6570 1000 10 监控类装载卸载的数量和空间 以及装载时间 1秒1次,监控10次 6570代表进程id
备注 如果频繁装载卸载代表 有问题,垃圾回收的是内存,卸载是class 对象,只有加载类的classloader 被垃圾回收的时候,才会卸载这个类加载器加载的类。根加载器,扩展类加载器和系统类加载器加载的类不会被卸载,只有自定义的类加载器加载的类当类加载器 被垃圾回收的时候(classloader=null) ,这些类才会被卸载。
- Loaded : 装载的类的数量
- Bytes : 装载类所占用的字节数
- Unloaded : 卸载类的数量
- Bytes : 卸载类的字节数
- Time : 装载和卸载类所花费的时间
2.2 jstat -gc 6570 1000 10 监控Java堆状况,包括Eden区、2个Survivor区、老年代、永久代等的容量 1秒1次,监控10次 6570代表进程id
S0C S1C S0U S1U EC EU OC OU MC MU CCSC CCSU YGC YGCT FGC FGCT GCT
72192.0 71680.0 0.0 31069.1 554496.0 445387.1 1398272.0 1384064.4 101720.0 99307.2 12416.0 11824.2 741 30.408 22 16.258 46.665
- S0C: Young Generation第一个survivor space的内存大小 (kB).
- S1C: Young Generation第二个survivor space的内存大小 (kB).
- S0U: Young Generation第一个Survivor space当前已使用的内存大小 (kB).
- S1U: Young Generation第二个Survivor space当前已经使用的内存大小 (kB).
- EC: Young Generation中eden space的内存大小 (kB).
- EU: Young Generation中Eden space当前已使用的内存大小 (kB).
- OC: Old Generation的内存大小 (kB).
- OU: Old Generation当前已使用的内存大小 (kB).
- PC: Permanent Generation的内存大小 (kB)
- PU: Permanent Generation当前已使用的内存大小 (kB).
- YGC: 从启动到采样时Young Generation GC的次数
- YGCT: 从启动到采样时Young Generation GC所用的时间 (s).
- FGC: 从启动到采样时Old Generation GC的次数.
- FGCT: 从启动到采样时Old Generation GC所用的时间 (s).
- GCT: 从启动到采样时GC所用的总时间 (s).
备注: FGC(完全GC)的数量正常来说FGC应该占整个GC(YGC+FGC)的1%到5%才正常
3 jmap
参数:
- option: 选项参数。
- pid: 需要打印配置信息的进程ID。
- executable: 产生核心dump的Java可执行文件。
- core: 需要打印配置信息的核心文件。
- server-id 可选的唯一id,如果相同的远程主机上运行了多台调试服务器,用此选项参数标识服务器。
- remote server IP or hostname 远程调试服务器的IP地址或主机名。
option
- no option: 查看进程的内存映像信息,类似 Solaris pmap 命令。
- heap: 显示Java堆详细信息
- histo[:live]: 显示堆中对象的统计信息
- clstats:打印类加载器信息
- finalizerinfo: 显示在F-Queue队列等待Finalizer线程执行finalizer方法的对象
- dump:<dump-options>:生成堆转储快照
- F: 当-dump没有响应时,使用-dump或者-histo参数. 在这个模式下,live子参数无效.
- help:打印帮助信息
- J<flag>:指定传递给运行jmap的JVM的参数
3.1 jmap -heap 11392 查看堆内存配置和使用情况
C:\Users\liqingshan>jmap -heap 11392
Attaching to process ID 11392, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.161-b12
using thread-local object allocation.
Parallel GC with 4 thread(s) //GC方式
Heap Configuration: //堆内存初始化配置
MinHeapFreeRatio = 0 //对应jvm启动参数-XX:MinHeapFreeRatio设置JVM堆最小空闲比率(default 40)
MaxHeapFreeRatio = 100 //对应jvm启动参数 -XX:MaxHeapFreeRatio设置JVM堆最大空闲比率(default 70)
MaxHeapSize = 2118123520 (2020.0MB) //对应jvm启动参数-XX:MaxHeapSize=设置JVM堆的最大大小
NewSize = 44564480 (42.5MB) //对应jvm启动参数-XX:NewSize=设置JVM堆的‘新生代’的默认大小
MaxNewSize = 705691648 (673.0MB) //对应jvm启动参数-XX:MaxNewSize=设置JVM堆的‘新生代’的最大大小
OldSize = 89653248 (85.5MB) //对应jvm启动参数-XX:OldSize=<value>:设置JVM堆的‘老生代’的大小
NewRatio = 2 //对应jvm启动参数-XX:NewRatio=:‘新生代’和‘老生代’的大小比率
SurvivorRatio = 8 //对应jvm启动参数-XX:SurvivorRatio=设置年轻代中Eden区与Survivor区的大小比值
MetaspaceSize = 21807104 (20.796875MB) //对应jvm启动参数-XX:MetaspaceSize=<value>:设置JVM堆的‘元空间’的初始大小
CompressedClassSpaceSize = 1073741824 (1024.0MB) //对应jvm启动参数-XX:CompressedClassSpaceSize=<value>:设置类指针压缩空间大小
MaxMetaspaceSize = 17592186044415 MB //对应jmv启动参数-XX:MaxMetaspaceSize=<value>:设置JVM堆的'元空间'的最大大小
G1HeapRegionSize = 0 (0.0MB)
Heap Usage: //堆内存使用情况
PS Young Generation
Eden Space: //Eden区内存分布
capacity = 24641536 (23.5MB) //总容量
used = 492872 (0.47003936767578125MB) //已使用容量
free = 24148664 (23.02996063232422MB) //剩余容量
2.000167522024601% used //使用比例
From Space: //其中一个Survivor区的内存分布
capacity = 524288 (0.5MB)
used = 0 (0.0MB)
free = 524288 (0.5MB)
0.0% used
To Space: //另一个Survivor区的内存分布
capacity = 524288 (0.5MB)
used = 0 (0.0MB)
free = 524288 (0.5MB)
0.0% used
PS Old Generation //当前的老年代内存分布
capacity = 34603008 (33.0MB)
used = 674280 (0.6430435180664062MB)
free = 33928728 (32.356956481933594MB)
1.9486167214133523% used
interned Strings occupying 146816 bytes.
3.2 jmap -histo:live 11392 查看堆内存中占用内存的class 的对象的列表 (降序),其中 :live 参数会指定JVM先触发一次GC之后, 再统计堆的柱状图。
C:\Users\liqingshan> jmap -histo:live 11392
num #instances #bytes class name
----------------------------------------------
1: 2336 397136 [C
2: 497 56704 java.lang.Class
3: 2065 49560 java.lang.String
4: 792 31680 java.util.TreeMap$Entry
5: 529 30224 [Ljava.lang.Object;
6: 10 25032 [B
7: 388 9312 java.util.LinkedList$Node
8: 195 8896 [Ljava.lang.String;
9: 228 7296 java.util.HashMap$Node
10: 195 6240 java.util.LinkedList
11: 256 4096 java.lang.Integer
12: 99 3960 java.lang.ref.SoftReference
13: 117 3744 java.util.Hashtable$Entry
14: 93 3616 [I
15: 13 3088 [Ljava.util.HashMap$Node;
16: 7 2632 java.lang.Thread
17: 2 2080 [[C
18: 38 1824 sun.util.locale.LocaleObjectCache$CacheEntry
19: 51 1632 java.util.concurrent.ConcurrentHashMap$Node
20: 20 1600 java.lang.reflect.Constructor
.................................................. // 实际测试按num编号有187条记录, 这里只贴出了前20条记录
Total 8528 674328
3.3 jmap -dump 命令转储内存镜像
jmap -dump:live,format=b,file=qingshanli 11392 转储活动对象的镜像。
jmap -dump:format=b,file=qingshanli 11392 转储所有对象的镜像。
jmap -F -dump:format=b,file=qingshanli 11392 强制转储所有对象的镜像。
可以使用jhat命令启动一个HTTP/HTML服务器: jhat -port 10000 qingshanli
C:\Users\liqingshan>jmap -dump:format=b,file=qingshanli 11392
Dumping heap to C:\Users\liqingshan\qingshanli ...
Heap dump file created
C:\Users\liqingshan>jhat -port 10000 qingshanli
Reading from qingshanli...
Dump file created Mon Jul 16 19:50:18 GMT+08:00 2018
Snapshot read, resolving...
Resolving 562831 objects...
Chasing references, expect 112 dots................................................................................................................
Eliminating duplicate references................................................................................................................
Snapshot resolved.
Started HTTP server on port 10000
Server is ready.
在浏览器中输入地址: http://localhost:10000, 就可以在页面查看堆转储快照详细信息。
4 jstack
Usage:
jstack [-l] <pid>
(to connect to running process)
jstack -F [-m] [-l] <pid>
(to connect to a hung process)
jstack [-m] [-l] <executable> <core>
(to connect to a core file)
jstack [-m] [-l] [server_id@]<remote server IP or hostname>
(to connect to a remote debug server)
Options:
-F to force a thread dump. Use when jstack <pid> 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
4.1 jstack pid >stack.txt 打印栈信息到stack.txt
4.2 jstack -F pid >stack.txt 强制 打印栈信息到stack.txt
4.3 jstack -l pid >stack.txt 打印额外的锁信息到stack.txt
4.4 jstack -m pid >stack.txt 打印jvm栈和本地栈帧到stack.txt
5jinfo 打印系统属性和 vm 参数
5.1 jinfo 14568
Attaching to process ID 150876, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.131-b11
Java System Properties:
java.runtime.name = Java(TM) SE Runtime Environment
java.vm.version = 25.131-b11
sun.boot.library.path = /usr/java/jdk1.8.0_131/jre/lib/amd64
java.protocol.handler.pkgs = org.springframework.boot.loader
java.vendor.url = http://java.oracle.com/
java.vm.vendor = Oracle Corporation
path.separator = :
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 = /home/work/x_engine/policy
java.vm.specification.name = Java Virtual Machine Specification
PID = 150876
java.runtime.version = 1.8.0_131-b11
java.awt.graphicsenv = sun.awt.X11GraphicsEnvironment
os.arch = amd64
java.endorsed.dirs = /usr/java/jdk1.8.0_131/jre/lib/endorsed
org.jboss.logging.provider = slf4j
line.separator =
java.io.tmpdir = /tmp
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
spring.beaninfo.ignore = true
sun.nio.ch.bugLevel =
java.specification.name = Java Platform API Specification
java.class.version = 52.0
sun.management.compiler = HotSpot 64-Bit Tiered Compilers
os.version = 3.10.0-514.el7.x86_64
user.home = /home/work
user.timezone = Asia/Shanghai
catalina.useNaming = false
java.awt.printerjob = sun.print.PSPrinterJob
file.encoding = UTF-8
java.specification.version = 1.8
catalina.home = /tmp/tomcat.671575679209494102.35011
user.name = work
java.class.path = /home/work/x_engine/policy/strategy-service.jar
java.vm.specification.version = 1.8
sun.arch.data.model = 64
sun.java.command = /home/work/x_engine/policy/strategy-service.jar
java.home = /usr/java/jdk1.8.0_131/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_131
java.ext.dirs = /usr/java/jdk1.8.0_131/jre/lib/ext:/usr/java/packages/lib/ext
sun.boot.class.path = /usr/java/jdk1.8.0_131/jre/lib/resources.jar:/usr/java/jdk1.8.0_131/jre/lib/rt.jar:/usr/java/jdk1.8.0_131/jre/lib/sunrsasign.jar:/usr/java/jdk1.8.0_131/jre/lib/jsse.jar:/usr/java/jdk1.8.0_131/jre/lib/jce.jar:/usr/java/jdk1.8.0_131/jre/lib/charsets.jar:/usr/java/jdk1.8.0_131/jre/lib/jfr.jar:/usr/java/jdk1.8.0_131/jre/classes
java.awt.headless = true
java.vendor = Oracle Corporation
catalina.base = /tmp/tomcat.671575679209494102.35011
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=12 -XX:InitialHeapSize=2147483648 -XX:MaxHeapSize=2147483648 -XX:MaxNewSize=715653120 -XX:MinHeapDeltaBytes=524288 -XX:NewSize=715653120 -XX:OldSize=1431830528 -XX:+PrintGC -XX:+PrintGCDateStamps -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseParallelGC
Command line: -Xmx2g -Xms2g -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:./gc.log -Dfile.encoding=UTF-8