本文内容节选自infoq上的此文章http://www.infoq.com/articles/java-profiling-with-open-source
If your application is experiencing unusually high memory load, frequent freezes or OOME, its often most useful to start by understanding just what kind of objects are present in memory. Luckily the JVM comes out-of-the-box with the ability to do so, with the built-in tool ‘jmap’.
JVM自带的工具jmap可以帮你理解内存现存的各种对象。
In order to run jmap, you need to know the PID of the application that you want to run jmap against. One easy way to obtain this is to use the JVM-provided tool jps, which will list out each and every JVM process running on your machine, along with each process’ PID. The output of jps will look like the following:
但在使用jmap之前,你需要先获取到该application的PID, 一个简单的方法是使用另一个jvm自带的命令jps,该命令会列出你机器上所有jvm进程,包括PID。
To print out a memory histogram, we will invoke the jmap console app passing in our application’s PID and the “-histo:live” option . So, if we wanted to retrieve a memory histogram for the “eureka.Proxy” application from the figure above, we invoke jmap as:
为了打印内存使用图,我们会使用一下命令。注:jmap 有不同的参数选项
jmap –histo:live 45417
The output of the above command might look something like the following:
原文后面还有几张图来对比在内存泄漏状况下使用jmap的情况,在此就省略啦。
Note that jmap isn’t a profiling tool, and that the JVM might halt during the histogram generation, so be sure its acceptable for your application to pause for the time it takes to generate the histogram. In my experience though, generating a histogram is something you do more often when debugging a serious bug, and so a few one minute pauses of the application is likely acceptable during such circumstances. This brings me nicely along to the next topic – the semi-automatic profiling tool VisualVM.
在使用jmap的时候,会导致你的应用暂停。根据作者的经验,一般在调试一个严重bug的情况下使用是可以接受的。下面介绍半自动的性能分析工具VisualVM.
在原文中作者只是给了个使用VisualVM的例子,并没有告知如何安装使用VisualVM,在http://visualvm.java.net/可以下载VisualVM,同时VisualVM入门指南在此http://visualvm.java.net/zh_CN/gettingstarted.html。具体的可以自己下载安装试用体验。
原文中最后一段话指出VisualVM不是一个全特性的优化工具,同时也不能一直运行于你生产环境的JVM,还不能存储获得的数据,以及不能设定警报值等。所以下面会介绍BTrace。
Understand that VisualVM is not a full-featured profiler, as its not capable of constantly running against your production JVM. It won’t persist its data, nor will it be able to specify thresholds and send alerts when these thresholds are breached. To be able to get closer to the goal of a full-featured profiler, let’s look next at BTrace, a full-featured open source Java agent.
看完了BTrace的介绍发现这个工具是需要写annotation到代码中的,同时还需要用它自带的一个编译器编译过,而且本身只输出文本,没有提供任何图形化的工具,MY GOD, 还是算了吧。
在试用VisualVM的过程中发现了一个有用的java命令,jstat - Java Virtual Machine Statistics Monitoring Tool,该命令可以查看当然jvm的一些统计信息。同使用jmap一样,先用jps获取到PID,然后使用jstat观察该PID对象的JVM信息。
例如: jstat -gc 24910
S0C S1C S0U S1U EC EU OC OU PC PU YGC YGCT FGC FGCT GCT
9152.0 12416.0 1464.2 0.0 92864.0 5874.1 352256.0 59318.4 65536.0 63847.1 34 1.098 0 0.000 1.098
显示的结果是GC的相关信息。每个column对应的信息如下
Column | Description |
---|---|
S0C | Current survivor space 0 capacity (KB). |
S1C | Current survivor space 1 capacity (KB). |
S0U | Survivor space 0 utilization (KB). |
S1U | Survivor space 1 utilization (KB). |
EC | Current eden space capacity (KB). |
EU | Eden space utilization (KB). |
OC | Current old space capacity (KB). |
OU | Old space utilization (KB). |
PC | Current permanent space capacity (KB). |
PU | Permanent space utilization (KB). |
YGC | Number of young generation GC Events. |
YGCT | Young generation garbage collection time. |
FGC | Number of full GC events. |
FGCT | Full garbage collection time. |
GCT | Total garbage collection time. |
更多信息可参看此网页http://download.oracle.com/javase/6/docs/technotes/tools/share/jstat.html