JVM 常用命令(二)

JVM 常用命令(二)

薛定谔的汪

昨天学习了三个JVM常用命令:jps、jstat、jinfo,今天学习jmap、jstack这两个命令。

jmap

jmap命令常用于生成heap dump文件,还可以使用**-XX:+HeapDumpOnOutOfMemoryError-XX:+heapDumpPath**配合使用,当发生OOM时,将heap信息dump指定文件中。

jmap不仅可以dump出heap信息,还可以查询Java堆和永久代的详细信息,如当前使用的是哪种垃圾收集器等。

使用格式:

1
jmap [option]  pid

option参数:

  • dump : 生成堆转储快照
  • finalizerinfo : 显示在F-Queue队列等待Finalizer线程执行finalizer方法的对象
  • heap : 显示Java堆详细信息
  • histo : 显示堆中对象的统计信息
  • permstat : to print permanent generation statistics
  • F : 当-dump没有响应时,强制生成dump快照

举个栗子:

-dump

1
jmap -dump:live,format=b,file=filename pid

dump堆到文件,format指定输出格式,live指明是活着的对象,file指定文件名

1
2
3
jmap -dump:live,format=b,file=dump.dump 20828
Dumping heap to /home/tomcat/dump.hprof ...
Heap dump file created

-finalizerinfo

打印等待回收对象的信息:

1
2
3
4
5
6
jmap -finalizerinfo 20828
Attaching to process ID 20828, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 23.25-b01
Number of objects pending for finalization: 0

-heap

打印heap概要信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
jmap -heap 20828

Attaching to process ID 20828, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 23.25-b01

using thread-local object allocation.
Mark Sweep Compact GC

Heap Configuration: #堆内存初始化配置
MinHeapFreeRatio = 40 #对应jvm启动参数-XX:MinHeapFreeRatio设置JVM堆最小空闲比率(default 40)
MaxHeapFreeRatio = 70 #对应jvm启动参数 -XX:MaxHeapFreeRatio设置JVM堆最大空闲比率(default 70)
MaxHeapSize = 1073741824 (1024.0MB) #对应jvm启动参数-XX:MaxHeapSize=设置JVM堆的最大大小
NewSize = 1310720 (1.25MB) #对应jvm启动参数-XX:NewSize=设置JVM堆的‘新生代’的默认大小
MaxNewSize = 17592186044415 MB #对应jvm启动参数-XX:MaxNewSize=设置JVM堆的‘新生代’的最大大小
OldSize = 5439488 (5.1875MB) #对应jvm启动参数-XX:OldSize=设置JVM堆的‘老生代’的大小
NewRatio = 2 #对应jvm启动参数-XX:NewRatio=:‘新生代’和‘老生代’的大小比率
SurvivorRatio = 8 #对应jvm启动参数-XX:SurvivorRatio=设置年轻代中Eden区与Survivor区的大小比值 默认8:1:1
PermSize = 67108864 (64.0MB) #对应jvm启动参数-XX:PermSize=设置JVM堆的‘永生代’的初始大小
MaxPermSize = 268435456 (256.0MB)#对应jvm启动参数-XX:PermSize=设置JVM堆的‘永生代’的最大值
G1HeapRegionSize = 0 (0.0MB)

Heap Usage: #堆使用情况
New Generation (Eden + 1 Survivor Space):
capacity = 79298560 (75.625MB)
used = 53412648 (50.938270568847656MB)
free = 25885912 (24.686729431152344MB)
67.35639083483987% used
Eden Space:
capacity = 70516736 (67.25MB)
used = 53412648 (50.938270568847656MB)
free = 17104088 (16.311729431152344MB)
75.74464025107459% used
From Space:
capacity = 8781824 (8.375MB)
used = 0 (0.0MB)
free = 8781824 (8.375MB)
0.0% used
To Space:
capacity = 8781824 (8.375MB)
used = 0 (0.0MB)
free = 8781824 (8.375MB)
0.0% used
tenured generation:
capacity = 176041984 (167.88671875MB)
used = 99224448 (94.6278076171875MB)
free = 76817536 (73.2589111328125MB)
56.36408187719584% used
Perm Generation:
capacity = 73334784 (69.9375MB)
used = 73134520 (69.74651336669922MB)
free = 200264 (0.19098663330078125MB)
99.72691812932864% used

26706 interned Strings occupying 3092160 bytes.

jstack

jstack书上说是用于生成JVM当前时刻的线程快照,可以用来分析线程停顿时间长、死锁的原因,看到有问题的线程在后台执行什么操作,或是在等待什么资源。

命令格式

1
jstack [option] pid

option分析

  • -F : 当正常输出请求不被响应时,强制输出线程堆栈
  • -l : 除堆栈外,显示关于锁的附加信息
  • -m : 如果调用到本地方法的话,可以显示C/C++的堆栈

关于jstack命令的学习,暂时在脑海里有这个印象,以后项目中用到后会结合实际去理解、分析。

  • Title: JVM 常用命令(二)
  • Author: 薛定谔的汪
  • Created at : 2017-12-17 10:45:11
  • Updated at : 2023-11-17 19:37:37
  • Link: https://www.zhengyk.cn/2017/12/17/jvm/JVM-06/
  • License: This work is licensed under CC BY-NC-SA 4.0.
On this page
JVM 常用命令(二)