Java 分析 - 如何通过对我的应用程序的方法分析来获取方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2267594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java profiling - how can I get a method by method analysis of my application?
提问by MalcomTucker
I want to run my Java app and for a given workload be able to see:
我想运行我的 Java 应用程序,并且对于给定的工作负载能够看到:
- 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
谢谢
Editjvisualvm 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
编辑jvisualvm 看起来像这个工具 - 它在大约 30 秒内确定了问题。我只需要知道在方法配置文件的上下文中“selftime”是什么意思。谢谢
采纳答案by Thorbj?rn Ravn Andersen
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.
对于在 Sun 的 java 6 中运行的程序,最简单的方法是使用 jdk 中的 jvisualvm 程序。允许您在没有任何特殊设置的情况下附加和配置文件。
回答by Thomas L?tzer
Take a look at Eclipse TPTP. They can provide exactly that and much more for any application started from Eclipse.
看看Eclipse TPTP。它们可以为从 Eclipse 启动的任何应用程序提供完全相同的功能以及更多功能。
回答by gubby
Easiest way is to use -prof, e.g: java -prof -jar yourjar.jar
最简单的方法是使用 -prof,例如:java -prof -jar yourjar.jar
That will print a file called java.prof after the program has finished running.
这将在程序运行完成后打印一个名为 java.prof 的文件。
See the HPROF documentation page
请参阅HPROF 文档页面
In my application I use: -Xrunhprof:cpu=samples,thread=y,doe=y
在我的应用程序中,我使用: -Xrunhprof:cpu=samples,thread=y,doe=y
This prints a report that contains, amongst other things, this:
这将打印一份报告,其中包含以下内容:
CPU SAMPLES BEGIN (total = 55110) Sun Feb 7 17:02:51 2010
rank self accum count trace method
1 69.68% 69.68% 38399 300361 java.net.SocketInputStream.socketRead0
2 24.40% 94.08% 13448 300386 java.net.SocketInputStream.socketRead0
3 0.20% 94.28% 108 300425 java.io.FileOutputStream.writeBytes
4 0.19% 94.47% 107 300976 java.net.PlainDatagramSocketImpl.receive0
5 0.19% 94.65% 102 300414 package.BlockingSampleBuffer.addSample
6 0.16% 94.82% 90 300365 java.net.SocketOutputStream.socketWrite0
7 0.16% 94.98% 89 300412 package.BlockingSampleBuffer.addSample
8 0.15% 95.13% 84 300430 java.lang.Object.wait
9 0.14% 95.27% 77 300592 java.io.FileOutputStream.writeBytes
10 0.14% 95.41% 76 300566 java.lang.AbstractStringBuilder.<init>
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).
所以你可以看到在各种方法中花费的总时间(以秒为单位)。在我的应用程序中,大部分时间都在等待来自远程主机的数据(不太可能通过互联网连接)。
回答by karoberts
If you are willing to spend a little money,
如果你愿意花一点钱,
JProfiler: http://www.ej-technologies.com/products/jprofiler/overview.html
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.
非常好,它向您显示了使用时间的百分比、使用的绝对时间以及直到方法级别的调用次数。它理解 EJB 调用、Web 服务调用,甚至会显示 jdbc 调用的 SQL。我经常使用它来查找性能问题。
It has memory profiling too, but I find the cpu profiling much more useful.
它也有内存分析,但我发现 cpu 分析更有用。
回答by Bill K
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.
已经列出了几个分析器(Eclipse one 和 JProfiler)。我只想强烈推荐探查器是您编程工具箱中的工具之一。
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.
顺便说一下,它们比 java 工具的静态输出强大得多——尽管在这种特定情况下 java 工具可能就足够了。分析器可以告诉您每个线程在做什么,并且可以制作一些非常酷的调用图形(流程图样式),以帮助您分析不是您编写的代码。
Just find one, and use it for a week or two so that you know what it offers.
只需找到一个,并使用它一两个星期,这样您就知道它提供了什么。
回答by Hussain
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
这正是 AOP 帮助的地方。可以在不更改代码的情况下添加/删除方面。如果您使用的是 Spring;创建方面,包括 JoinPoint、Advice 列出评估执行时间的类和公共方法。将这些 bean 添加到 Spring 配置中。否则将 AspectJ Container 用于非 Spring 应用程序
回答by manikanta
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).
Java 1.7* 与 Java Mission Control (jmc) 捆绑在一起,它具有“飞行记录器”功能,可用于分析方法执行情况。分析结果的显示几乎与 AppDynamics 类似 - 很容易发现性能问题(尤其是哪些方法占用了所有 CPU)。
Though not detailed, but good blog post explaining the about Flight Recorder: http://hirt.se/blog/?p=364
虽然不详细,但很好的博客文章解释了飞行记录器:http: //hirt.se/blog/?p=364
* Not sure about the minor version
* 不确定次要版本