简体   繁体   English

跟踪 java 程序执行

[英]trace java program execution

I am having a hard time finding some program to trace the execution of a java program.我很难找到一些程序来跟踪 java 程序的执行。

For a starter just something that would give me like all method calls and time spent doing each call would be nice.对于初学者来说,就像所有方法调用和每次调用所花费的时间一样,这会给我带来好处。

Do you have any suggestion?你有什么建议吗? Thank you in advance先感谢您

EDIT: i would like to get the whole call tree, or hierarchy, whatever it is exactly called, from startup, with time spent, and i couldn't get it with VisualVM.编辑:我想从启动时获得整个调用树或层次结构,无论它被确切称为什么,花费时间,而我无法使用 VisualVM 获得它。 I found a screenshot of some tools that supposedly could do it but i haven't gone through with it and i would like something more simple to begin with:我找到了一些据说可以做到的工具的屏幕截图,但我还没有完成它,我想要一些更简单的开始: 在此处输入图像描述

That would be pretty close to what i have in mind: it gives a trace of some method call tree, the time spent in each method, even a representation of it.那将非常接近我的想法:它给出了一些方法调用树的踪迹,每个方法花费的时间,甚至是它的表示。

What were the original problems i was trying to solve?我试图解决的最初问题是什么? Here is almost the whole story: First, i am working on some tool that has to populate some knowledge base.这几乎是整个故事:首先,我正在开发一些必须填充一些知识库的工具。 I recently realized that it was pretty slow (some specific request, that is adding ~30 entities to the base would take more than 20 seconds), so i would like to know if the time is spent on my code, on the network (REST call to update that knowledge base), in the knowledge base itself, or in my code.我最近意识到它很慢(一些特定的请求,即向基础添加约 30 个实体需要 20 多秒),所以我想知道时间是否花在我的代码上,在网络上(REST调用以更新该知识库)、知识库本身或我的代码中。 And if my code is slow, i want to know what exactly it is doing that is taking so much time.如果我的代码很慢,我想知道它到底在做什么需要这么多时间。

Another reason is that i was reading "Effective Java 3rd edition", great book btw.另一个原因是我正在阅读“有效的 Java 第 3 版”,顺便说一句很棒的书。 In the item 6 "Avoid creating unnecessary objects"在第 6 项“避免创建不必要的对象”中

There is this code snippet:有这个代码片段:

// Hideously slow! Can you spot the object creation?
private static long sum() {
  Long sum = 0L;
  for (long i = 0; i <= Integer.MAX_VALUE; i++)
    sum += i;
  return sum;
}

compared to that one:与那个相比:

private static long sum() {
  long sum = 0L;
  for (long i = 0; i <= Integer.MAX_VALUE; i++)
    sum += i;
  return sum;
}

I can see that he second version will take less time, i understand why, there are no Long object instanciated, etc. but i would like to see, with actual measures, how long exactly it is taking doing instanciations, see with measures the number of instances of Long, and other objects are being created, what percentage of memory Long objects are taking in that case etc.我可以看到他的第二个版本会花费更少的时间,我明白为什么,没有实例化 Long object 等,但我想用实际措施看看它究竟需要多长时间来做实例化,用措施看看数字Long 的实例和正在创建的其他对象,memory Long 对象在这种情况下所占的百分比等。

And some other experiences at work, where we would blindly do some great deal of refactoring, some heated debates about whether some implementation X were "more efficient" than implementation Y, but actually we never have any data to back it up.还有一些其他的工作经验,我们会盲目地进行大量的重构,一些关于某些实现 X 是否比实现 Y“更有效”的激烈辩论,但实际上我们从来没有任何数据来支持它。

All in all, i realize i needed tool to get actual and solid data about what the code is doing, what is happening in the JVM, how much memory is being allocated, by what object, how much time is being spent doing what, etc.总而言之,我意识到我需要工具来获取有关代码正在做什么的实际和可靠数据,JVM 中发生了什么,memory 分配了多少,ZA8CFDE6331BD59EB2AC96F8911C4B66 花费了多少时间等等.

That contains A LOT of specific questions, and my first one was this one: "how can i trace the execution of even a simple program".其中包含很多具体问题,我的第一个问题是:“我如何跟踪一个简单程序的执行情况”。

Thank you and sorry for not being super specific to begin with.谢谢你,很抱歉一开始就没有特别具体。

And the other post that was suggested didn't help THAT much, using VisualVM with the plugin Profile Startup, on the toy example from Effective Java, i can get something when i count up to Integer.MAX_VALUE,建议的另一篇文章并没有太大帮助,在 Effective Java 的玩具示例中使用 VisualVM 和插件 Profile Startup,当我数到 Integer.MAX_VALUE 时,我可以得到一些东西,

在此处输入图像描述

but if i only count up to 1.000.000 for example it looks like VisualVM doesn't have time to get started (although i thought launching my program with the option -agentpath:/home/joseph/dev/visualvm_142/profiler/lib/deployed/jdk16/linux-amd64/libprofilerinterface.so=/home/joseph/dev/visualvm_142/profiler/lib,5140 would let VisualVM start but all i get is a blank screen with "Status: profiling inactive" but it's all the same as the execution where i was counting to Integer.MAX_VALUE, i just dont' get it):但是如果我只数到 1.000.000 例如它看起来 VisualVM 没有时间开始(虽然我想用选项启动我的程序 -agentpath:/home/joseph/dev/visualvm_142/profiler/lib/ deploy/jdk16/linux-amd64/libprofilerinterface.so=/home/joseph/dev/visualvm_142/profiler/lib,5140 会让 VisualVM 启动,但我得到的只是一个空白屏幕,上面有“状态:分析不活动”,但都是一样的作为我计数到 Integer.MAX_VALUE 的执行,我只是不明白):

在此处输入图像描述

This little "Trace" tool developed by Oracle might help you: https://docs.oracle.com/javase/7/docs/technotes/guides/jpda/trace.html This little "Trace" tool developed by Oracle might help you: https://docs.oracle.com/javase/7/docs/technotes/guides/jpda/trace.html

It is based on Java Debug Interface (JDI).它基于 Java 调试接口 (JDI)。

I was really keen on using it, but I have spent 10 minutes looking for the Oracle examples to download, without being able to find them.我真的很想使用它,但我花了 10 分钟寻找要下载的 Oracle 示例,但找不到它们。

In the past I have used a lot BTrace https://github.com/btraceio/btrace , I see that it's still being actively maintained.过去我使用了很多 BTrace https://github.com/btraceio/btrace ,我看到它仍在积极维护中。

The correct term is profiling .正确的术语是profiling Because of the long list of long (Long?) comments I make it an answer.由于长长的(长?)评论列表,我将其作为答案。

Your commercial IntelliJ IDEA, and the NetBeans IDE have a profiler, where you can start a profile, take snapshots to compare, find critical paths, memory usage, bottlenecks, time durations.您的商业 IntelliJ IDEA 和 NetBeans IDE 有一个分析器,您可以在其中启动配置文件、拍摄快照以进行比较、查找关键路径、memory 使用情况、持续时间、瓶颈。 You might want to compare them, though IntelliJ is likely to be better.您可能想比较它们,尽管 IntelliJ 可能会更好。

Then there are code style checkers like SonarLint, that just may find things like calculating with wrapped types.然后有像 SonarLint 这样的代码样式检查器,它可能会发现诸如使用包装类型进行计算之类的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM