Java:如何查看代码的哪些部分运行最多? (分析)

时间:2022-01-09 08:18:56

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).

我正在用Java编写一个简单的跳棋游戏。当我将鼠标悬停在电路板上时,我的处理器会上升到50%(核心上为100%)。

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.

有什么工具可以告诉我问题出在哪里吗?我目前正在使用Eclipse。

12 个解决方案

#1


12  

This is called "profiling". Your IDE probably comes with one: see Open Source Profilers in Java.

这称为“剖析”。您的IDE可能附带一个:请参阅Java中的开源配置文件。

#2


3  

Use a profiler (e.g yourkit )

使用分析器(例如yourkit)

#3


2  

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.

剖析?我不知道你使用的是什么IDE,但Eclipse有一个不错的proflier,还有一些java-source的开源分析器列表。

#4


2  

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.)

我没有详细介绍我的程序,所以我没有太多的经验,但是当我测试它时,我已经使用过NetBeans IDE分析器。 (我通常也使用Eclipse。我还将研究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.

NetBeans探查器将告诉您哪个线程正在执行多长时间,以及哪些方法已被调用多长时间,并将为您提供条形图以显示每个方法花费的时间。这应该会给你一个关于哪种方法导致问题的提示。如果您感到好奇,可以查看NetBeans IDE提供的Java概要分析器。

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!

#5


1  

1) It is your fault :)

1)这是你的错:)

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.

2)如果您正在使用eclipse或netbeans,请尝试使用分析功能 - 它应该很快告诉您代码花费了大量时间。

3) failing that, add console output where you think the inner loop is -- you should be able to find it quickly.

3)如果失败了,在你认为内循环的地方添加控制台输出 - 你应该能够快速找到它。

#6


1  

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.

是的,有这样的工具:你必须分析代码。您可以在eclipse中尝试TPTP,也可以尝试使用JProfiler。这将让你看到被调用的内容和频率。

#7


1  

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.

使用分析器。有许多。这是一个列表:http://java-source.net/open-source/profilers。例如,您可以使用JIP,一个Java编码的分析器。

#8


1  

Clover will give a nice report showing hit counts for each line and branch. For example, this line was executed 7 times.

Clover将提供一个很好的报告,显示每个线和分支的命中数。例如,这条线被执行了7次。

Plugins for Eclipse, Maven, Ant and IDEA are available. It is free for open source, or you can get a 30 day evaluation license.

可以使用Eclipse,Maven,Ant和IDEA的插件。它是免费的开源,或者您可以获得30天的评估许可证。

#9


1  

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.

如果您使用的是Sun Java 6,那么最新的JDK版本会在bin目录中附带JVisualVM。这是一个功能强大的监视和分析工具,只需要很少的工作量 - 您甚至不需要使用特殊参数启动程序 - JVisualVM只列出所有当前运行的java进程,并选择要使用的进程。

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.

有很多更强大的工具,但首先要有一个免费的游戏。然后,当您了解其他功能的可用性时,您将获得有关它们如何帮助您的信息。

#10


1  

This is a typically 'High CPU' problem.

这通常是“高CPU”问题。

There are two kind of high CPU problems

有两种高CPU问题

a) Where on thread is using 100% CPU of one core (This is your scenario)

a)在线程上使用100%CPU的一个核心(这是你的场景)

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.

b)当我们执行某些操作时,CPU使用率“异常高”。在这种情况下,CPU可能不是100%但会异常高。通常,当我们在代码中进行CPU密集型操作(如XML解析,序列化反序列化等)时会发生这种情况。

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)

案例(a)易于分析。当您在30秒间隔内遇到100%CPU 5-6线程转储时。查找一个活动的线程(处于“runnable”状态)并且在同一个方法中(您可以通过监视线程堆栈来推断)。很可能你会看到“忙碌的等待”(参见下面的代码示例)

while(true){
  if(status) break;
  // Thread.sleep(60000); // such a statement would have avoided busy wait
}

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.

情况(b)也可以使用等间隔的线程转储进行分析。如果幸运的话,您将能够找到问题代码,如果您无法使用线程转储识别问题代码。您需要求助于分析器。根据我的经验,YourKit profiler非常好。

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.

我总是首先尝试使用线程转储。 Profilers只是最后的选择。在80%的情况下,我们将能够识别使用线程转储。

#11


0  

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.

或者使用JUnit测试用例和代码覆盖工具来处理你的一些常见组件。如果有组件调用其他组件,您将很快看到执行多次的组件。

I use Clover with JUnit test cases, but for open-source, I hear EMMA is pretty good.

我使用Clover和JUnit测试用例,但对于开源,我听说EMMA非常好。

#12


0  

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.

在单线程代码中,我发现添加了一些这样的语句:System.out.println(“A:”+ System.currentTimeMillis());比使用分析器更简单,更有效。您可以很快缩小导致问题的代码部分。

#1


12  

This is called "profiling". Your IDE probably comes with one: see Open Source Profilers in Java.

这称为“剖析”。您的IDE可能附带一个:请参阅Java中的开源配置文件。

#2


3  

Use a profiler (e.g yourkit )

使用分析器(例如yourkit)

#3


2  

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.

剖析?我不知道你使用的是什么IDE,但Eclipse有一个不错的proflier,还有一些java-source的开源分析器列表。

#4


2  

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.)

我没有详细介绍我的程序,所以我没有太多的经验,但是当我测试它时,我已经使用过NetBeans IDE分析器。 (我通常也使用Eclipse。我还将研究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.

NetBeans探查器将告诉您哪个线程正在执行多长时间,以及哪些方法已被调用多长时间,并将为您提供条形图以显示每个方法花费的时间。这应该会给你一个关于哪种方法导致问题的提示。如果您感到好奇,可以查看NetBeans IDE提供的Java概要分析器。

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!

#5


1  

1) It is your fault :)

1)这是你的错:)

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.

2)如果您正在使用eclipse或netbeans,请尝试使用分析功能 - 它应该很快告诉您代码花费了大量时间。

3) failing that, add console output where you think the inner loop is -- you should be able to find it quickly.

3)如果失败了,在你认为内循环的地方添加控制台输出 - 你应该能够快速找到它。

#6


1  

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.

是的,有这样的工具:你必须分析代码。您可以在eclipse中尝试TPTP,也可以尝试使用JProfiler。这将让你看到被调用的内容和频率。

#7


1  

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.

使用分析器。有许多。这是一个列表:http://java-source.net/open-source/profilers。例如,您可以使用JIP,一个Java编码的分析器。

#8


1  

Clover will give a nice report showing hit counts for each line and branch. For example, this line was executed 7 times.

Clover将提供一个很好的报告,显示每个线和分支的命中数。例如,这条线被执行了7次。

Plugins for Eclipse, Maven, Ant and IDEA are available. It is free for open source, or you can get a 30 day evaluation license.

可以使用Eclipse,Maven,Ant和IDEA的插件。它是免费的开源,或者您可以获得30天的评估许可证。

#9


1  

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.

如果您使用的是Sun Java 6,那么最新的JDK版本会在bin目录中附带JVisualVM。这是一个功能强大的监视和分析工具,只需要很少的工作量 - 您甚至不需要使用特殊参数启动程序 - JVisualVM只列出所有当前运行的java进程,并选择要使用的进程。

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.

有很多更强大的工具,但首先要有一个免费的游戏。然后,当您了解其他功能的可用性时,您将获得有关它们如何帮助您的信息。

#10


1  

This is a typically 'High CPU' problem.

这通常是“高CPU”问题。

There are two kind of high CPU problems

有两种高CPU问题

a) Where on thread is using 100% CPU of one core (This is your scenario)

a)在线程上使用100%CPU的一个核心(这是你的场景)

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.

b)当我们执行某些操作时,CPU使用率“异常高”。在这种情况下,CPU可能不是100%但会异常高。通常,当我们在代码中进行CPU密集型操作(如XML解析,序列化反序列化等)时会发生这种情况。

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)

案例(a)易于分析。当您在30秒间隔内遇到100%CPU 5-6线程转储时。查找一个活动的线程(处于“runnable”状态)并且在同一个方法中(您可以通过监视线程堆栈来推断)。很可能你会看到“忙碌的等待”(参见下面的代码示例)

while(true){
  if(status) break;
  // Thread.sleep(60000); // such a statement would have avoided busy wait
}

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.

情况(b)也可以使用等间隔的线程转储进行分析。如果幸运的话,您将能够找到问题代码,如果您无法使用线程转储识别问题代码。您需要求助于分析器。根据我的经验,YourKit profiler非常好。

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.

我总是首先尝试使用线程转储。 Profilers只是最后的选择。在80%的情况下,我们将能够识别使用线程转储。

#11


0  

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.

或者使用JUnit测试用例和代码覆盖工具来处理你的一些常见组件。如果有组件调用其他组件,您将很快看到执行多次的组件。

I use Clover with JUnit test cases, but for open-source, I hear EMMA is pretty good.

我使用Clover和JUnit测试用例,但对于开源,我听说EMMA非常好。

#12


0  

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.

在单线程代码中,我发现添加了一些这样的语句:System.out.println(“A:”+ System.currentTimeMillis());比使用分析器更简单,更有效。您可以很快缩小导致问题的代码部分。