现实企业级Java开发中,有时候我们会碰到下面这些问题:
OutOfMemoryError,内存不足
内存泄露
线程死锁
锁争用(Lock Contention)
Java进程消耗CPU过高
......
这些问题在日常开发中可能被很多人忽视(比如有的人遇到上面的问题只是重启服务器或者调大内存,而不会深究问题根源),但能够理解并解决这些问题是Java程序员进阶的必备要求。本文将对一些常用的JVM性能调优监控工具进行介绍,希望能起抛砖引玉之用。本文参考了网上很多资料,难以一一列举,在此对这些资料的作者表示感谢!关于JVM性能调优相关的资料,请参考文末。
A、 jps(Java Virtual Machine Process Status Tool)
jps主要用来输出JVM中运行的进程状态信息。语法格式如下:
1
|
jps [options] [hostid]
|
如果不指定hostid就默认为当前主机或服务器。
命令行参数选项说明如下:
1
2
3
4
|
-q 不输出类名、Jar名和传入main方法的参数
-m 输出传入main方法的参数
-l 输出main类或Jar的全限名
-
v
输出传入JVM的参数
|
比如下面:
1
2
3
4
5
6
7
8
|
root@ubuntu:/
# jps -m -l
2458 org.artifactory.standalone.main.Main
/usr/local/artifactory-2
.2.5
/etc/jetty
.xml
29920 com.sun.tools.hat.Main -port 9998
/tmp/dump
.dat
3149 org.apache.catalina.startup.Bootstrap start
30972 sun.tools.jps.Jps -m -l
8247 org.apache.catalina.startup.Bootstrap start
25687 com.sun.tools.hat.Main -port 9999 dump.dat
21711 mrf-center.jar
|
B、 jstack
jstack主要用来查看某个Java进程内的线程堆栈信息。语法格式如下:
1
2
3
|
jstack [option] pid
jstack [option] executable core
jstack [option] [server-
id
@]remote-
hostname
-or-ip
|
命令行参数选项说明如下:
1
2
|
-l long listings,会打印出额外的锁信息,在发生死锁时可以用jstack -l pid来观察锁持有情况
-m mixed mode,不仅会输出Java堆栈信息,还会输出C
/C
++堆栈信息(比如Native方法)
|
jstack可以定位到线程堆栈,根据堆栈信息我们可以定位到具体代码,所以它在JVM性能调优中使用得非常多。下面我们来一个实例找出某个Java进程中最耗费CPU的Java线程并定位堆栈信息,用到的命令有ps、top、printf、jstack、grep。
第一步先找出Java进程ID,我部署在服务器上的Java应用名称为mrf-center:
1
2
|
root@ubuntu:/
# ps -ef | grep mrf-center | grep -v grep
root 21711 1 1 14:47 pts
/3
00:02:10 java -jar mrf-center.jar
|
得到进程ID为21711,第二步找出该进程内最耗费CPU的线程,可以使用ps -Lfp pid或者ps -mp pid -o THREAD, tid, time或者top -Hp pid,我这里用第三个,输出如下:
TIME列就是各个Java线程耗费的CPU时间,CPU时间最长的是线程ID为21742的线程,用
1
|
printf
"%x\n"
21742
|
得到21742的十六进制值为54ee,下面会用到。
OK,下一步终于轮到jstack上场了,它用来输出进程21711的堆栈信息,然后根据线程ID的十六进制值grep,如下:
1
2
|
root@ubuntu:/
# jstack 21711 | grep 54ee
"PollIntervalRetrySchedulerThread"
prio=10 tid=0x00007f950043e000 nid=0x54ee
in
Object.wait() [0x00007f94c6eda000]
|
可以看到CPU消耗在PollIntervalRetrySchedulerThread这个类的Object.wait(),我找了下我的代码,定位到下面的代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
// Idle wait
getLog().info(
"Thread ["
+ getName() +
"] is idle waiting..."
);
schedulerThreadState = PollTaskSchedulerThreadState.IdleWaiting;
long
now = System.currentTimeMillis();
long
waitTime = now + getIdleWaitTime();
long
timeUntilContinue = waitTime - now;
synchronized
(sigLock) {
try
{
if
(!halted.get()) {
sigLock.wait(timeUntilContinue);
}
}
catch
(InterruptedException ignore) {
}
}
|
它是轮询任务的空闲等待代码,上面的sigLock.wait(timeUntilContinue)就对应了前面的Object.wait()。
jmap查看内存使用情况与生成heapdump
如果想分析自己的JAVA Application时,可以使用jmap程序来生成heapdump文例:
jmap -heap 1234 (1234为进程号)
jmap是JDK自带的一个工具,非常小巧方便,其支持参数如下:
-heap:打印heap空间的概要,这里可以粗略的检验heap空间的使用情况。
例:
jmap -heap 12345
输出:
Attaching to process ID 2657, please wait...
Debugger attached successfully.
Client compiler detected.
JVM version is 1.5.0_16-b02
using thread-local object allocation.
Mark Sweep Compact GC
Heap Configuration:
MinHeapFreeRatio = 40
MaxHeapFreeRatio = 70
MaxHeapSize = 67108864 (64.0MB)
NewSize = 655360 (0.625MB)
MaxNewSize = 4294901760 (4095.9375MB)
OldSize = 1441792 (1.375MB)
NewRatio = 12
SurvivorRatio = 8
PermSize = 8388608 (8.0MB)
MaxPermSize = 67108864 (64.0MB)
Heap Usage:
New Generation (Eden + 1 Survivor Space):----------------------------------------新生代区
capacity = 4521984 (4.3125MB)
used = 1510200 (1.4402389526367188MB)
free = 3011784 (2.8722610473632812MB)
33.39684527853261% used
Eden Space:--------------------------------------------------------------------伊甸园区
capacity = 4063232 (3.875MB)
used = 1495992 (1.4266891479492188MB)
free = 2567240 (2.4483108520507812MB)
36.81778446320565% used
From Space:-------------------------------------------------------------------年轻代(幸存者乐园1)
capacity = 458752 (0.4375MB)
used = 14208 (0.0135498046875MB)
free = 444544 (0.4239501953125MB)
3.0970982142857144% used
To Space:-----------------------------------------------------------------------------年轻代(幸存者乐园2)
capacity = 458752 (0.4375MB)
used = 0 (0.0MB)
free = 458752 (0.4375MB)
0.0% used
concurrent mark-sweep generation:-------------------------------------------------老年代
capacity = 8589934592 (8192.0MB)
used = 0 (0.0MB)
free = 8589934592 (8192.0MB)
0.0% used
Perm Generation:----------------------------------------------------------------------永久代
capacity = 11796480 (11.25MB)
used = 11712040 (11.169471740722656MB)
free = 84440 (0.08052825927734375MB)
99.28419325086806% used
以上的输出很简单,第四行起开始输出此进程我们的JAVA使用的环境。
Heap Configuration,指在我们启动时设置的一些JVM参数。像最大使用内存大小,年老代,年青代,持久代大小等。有这个可以很简单的查看本进程的内存使用情况。也许进程占用的总内存比较多,但我们在这里可以看到真正用到的并没有多少,很多都是"Free"。内存使用的堆积大多在老年代,内存池露始于此,所以要格外关心“tenured generation”。
-heap:format=b:产生一个HeapDump文件,此为生成heapdump文件的重要参数。
例:jmap -heap:format=b 2657
会产生一个heap.bin的heapdump文件。
需要注意的是,此生成heapdump的参数为JDK1.5,在1.6中的格式为:
jmap -dump:live,format=b,file=xxx 2657
这里更加强大一些,可以指定是存活的对象,还有生成heapdump的文件名。
-histo:这里会生成一个类的统计报表,此表简单无比,如显示什么类有多少个实例,共占了多少字节等,如下:
Size Count Class description
-------------------------------------------------------
8394352 105 long[]
8293192 57202 char[]
7834776 14157 byte[]
6713592 53743 * ConstMethodKlass
4194320 1 com.xjawa.cms5server.Kontent[]
4055072 12319 int[]
3291104 85082 * SymbolKlass
3016040 53743 * MethodKlass
2774936 4253 * ConstantPoolKlass
1871480 4253 * InstanceKlassKlass
1811808 3990 * ConstantPoolCacheKlass
1488672 62028 java.lang.String
1203280 13258 java.lang.Object[]
-permstat:打印一些持久代上的内存使用状态,有“活”的,有“死”的。
-h 或者 -help:查看jmap所有可用命令及作用。
jhat
JVM Heap Analysis Tool命令是与jmap搭配使用,用来分析jmap生成的dump,jhat内置了一个微型的HTTP/HTML服务器,生成dump的分析结果后,可以在浏览器中查看。在此要注意,一般不会直接在服务器上进行分析,因为jhat是一个耗时并且耗费硬件资源的过程,一般把服务器生成的dump文件复制到本地或其他机器上进行分析。【内存分析】
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
|
[root@localhost bin]
# jhat -help
Usage: jhat [-stack <bool>] [-refs <bool>] [-port <port>] [-baseline <
file
>] [-debug <int>] [-version] [-h|-help] <
file
>
-J<flag> Pass <flag> 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 <port>: Set the port
for
the HTTP server. Defaults to 7000
-exclude <
file
>: Specify a
file
that lists data members that should
be excluded from the reachableFrom query.
-baseline <
file
>: 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 <int>: 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
<
file
> The
file
to
read
For a dump
file
that contains multiple heap dumps,
you may specify
which
dump
in
the
file
by appending
"#<number>"
to the
file
name, i.e.
"foo.hprof#3"
.
All boolean options default to
"true"
|
参数
-refs false|true
-port port-number
-baseline exclude-file
-debug int
-version
示例
1
|
jhat -J-Xmx512m dump.hprof
|
1
|
jhat -port 7000 mem.dat
|
jmap -dump:format=b,file=mem.dat pid #将内存使用的详细情况输出到mem.dat 文件
通过jhat -port 7000 mem.dat可以将mem.dat的内容以web的方式暴露到网络,访问http://ip-server:7000查看。
