Effective Java Profiling With Open Source Tools(开源Java性能分析工具)

本文介绍了如何使用jmap工具获取Java应用程序内存使用情况,通过PID获取内存直方图,以及如何利用VisualVM进行更全面的性能分析。此外,还提到了BTrace工具的使用和限制,最后介绍了jstat命令用于监视JVM统计信息。
摘要由CSDN通过智能技术生成

本文内容节选自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对应的信息如下

ColumnDescription
S0CCurrent survivor space 0 capacity (KB).
S1CCurrent survivor space 1 capacity (KB).
S0USurvivor space 0 utilization (KB).
S1USurvivor space 1 utilization (KB).
ECCurrent eden space capacity (KB).
EUEden space utilization (KB).
OCCurrent old space capacity (KB).
OUOld space utilization (KB).
PCCurrent permanent space capacity (KB).
PUPermanent space utilization (KB).
YGCNumber of young generation GC Events.
YGCTYoung generation garbage collection time.
FGCNumber of full GC events.
FGCTFull garbage collection time.
GCTTotal garbage collection time.

更多信息可参看此网页http://download.oracle.com/javase/6/docs/technotes/tools/share/jstat.html

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值