JVM有一个对程序优化非常有用的参数-agentlib,对运行很慢的程序优化有很大的帮助。
一:获得这个参数的帮助:
$java -agentlib:hprof=help
Option Name and Value Description Default
--------------------- ----------- -------
heap=dump|sites|all heap profiling all
cpu=samples|times|old CPU usage off
monitor=y|n monitor contention n
format=a|b text(txt) or binary output a
file=
net=
depth=
interval=
cutoff=
lineno=y|n line number in traces? y
thread=y|n thread in traces? n
doe=y|n dump on exit? y
msa=y|n Solaris micro state accounting n
force=y|n force output to
verbose=y|n print messages about dumps y
解释:
heap:获得heap的信息
cpu:采样模式(隔一段时间采样一次)|times(隔一定的时间采样一次)|old(不知)
monitor: 获得monitor的相关信息
format: a(text)|b(二进制)
file: 指定把文件写到哪里,如:file=C:/temp/java.hprof1.txt
net=
depth=
interval=
cutoff=
lineno=y|n 是否在stack strace中显示行号
thread=y|n 不知
doe=y|n 是否要在退出的时候dump
msp=y|n 不知
force=y|n 不知
verbose=y|n 打印dump信息
二:example
-agentlib:hprof=cpu=samples,interval=100,depth=10,file=c:/temp/java.hprof1.txt
下面是我的程序生成的java.hprof1.txt中的片段
rank self accum count trace method
1 6.04% 6.04% 495 300767 java.lang.Throwable.fillInStackTrace
2 2.47% 8.51% 202 300787 java.lang.Throwable.fillInStackTrace
3 2.14% 10.65% 175 300856 java.lang.Throwable.fillInStackTrace
4 1.84% 12.49% 151 300758 java.lang.Throwable.fillInStackTrace
5 1.45% 13.95% 119 300771 java.lang.Throwable.fillInStackTrace
6 1.31% 15.25% 107 300960 java.io.WinNTFileSystem.getLastModifiedTime
7 1.27% 16.52% 104 300756 java.io.FileOutputStream.close0
8 1.09% 17.61% 89 301138 java.io.FileOutputStream.close0
9 1.05% 18.66% 86 300225 java.io.FileInputStream.open
10 1.01% 19.67% 83 301493 java.io.FileOutputStream.close0
11 0.98% 20.65% 80 300964 java.lang.Throwable.fillInStackTrace
12 0.96% 21.61% 79 301133 java.io.FileOutputStream.close0
13 0.95% 22.57% 78 301061 java.lang.ref.Reference$ReferenceHandler.run
14 0.95% 23.52% 78 301508 java.io.FileOutputStream.close0
15 0.94% 24.46% 77 301461 java.io.FileOutputStream.close0
16 0.87% 25.33% 71 301147 java.io.FileOutputStream.close0
17 0.85% 26.18% 70 301500 java.io.FileOutputStream.close0
18 0.83% 27.01% 68 301891 java.io.FileOutputStream.close0
19 0.83% 27.84% 68 301492 java.io.FileOutputStream.close0
20 0.81% 28.65% 66 301494 java.io.FileOutputStream.close0
21 0.78% 29.43% 64 301502 java.io.FileOutputStream.close0
22 0.74% 30.17% 61 301861 java.io.FileOutputStream.close0
23 0.72% 30.90% 59 302750 java.io.FileOutputStream.close0
24 0.71% 31.60% 58 301503 java.io.FileOutputStream.close0
25 0.68% 32.29% 56 300054 java.util.zip.ZipFile.getEntry
26 0.68% 32.97% 56 301501 java.io.FileOutputStream.close0
27 0.63% 33.61% 52 301946 java.io.FileOutputStream.close0
28 0.62% 34.23% 51 301948 java.io.FileOutputStream.close0
29 0.61% 34.84% 50 301496 java.io.FileOutputStream.close0
30 0.60% 35.44% 49 300851 java.io.BufferedReader.ensureOpen
31 0.60% 36.04% 49 302753 java.io.FileOutputStream.close0
32 0.57% 36.61% 47 300819 java.lang.Throwable.fillInStackTrace
33 0.55% 37.16% 45 301990 java.io.FileOutputStream.close0
34 0.55% 37.71% 45 300587 java.lang.ref.Reference$ReferenceHandler.run
35 0.53% 38.23% 43 301985 java.io.FileOutputStream.close0
36 0.53% 38.76% 43 302455 java.io.FileOutputStream.close0
37 0.53% 39.28% 43 300111 java.util.zip.ZipFile.read
38 0.51% 39.80% 42 301666 java.io.FileOutputStream.close0
39 0.48% 40.27% 39 302697 java.io.FileOutputStream.close0
40 0.48% 40.75% 39 302877 java.io.FileOutputStream.close0
41 0.48% 41.23% 39 301670 java.io.FileOutputStream.close0
42 0.46% 41.69% 38 300774 java.io.BufferedReader.
43 0.46% 42.15% 38 302694 java.io.FileOutputStream.close0
44 0.43% 42.58% 35 302656 java.io.FileOutputStream.close0
45 0.43% 43.01% 35 301674 java.io.FileOutputStream.close0
46 0.43% 43.44% 35 301667 java.io.FileOutputStream.close0
47 0.40% 43.84% 33 301991 java.io.FileOutputStream.close0
48 0.38% 44.22% 31 300770 java.io.BufferedReader.ensureOpen
从上面可以看出: fillInStackTrace 和 文件的close程序花了大量的时间