Java:如何查看代码的哪些部分运行最多? (分析)
我正在用 Java 编写一个简单的跳棋游戏。 当我将鼠标悬停在主板上时,我的处理器性能提升至 50%(核心上为 100%)。
我想找出我的代码的哪一部分(假设是我的错)在此期间正在执行。
我尝试过调试,但在这种情况下逐步调试效果不太好。
有没有什么工具可以告诉我问题出在哪里? 我目前正在使用 Eclipse。
I am writing a simple checkers game in Java. When I mouse over the board my processor ramps up to 50% (100% on a core).
I would like to find out what part of my code(assuming its my fault) is executing during this.
I have tried debugging, but step-through debugging doesn't work very well in this case.
Is there any tool that can tell me where my problem lies? I am currently using Eclipse.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
如果您使用的是 Sun Java 6,则最新的 JDK 版本在 bin 中附带 JVisualVM目录。 这是一个功能强大的监视和分析工具,使用起来只需很少的努力 - 您甚至不需要使用特殊参数启动程序 - JVisualVM 简单地列出所有当前正在运行的 java 进程,您可以选择您想要使用的进程。
该工具将告诉您哪些方法正在使用所有处理器时间。
有很多更强大的工具,但首先使用免费的工具。 然后,当您了解其他可用功能时,您就会了解它们如何为您提供帮助。
If you're using Sun Java 6, then the most recent JDK releases come with JVisualVM in the bin directory. This is a capable monitoring and profiling tool that will require very little effort to use - you don't even need to start your program with special parameters - JVisualVM simply lists all the currently running java processes and you choose the one you want to play with.
This tool will tell you which methods are using all the processor time.
There are plenty of more powerful tools out there, but have a play with a free one first. Then, when you read about what other features are available out there, you'll have an inking about how they might help you.
Clover 将提供一个很好的报告,显示每行和分支的命中数。 例如,此line 被执行了 7 次。
提供 Eclipse、Maven、Ant 和 IDEA 插件。 它免费开源,或者您可以获得30 天评估许可证。
Clover will give a nice report showing hit counts for each line and branch. For example, this line was executed 7 times.
Plugins for Eclipse, Maven, Ant and IDEA are available. It is free for open source, or you can get a 30 day evaluation license.
使用分析器。 有许多。 这是一个列表: http://java-source.net/open-source/profilers。
例如,您可以使用 JIP,一个 Java 编码的分析器。
Use a profiler. There are many. Here is a list: http://java-source.net/open-source/profilers.
For example you can use JIP, a java coded profiler.
或者对您的一些常见组件使用 JUnit 测试用例和代码覆盖工具。 如果有组件调用其他组件,您很快就会看到这些组件被执行了很多次。
我将 Clover 与 JUnit 测试用例一起使用,但对于开源,我听说 EMMA 相当不错。
Or use JUnit test cases and a code coverage tool for some common components of yours. If there are components that call other components, you'll quickly see those executed many more times.
I use Clover with JUnit test cases, but for open-source, I hear EMMA is pretty good.
在单线程代码中,我发现添加了一些如下语句:
System.out.println("A: "+ System.currentTimeMillis());
与使用分析器一样简单且有效。 您很快就可以缩小导致问题的代码部分的范围。
In single-threaded code, I find adding some statements like this:
System.out.println("A: "+ System.currentTimeMillis());
is simpler and as effective as using a profiler. You can soon narrow down the part of the code causing the problem.
这是典型的“CPU 高”问题。
有两种高 CPU 问题
a) 线程使用一个核心 100% CPU(这是您的场景)
b) 当我们执行某些操作时,CPU 使用率“异常高”。 在这种情况下,CPU 可能不是 100%,而是异常高。 通常,当我们在代码中进行 CPU 密集型操作(例如 XML 解析、序列化反序列化等)时,就会发生这种情况。
情况 (a) 很容易分析。 当您在 30 秒的时间间隔内经历 100% CPU 5-6 线程转储时。 查找处于活动状态(处于“可运行”状态)并且位于同一方法内的线程(您可以通过监视线程堆栈来推断)。 您很可能会看到“忙等待”(请参阅下面的代码以获取示例)
情况 (b) 也可以使用以相等间隔进行的线程转储进行分析。 如果幸运的话,您将能够找到问题代码,如果您无法使用线程转储来识别问题代码。 您需要求助于分析器。 根据我的经验,YourKit 分析器非常好。
我总是首先尝试使用线程转储。 分析器只是最后的手段。 在 80% 的情况下,我们将能够使用线程转储来识别。
This is a typically 'High CPU' problem.
There are two kind of high CPU problems
a) Where on thread is using 100% CPU of one core (This is your scenario)
b) CPU usage is 'abnormally high' when we execute certain actions. In such cases CPU may not be 100% but will be abnormally high. Typically this happens when we have CPU intensive operations in the code like XML parsing, serialization de-serialization etc.
Case (a) is easy to analyze. When you experience 100% CPU 5-6 thread dumps in 30 sec interval. Look for a thread which is active (in "runnable" state) and which is inside the same method (you can infer that by monitoring the thread stack). Most probably that you will see a 'busy wait' (see code below for an example)
Case (b) also can be analyzed using thread dumps taken in equal interval. If you are lucky you will be able to find out the problem code, If you are not able to identify the problem code by using thread dump. You need to resort to profilers. In my experience YourKit profiler is very good.
I always try with thread dumps first. Profilers will only be last resort. In 80% of the cases we will be able to identify using thread dumps.
是的,有这样的工具:您必须分析代码。 您可以在 Eclipse 中尝试 TPTP 或尝试 TPTP 。 ej-technologies.com/products/jprofiler/overview.html" rel="nofollow noreferrer">JProfiler。 这会让您看到正在调用什么以及调用的频率。
Yes, there are such tools: you have to profile the code. You can either try TPTP in eclipse or perhaps try JProfiler. That will let you see what is being called and how often.
1) 这是你的错:)
2) 如果你使用的是 eclipse 或 netbeans,请尝试使用分析功能——它应该很快告诉你你的代码在哪里花费了很多时间。
3)如果失败,请在您认为内部循环所在的位置添加控制台输出——您应该能够快速找到它。
1) It is your fault :)
2) If you're using eclipse or netbeans, try using the profiling features -- it should pretty quickly tell you where your code is spending a lot of time.
3) failing that, add console output where you think the inner loop is -- you should be able to find it quickly.
简而言之,分析器会告诉您程序的哪一部分正在被分析经常打电话多少次。
我没有太多地分析我的程序,所以我没有太多的经验,但我已经尝试过 NetBeans IDE 分析器。 (我通常也使用 Eclipse。我还将研究 Eclipse 中的分析功能。)
NetBeans 分析器将告诉您哪个线程执行了多长时间,以及哪些方法被调用了多长时间,并为您提供条形图显示每种方法花费了多少时间。 这应该会提示您哪个方法导致了问题。 您可以查看 Java profiler如果您好奇的话,NetBeans IDE 提供了。
分析是一种通常用于测量程序的哪些部分占用大量执行时间的技术,进而可用于评估执行优化是否有利于提高程序的性能。
祝你好运!
In a nutshell, profilers will tell you which part of your program is being called how many often.
I don't profile my programs much, so I don't have too much experience, but I have played around with the NetBeans IDE profiler when I was testing it out. (I usually use Eclipse as well. I will also look into the profiling features in Eclipse.)
The NetBeans profiler will tell you which thread was executing for how long, and which methods have been called how long, and will give you bar graphs to show how much time each method has taken. This should give you a hint as to which method is causing problems. You can take a look at the Java profiler that the NetBeans IDE provides, if you are curious.
Profiling is a technique which is usually used to measure which parts of a program is taking up a lot of execution time, which in turn can be used to evaluate whether or not performing optimizations would be beneficial to increase the performance of a program.
Good luck!
分析? 我不知道你使用的是什么IDE,但是 Eclipse 有一个不错的分析器,并且在 java 源。
Profiling? I don't know what IDE you are using, but Eclipse has a decent proflier and there is also a list of some open-source profilers at java-source.
使用分析器(例如 yourkit )
Use a profiler (e.g yourkit )
这称为“分析”。 您的 IDE 可能附带一个:请参阅Java 中的开源分析器。
This is called "profiling". Your IDE probably comes with one: see Open Source Profilers in Java.