java 如何找出哪种方法需要花费很多时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5139354/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 09:38:49  来源:igfitidea点击:

How to find out which method is taking much time?

javadebuggingtimeprofiling

提问by Rakesh Juyal

One of my component is taking too much time to execute. It calls lots of services which in turn calls many dao methods. Now, is there any way to get the time taken by each method it is calling.
I don't want to write System.currentmillisbefore and after each method to calculate time taken as there are too many methods.
I think i may need to use interceptors or may be any profiler can do that. I am not sure, please help.

我的一个组件需要太多时间来执行。它调用了许多服务,这些服务又调用了许多 dao 方法。现在,有没有办法获得它正在调用的每个方法所花费的时间。
我不想System.currentmillis在每个方法之前和之后写来计算花费的时间,因为方法太多了。
我想我可能需要使用拦截器,或者任何分析器都可以做到这一点。我不确定,请帮忙。

采纳答案by posdef

Use jvisualvmwhich should come with JDK (if I remember correctly). It's a GUI for your JVM, and has really nice functions. Check out its featuresthere are some screenshots as well...

使用jvisualvmJDK 应该附带的(如果我没记错的话)。它是您的 JVM 的 GUI,并且具有非常好的功能。查看它的功能,还有一些屏幕截图......

And you can follow these steps to integrate it as launcher in eclipse Steps to integrate in eclipse

您可以按照以下步骤将其集成为 eclipse 中的启动器 集成到 eclipse 中的步骤

回答by Mike Dunlavey

Don't look at it as measuring time to find the problem.

不要把它看作是衡量发现问题的时间。

Use its slowness to expose it. Just use the same method you would if it were an infinite loop.

用它的缓慢来暴露它。只需使用与无限循环相同的方法即可。

That is, pause it a few times while it's being slow, and each time inspect the call stack of each thread. The guilty methods and lines of code will appear on multiple samples. Check the last paragraph of this post.

也就是说,在它变慢的时候暂停它几次,每次检查每个线程的调用堆栈。有罪的方法和代码行将出现在多个样本中。检查这篇文章最后一段

Here's more on how it works.

这里有更多关于它是如何工作的。

回答by Tom W

You can use the Java Profiler in the NetBeans IDE.

您可以在 NetBeans IDE 中使用 Java Profiler。

Have a look here: http://netbeans.org/features/java/profiler.html

看看这里:http: //netbeans.org/features/java/profiler.html

The Java VisualVM is another alternative: http://java.net/projects/visualvm/content/. You can attach it to any running Java program, including ones you start through Eclipse.

Java VisualVM 是另一种选择:http: //java.net/projects/visualvm/content/。您可以将它附加到任何正在运行的 Java 程序,包括您通过 Eclipse 启动的程序。