Java 分析 - 如何对我的应用程序进行方法分析?
我想运行我的 Java 应用程序,并且对于给定的工作负载能够看到:
- 给定函数被调用了多少次
- 每个函数调用的相对成本是多少(即每个函数调用需要多长时间执行)
我大致知道瓶颈在哪里在我的应用程序中,但我需要更细粒度的视图才能缩小范围。
谢谢
编辑 jvisualvm 看起来像这个工具 - 它在大约 30 秒内确定了问题。我只需要知道“selftime”在方法配置文件的上下文中意味着什么。谢谢
I want to run my Java app and for a given workload be able to see:
- how many times a given function was called
- how expensive each function call is in relative terms (i.e. how long each takes to execute)
I know broadly where the bottle neck is in my application but I need a much more fine grained view in order to narrow it down.
Thanks
Edit jvisualvm looks like the tool - it identified the problem in about 30 seconds. I just need to know what 'selftime' means in the context of a method profile. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
对于在 Sun 的 java 6 中运行的程序来说,最简单的方法是使用 jdk 中的 jvisualvm 程序。允许您附加和配置文件而无需任何特殊设置。
Simplest approach for a program running in java 6 from Sun is to use the jvisualvm program in the jdk. Allows you to attach and profile without any special setup.
最简单的方法是使用 -prof,例如:
java -prof -jar yourjar.jar
程序运行完毕后会打印一个名为 java.prof 的文件。
请参阅 HPROF 文档页面
在我的应用程序中,我使用:
-Xrunhprof:cpu=samples,thread=y,doe=y
这会打印一份报告,其中包含以下内容:
这样您就可以看到各种方法所花费的总时间(以秒为单位)。
在我的应用程序中,大部分时间都在等待来自远程主机的数据(不太可能通过互联网连接)。
Easiest way is to use -prof, e.g:
java -prof -jar yourjar.jar
That will print a file called java.prof after the program has finished running.
See the HPROF documentation page
In my application I use:
-Xrunhprof:cpu=samples,thread=y,doe=y
This prints a report that contains, amongst other things, this:
So you can see the total time (in seconds) spent in various methods.
In mine the app spends most of its time waiting for data from a remote host (not unlikely over an internet connection).
列出了几个分析器(Eclipse 和 JProfiler)。我只是想强烈推荐分析器是您编程工具箱中的工具之一。
这是大多数程序员都会忽略的事情,但是探查器可以解决整类很难通过其他方式解决的问题。
我只是(对每个人,不仅仅是提问者)说,如果您还没有使用过探查器,请找到一个,下载并运行它。
顺便说一句,它们比 java 工具的静态输出强大得多——尽管在这种特定情况下 java 工具可能就足够了。探查器可以告诉您每个线程正在做什么,并且可以制作一些非常酷的调用图形(流程图样式),这将帮助您分析您没有编写的代码。
只需找到一个,并使用它一两周,以便您了解它的功能。
There have been a couple profilers listed (The eclipse one and JProfiler). I just want to HIGHLY RECOMMEND that a profiler is one of the tools in your programming toolchest.
This is something most programmers pass over, but a profiler can solve entire classes of problems that are very difficult to solve otherwise.
I'm just saying (to everyone, not just the questioner) that if you haven't used a profiler go find one, download it and run it.
By the way, they are much more powerful than the static output of the java tools--although the java tools might be enough in this specific case. A profiler can tell you what each thread is doing and can make some pretty cool call graphics (flow diagram style) that will help you analyze code you didn't write.
Just find one, and use it for a week or two so that you know what it offers.
Java 1.7* 与 Java Mission Control (jmc) 捆绑在一起,它具有“飞行记录器”功能,可用于分析方法执行。分析结果的显示几乎与 AppDynamics 类似 - 很容易发现性能问题(特别是哪些方法正在消耗所有 CPU)。
虽然不详细,但很好的博客文章解释了飞行记录器:http://hirt.se/blog/ ?p=364
* 不确定次要版本
Java 1.7* comes bundled with Java Mission Control (jmc), which has 'Flight Recorder' feature which can be used to profile the method execution. Profiling results are displayed almost similar to AppDynamics - easy to spot the perf issues (esp. which methods are eating up all the CPU).
Though not detailed, but good blog post explaining the about Flight Recorder: http://hirt.se/blog/?p=364
* Not sure about the minor version
查看 Eclipse TPTP。他们可以为任何从 Eclipse 启动的应用程序提供这些功能以及更多功能。
Take a look at Eclipse TPTP. They can provide exactly that and much more for any application started from Eclipse.
如果你愿意花一点钱,
JProfiler:
http://www.ej-technologies.com/products/jprofiler/overview。 html
非常好,它显示了所用时间的百分比、所用绝对时间以及方法级别的调用次数。它理解EJB调用、Web服务调用,甚至会显示jdbc调用的SQL。我经常使用它来查找性能问题。
它也有内存分析,但我发现 cpu 分析更有用。
If you are willing to spend a little money,
JProfiler:
http://www.ej-technologies.com/products/jprofiler/overview.html
is very good, it shows you % of time used, absolute time used, and # of invocations down to the method level. It understands EJB calls, web service calls, and will even show the SQL of jdbc calls. I use it frequently to find performance issues.
It has memory profiling too, but I find the cpu profiling much more useful.
这正是 AOP 可以发挥作用的地方。无需更改代码即可添加/删除方面。
如果你使用Spring;创建包括 JoinPoint 在内的方面、列出用于评估执行时间的类和公共方法的建议。将这些 bean 添加到 Spring 配置中。
否则使用 AspectJ 容器进行非 Spring 应用程序
Exactly this is where AOP helps. Aspects can be added/removed without changing code.
If you are using Spring; create Aspect including JoinPoint, Advice listing the Classes and public methods that are to evaluate execution time. Add these beans to the Spring config.
Else use AspectJ Container for non-spring application