VisualVM 未显示任何调用 CPU 性能分析的方法

发布于 2024-11-19 01:24:33 字数 332 浏览 5 评论 0原文

我正在运行 Java 1.6_21 SDK 构建应用程序。它是在 Eclipse 中构建的,我使用 VistualVM eclipse 插件在应用程序启动时启动 VisualVM。

当我转到“配置文件”选项卡并单击“CPU 分析”时,它仅显示正在运行的线程,但不显示任何方法调用。几乎没有。我用谷歌搜索了一大堆东西,但似乎没有一个合适的。有人见过这个问题吗?有解决办法吗??

环境:Windows XP 32位 “经典”Eclipse SDK 3.7.0 SDK 1.6_21 中的 VisualVM

对我来说没有任何意义。我可以获得内存堆信息,但成员调用信息为零。

非常感谢任何帮助。

I'm running a Java 1.6_21 SDK build app. Its been build in Eclipse, and i'm using the VistualVM eclipse plugin to launch VisualVM when the app starts.

When I go to the Profile tab and click CPU profiling, It only shows the THREADS that are running, but it doesn't show any METHOD calls. Quite litterally, none. I've googled a whole bunch of things, but nothing seems to fit. has ANYONE seen this problem? Is there a solution??

Environment: Windows XP 32-bit
"Classic" Eclipse SDK 3.7.0
VisualVM from SDK 1.6_21

It doesn't make any sense to me. I can get memory heap information, but zero member-calling information.

Any help is extremely appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夕色琉璃 2024-11-26 01:24:33

您是否尝试过设置要分析的类。

选择窗口右上角的“设置”复选框设置需要分析的包。

have you tried setting classes to be profiled.

select the Settings checkbox in the top right cornet of the window & set the packages which needs to be profiled.

成熟的代价 2024-11-26 01:24:33

请参阅使用 VisualVM 进行分析,第 1 部分< /a> 和 使用 VisualVM 进行分析,第 2 部分了解如何设置 CPU 设置。

注意:如果有人有 VisualVM,那么替换文章中的屏幕截图将是一个很好的补充,因为它们没有以良好的分辨率保存,或者一开始质量就很差

VisualVM 进行分析,第 1 部分

使用 VisualVM 进行分析,第 1 部分
作者:Jiri Sedlacek,发表于 2008 年 7 月 28 日

本文介绍了 VisualVM 工具的开箱即用的分析功能,无需任何其他配置,只需使用默认设置。

除了各种监视功能之外,该工具还包含一个基于 NetBeans 分析器。虽然 VisualVM 中的分析器 UI 看起来很简单(尤其是与 NetBeans 分析器相比),但分析功能几乎与 NetBeans 中的一样强大。这两个工具之间的主要区别在于它们的工作流程:NetBeans 探查器以项目/源为中心,是在应用程序开发过程中进行迭代测试/提高性能的绝佳工具,而 VisualVM 可以用作独立的探查器,可与任何正在运行的应用程序配合使用,而无需任何操作。具有源代码的任何上下文。

注意:对于在 JDK 6.0 Update 6 及更早版本上运行的分析应用程序,需要使用 Xshare:off 标志来启动应用程序,该标志会禁用类共享,否则分析可能会导致分析的 JVM 崩溃。对于在 JDK 6.0 Update 7 及更高版本上运行的应用程序,分析应该无需任何修改即可工作。

VisualVM 分析主屏幕

分析 UI 非常简单,默认情况下它仅显示三个按钮,用于分析性能(CPU 按钮)、内存使用情况(内存按钮)和停止分析会话(停止按钮)。其他 UI 元素是显示实际分析状态的状态区域和显示分析会话期间收集的实时分析数据的分析结果部分。对于高级分析,还有一个默认情况下隐藏的“设置”部分。它允许微调分析设置,[使用 VisualVM 进行分析,第 2 部分文章](#使用 VisualVM 进行分析,第 2 部分文章)对此进行了详细描述。

注意:虽然分析器选项卡适用于每个可分析的应用程序,但一次只能分析一个应用程序,但不支持并发分析会话。

分析性能

当按下 Profiler 选项卡中的 CPU 按钮时,分析​​器会连接到应用程序并开始分析其性能。一开始,探查器需要检测应用程序的某些方法,检测的方法的实际数量显示在“状态”区域中。由于分析器使用动态检测,因此检测方法的数量可能会在分析会话期间发生变化。连接探查器并开始收集数据后,视图如屏幕截图所示:

附加 Profiler 后 VisualVM 的照片

您可以查看应用程序的实时分析数据,包括每个分析方法的调用次数和总数显示执行时间。请注意,并非所有方法都会被分析,默认情况下 分析器不会分析来自 java.*、javax.*、sun.*、sunw.*、com.sun.* 的方法 (+ Mac OS X 上的 >com.apple.*apple.awt.*)软件包。这些包中的方法所花费的时间会添加到调用它们的分析方法的执行时间中,例如自定义类 (CustomClass.) 的构造函数执行时间包括超级构造函数等的执行时间

。 sstatic.net/5GOGn.png" rel="nofollow noreferrer">VisualVM Profiler Toolbar

分析工具栏结果部分包含以下操作(从左至右):

  • 自动更新结果:结果大约定期刷新。每 1.2 秒
  • 立即更新结果:立即获取实际数据,通常在禁用自动刷新时使用
  • 运行垃圾收集:在分析应用程序中调用 GC
  • 重置收集的结果缓冲区< /strong>:重置迄今为止收集的数据,对于测量增量结果非常有用
  • 拍摄收集结果的快照:将所有收集的数据保存到持久快照中以进行更详细的分析
  • 将当前视图保存到图像:将结果表保存到 .png 图像中

最重要的操作是拍摄收集结果的快照(也可在应用程序树中已分析应用程序的上下文菜单中作为分析器快照使用)。它拍摄收集的分析结果的快照(与 NetBeans 分析器快照兼容)并在单独的选项卡中打开它。此快照提供了几种不同的视图:调用树显示以线程开头的方法调用树,热点显示按执行时间排序的所有分析方法的列表以及组合方法 显示调用树和分析方法列表的视图。最后一个视图 Info 显示快照的基本信息和快照的详细分析配置。每个方法的上下文菜单中都有一个方便的操作,称为显示返回跟踪,并显示调用该方法的所有位置。

分析器显示组合视图(大概是因为图像是漂亮的像素)Profiler 显示热点视图(大概是因为图像像素很漂亮)

可以为单个分析拍摄多个分析器快照例如,会话可以比较应用程序的各种情况/负载/配置下的性能。然后,所有探查器快照都可以保存到单个应用程序快照中(使用应用程序树中已分析应用程序的上下文菜单中的“应用程序快照”操作),将它们存档在一个文件中或将它们发送给应用程序开发人员/质量人员工程师。

分析内存

当按下“分析器”选项卡中的“内存”按钮时,分析​​器将连接到应用程序并开始分析其内存消耗。一开始,探查器需要检测应用程序的所有类,检测的类的实际数量显示在“状态”区域中。由于所有应用程序类都需要进行检测,因此探查器需要一些时间才能开始收集数据。为了降低分析开销,默认情况下会跟踪每 10 个对象分配(也显示在状态区域中)。评估分析结果时请记住这一点。连接探查器并开始收集数据后,视图如屏幕截图所示:

Profiler 显示演示应用程序的内存使用情况

您可以查看应用程序的实时分析数据,每个类的实时实例数和他们的显示尺寸。最右边的列显示幸存代指标,这对于检测内存泄漏非常有用。有关幸存一代的详细说明,请参阅 这篇文章。您还可以在一段时间内监视特定类的分配历史记录(底部的“历史记录”子选项卡),为此,请调用类上下文菜单中的记录类历史记录操作。

内存分析结果的工具栏与 CPU 分析结果的工具栏相同。您还可以使用分析结果部分工具栏中的“拍摄收集结果的快照”操作或分析器快照上下文菜单操作将收集的结果保存到快照中。这对于检测内存泄漏特别有用,使用“文件”菜单中提供的“比较内存快照”操作,您可以直观地显示两个可比较内存结果之间的差异,例如评估之后是否/哪些对象仍保留在堆上。执行一些动作。

分析器显示大概是内存配置文件快照

注意:停止分析会话(CPU 和内存)时,分析器会恢复原始应用程序字节码。这可能需要一些时间,并且应用程序和/或整个系统可能会暂时无法响应。

探查器校准

当 VisualVM 第一次启动时,探查器会执行初始校准。在此过程中,将测量整个系统和实际 JDK 性能,这些数据随后用于从收集的性能数据中减去分析开销。分析器要求系统具有“稳定”的性能,这意味着任何用于动态 CPU 频率切换的技术(如 SpeedStep 或 PowerNow!)在校准和分析过程中应禁用。

如果系统性能发生变化或者使用与校准后的不同的 JDK 进行分析,则收集到的性能结果可能会出现偏差,并且需要重新校准分析器。遗憾的是,VisualVM 1.0 中没有可用于重新校准的 UI 操作,再次调用校准的唯一方法是手动删除 /.nbprofiler/machinedata.jdk1X 中的校准数据文件并重新启动 VisualVM 。

使用 VisualVM 进行分析,第 2 部分

本文介绍了 VisualVM 工具,使用户可以更好地控制分析会话和收集的数据。您可能还想阅读[使用 VisualVM 进行分析,第 1 部分](#使用 VisualVM 进行分析,第 1 部分) 文章,其中描述了 VisualVM 的基本分析功能。

默认情况下,VisualVM 中的 Profiler UI 不显示任何设置,初始配置可确保即使是没有经验的用户,只需单击 CPU 或内存按钮即可通过零配置获得一些有用的结果。但是,高级用户仍然可以微调设置并充分利用 NetBeans 分析器引擎的功能。

要显示设置区域,必须选中 Profiler 选项卡右上角的设置复选框。设置区域由两个选项卡组成:CPU 设置内存设置,其中包含每种分析模式的控件。

VisualVM 设置

请注意,只有在分析会话未进行时才能更改设置,在分析过程中设置区域被禁用。

该区域底部还有一个恢复默认值按钮,可恢复初始配置。当值自定义不正确并且未收集分析数据时,这非常有用。

性能分析

对于性能分析,有两个重要的可用设置:分析根检测过滤器。一般来说,分析根定义分析器何时开始收集数据,而检测过滤器定义应该/不应该收集数据的包/类。

VisualVM CPU设置

从类开始分析文本框定义分析根。如果对所有方法进行检测,分析开销将是巨大的,并且分析最终将变得无法使用。这就是 NetBeans 分析器引入根方法概念的原因 - 一种仅分析感兴趣的代码而应用程序的其余部分全速运行的方法。

例如,典型的用例是分析通过单击按钮调用的操作的性能 - 如果将适当的 actionPerformed() 方法设置为根方法,则一旦 actionPerformed() 方法设置为根方法,分析器就会开始检测和分析应用程序>actionPerformed() 方法被调用,并且仅分析从 actionPerformed() 调用的方法。这样,分析开销非常小,并且只收集相关的分析结果,这大大提高了它们的可读性。

在 NetBeans 探查器中,选择根方法是非常自然的,因为它与编辑器紧密集成,但在 VisualVM 中,这可能有点棘手,而且不太用户友好。这就是 VisualVM 定义按类分析根的原因 - 这些类及其内部类中定义的所有方法都成为根方法。以类似的方式,包名称可以用作分析根。这就是默认分析根的定义方式:.**,这意味着 和子包中的所有方法都是异形。

默认设置在大多数情况下都有效,但对于应用程序服务器等大型应用程序,调整分析根以指向感兴趣的具体代码可能会很有用。例如,在分析 Web 应用程序时,最好将 servlet 的包或类名设置为分析根,而不是分析整个服务器。

在 VisualVM 中输入分析根的格式如下:

  • org.mypackage.**org.mypackage 中的所有方法和所有子包定义为根方法
  • org.mypackage.** 。 mypackage.* 将 org.mypackage 中的所有方法定义为根方法
  • org.mypackage.MyClass 定义 org.mypackage.MyClass 中的所有方法code> 及其内部类作为根方法为
  • 空文本框意味着应用程序中执行的所有方法都将被分析
    可以为单个分析会话定义多个分析根(由空格和/或逗号和/或换行符分隔)。

请注意,将 org.mypackage.MyClass.* 或 org.mypackage.MyClass.** 定义为分析根是没有意义的,因为该工具始终选择该类及其内部类的所有方法作为分析根。< /p>

仅分析类 / 不分析类文本框定义检测过滤器。这与 NetBeans 分析器中的快速过滤选项完全相同。它要么定义要分析的唯一包/类,要么定义不分析的包/类。例如,如果仅应分析应用程序的代码,而无需对其他框架或 JDK 代码进行任何传出调用,则应将此选项设置为仅分析类,.。如果应分析除 java 和 javax 包中的 JDK 类之外的所有代码,则应将此选项设置为“不分析类、java.、javax.”。默认的检测过滤器是“不分析类”、java.、javax.、sun.、sunw.、com.sun. (+ com.apple. ,apple.awt.(Mac OS X 上)),它会过滤掉所有 JDK 代码。

输入检测过滤器的格式如下:

  • org.mypackage.* 选择 org.mypackage 中的所有类以及要分析或排除的子包
  • org.mypackage. 选择所有要分析或排除的 org.mypackage 中的类
  • 空文本框选择要分析的应用程序的所有类
    可以为单个分析会话定义多个过滤器值(由空格和/或逗号和/或换行符分隔)。

##分析内存

对于内存分析,可以使用三个重要设置:分析范围跟踪对象的密度和记录分配堆栈跟踪

VisualVM 内存设置

分析范围由以下两个选项之一定义:对于分析对象分配选项,仅监视对象分配。这在评估应用程序分配了多少对象时非常有用。对于检查和识别内存泄漏,第二种选择更有用 - 分析对象分配和 GC。在这种模式下,分配的对象在整个生命周期内受到监视,从分配到垃圾回收。这允许轻松识别当前在某个点分配在堆上的活动对象。此外,在此模式下,“幸存一代”指标可用(有关更多信息,请参阅本文)。默认情况下,配置文件对象分配和 GC 选项处于选中状态。

跟踪每 N 个对象分配选项允许定义跟踪对象的密度。在大多数应用程序中,没有必要监视每个实例,当仅跟踪每个第 N 个对象时,总体分配行为是相同的。这种方法的优点是分析开销较低。要获取已分配或活动对象的准确数量,必须将此选项设置为 1。默认情况下,每第 10 个对象都会被跟踪。

第三个控件是记录分配堆栈跟踪复选框。选择此选项后,将在分配时为每个受监视对象进行堆栈跟踪。这允许识别已分配对象的具体方法(及其调用树)。默认情况下,禁用分配堆栈跟踪的收集以降低分析开销。

分配堆栈在 Visual VM 中跟踪

请注意,默认情况下,内存结果的某些列是隐藏的。您可以使用分析结果或内存快照表中的右上角按钮显示更多指标。

与 NetBeans Profiler 的比较

本节比较 NetBeans Profiler 和 VisualVM 中可用的分析设置。对于无法在 VisualVM 中自定义的设置,列出了预定义值。

CPU 分析设置:

设置NetBeans 分析器VisualVM
分析根包/类/方法/项目方法包/类
检测过滤器预定义集/用户定义/用户定义的
项目代码分析点启用/禁用N/A
仅分析技术检测/检测和分析 采样时间
检测排除 Thread.sleep() & Object.wait() 时间开/关始终打开
Profile 底层框架启动开/关始终关闭
分析技术仅检测/检测和检测 采样时间
检测分析新线程/可运行对象开/关开/关
分析线程限制1 至无限制始终 32
线程 CPU 定时器(仅限 Solaris)开/关始终关闭
检测方案总计/急切/惰性始终惰性
Instrument Method.invoke()开/关始终打开
仪器 getters/setter开/关始终关
仪器空方法开/关始终关

内存分析设置:

设置NetBeans 分析器VisualVM
分析范围对象分配/对象分配& GC对象分配/对象分配& 跟踪对象的GC
密度跟踪每个第 N 个对象跟踪每个第 N 个对象
分配堆栈跟踪开/关开/关
分析点启用/禁用N/A
限制堆栈跟踪深度1 帧至无限深度始终无限深度
获取时运行 GC结果开/关始终打开

请注意,通过选择 CPU 或内存快照的“信息”选项卡,您可以轻松确定 NetBeans 分析器和 VisualVM 中使用的分析设置。

进一步阅读

有关详细信息,请参阅 VisualVM文档,尤其是分析应用程序部分

您还可以找到 NetBeans Profiler 5.5 文档 很有用,尤其是检测根方法,<一个href="https://web.archive.org/web/20090608135056/http://profiler.netbeans.org/docs/help/5.5/custom_cpuprofiling.html" rel="nofollow noreferrer">CPU 分析设置内存分析设置 部分。

See Profiling With VisualVM, Part 1 and Profiling With VisualVM, Part 2 to find out how to set CPU settings.

Note: If anyone has VisualVM, it would be a great addition to replace the screenshots from the article as they were not saved in a good resolution or were of bad quality to begin with

Profiling With VisualVM, Part 1

Profiling With VisualVM, Part 1
By Jiri Sedlacek on Jul 28, 2008

This article describes the out-of-the-box profiling features of the VisualVM tool available without any additional configuration, just using the default settings.

Beside various monitoring features, the tool contains a built-in profiler based on the NetBeans profiler. While the profiler UI in VisualVM looks simple (especially when compared to the NetBeans profiler), the profiling capabilities are almost as powerful as in NetBeans. The main difference between these two tools is their workflow: whereas NetBeans profiler is project/sources-centered and is a great tool for iterative testing/improving performance during application development, VisualVM can be used as a standalone profiler which works with any running application without having any context of the source code.

Note: For profiling applications running on JDK 6.0 Update 6 and earlier, the applications need to be started with the Xshare:off flag which disables the class sharing, otherwise the profiling may cause a crash of profiled JVM. For applications running on JDK 6.0 Update 7 and higher the profiling should work without any modifications.

VisualVM Profiling Home Screen

The profiling UI is very simple, by default it shows just three buttons for analyzing performance (CPU button), memory usage (Memory button) and stopping the profiling session (Stop button). The other UI elements are Status area where the actual profiling status is shown and Profiling results section which displays live profiling data collected during the profiling session. For advanced profiling there's also a Settings section which is hidden by default. It allows to fine-tune the profiling settings, this is described in detail in [Profiling With VisualVM, Part 2 article](#Profiling With VisualVM, Part 2 article).

Note: Whereas the Profiler tab is available for each application which can be profiled, only one application can be profiled at a time, concurrent profiling sessions are not supported.

Profiling Performance

When the CPU button in Profiler tab is pressed, the profiler attaches to the application and starts profiling its performance. At the beginning the profiler needs to instrument some methods of the application, the actual number of instrumented methods is displayed in the Status area. Since the profiler uses dynamic instrumentation, the number of instrumented methods may change during the profiling session. After the profiler is attached and starts collecting the data, the view looks like on the screenshot:

Photo of VisualVM after Profiler is attached

You can see live profiling data for the application, for each profiled method number of invocations and total execution time is displayed. Note that not all methods are profiled, by default the profiler doesn't profile methods from java.*, javax.*, sun.*, sunw.*, com.sun.* (+ com.apple.*, apple.awt.* on Mac OS X) packages. Time spent in methods from these packages is added to execution time of profiled methods calling them, for example constructor execution time of a custom class (CustomClass.) includes execution time of super constructor etc.

VisualVM Profiler Toolbar

The toolbar of Profiling results section contains the following actions (from the left):

  • Update Results Automatically: results are periodically refreshed approx. each 1.2sec
  • Update Results Now: immediately fetches actual data, typically used when automatic refresh is disabled
  • Run Garbage Collection: invokes GC in profiled application
  • Reset Collected Results Buffer: resets the data collected so far, useful for measuring delta results
  • Take Snapshot of Collected Results: saves all the collected data into a persistent snapshot for more detailed analysis
  • Save current view to image: saves the results table into a .png image

The most important action is Take Snapshot of Collected Results (available also as Profiler Snapshot in context menu of profiled application in Applications tree). It takes a snapshot of collected profiling results (compatible with NetBeans profiler snapshot) and opens it in a separate tab. This snapshot provides several different views: Call Tree displaying methods call tree starting by threads, Hot Spots displaying a list of all profiled methods sorted by their execution time and Combined view showing both call tree and list of profiled methods. The last view Info displays basic snapshot information and detailed profiling configuration of the snapshot. There's one handy action available in context menu of each method, it's called Show Back Traces and displays all the places from where the method is being called.

Profiler showing the Combined view (presumably since image is pretty pixels)Profiler showing the Hot Spots view (presumably since image is pretty pixels)

Multiple profiler snapshots can be taken for a single profiling session for example to compare performance in various situations / loads / configurations of the application. All the profiler snapshots can be then saved into a single Application Snapshot (use Application Snapshot action in context menu of profiled application in Applications tree) to archive them in one file or to send them to application developers/quality engineers.

Profiling Memory

When Memory button in Profiler tab is pressed, the profiler attaches to the application and starts profiling its memory consumption. At the beginning the profiler needs to instrument all classes of the application, the actual number of instrumented classes is displayed in the Status area. Because all the application classes need to be instrumented, it takes some time until the profiler starts to collect the data. To lower the profiling overhead, each 10th object allocation is tracked by default (also displayed in Status area). Keep this in mind when evaluating the profiling results. After the profiler is attached and starts collecting the data, the view looks like on the screenshot:

Profiler showing the memory usage profile of the demo application

You can see live profiling data for the application, for each class number of live instances and their size is displayed. The rightmost column shows Surviving Generations metrics which is very useful for detecting memory leaks. Detailed explanation of Surviving Generations is available in this article. You can also monitor allocations history of a particular class during time (History subtab at the bottom), to do this invoke the Log Class History action in context menu of the class.

The toolbar of memory profiling results is the same as for CPU profiling results. You can also save the collected results into a snapshot using the Take Snapshot of Collected Results action in Profiling results section toolbar or Profiler Snapshot context menu action. This is especially useful for detecting memory leaks, using the Compare Memory Snapshots action available in File menu you can visually display the difference between two comparable memory results and for example evaluate if/which objects remained on the heap after executing some action.

Profiler showing the Memory Profile Snapshot, presumably

Note: When stopping the profiling session (both CPU an Memory) the profiler restores original application bytecode. This can take some time and the application and/or the whole system may not respond for a while.

Profiler Calibration

When VisualVM is started for the first time, the profiler performs an initial calibration. During this process the overall system and actual JDK performance is being measured and these data are later used to subtract the profiling overhead from collected performance data. The profiler requires a "stable" performance of the system, this means that any technique for dynamic CPU frequency switching like SpeedStep or PowerNow! should be disabled during both the calibration and profiling.

If the system performance changes or a different JDK than the calibrated one is used for profiling, the collected performance results may become biased and the profiler needs to be re-calibrated. Unfortunately there's no UI action available for recalibration in VisualVM 1.0, the only way to invoke the calibration again is to manually delete calibration data file in <user_directory>/.nbprofiler/machinedata.jdk1X and restart the VisualVM.

Profiling With VisualVM, Part 2

This article describes advanced profiling features of the VisualVM tool which give the user more control over the profiling session and collected data. You may also want to read the [Profiling With VisualVM, Part 1](#Profiling With VisualVM, Part 1) article describing the basic profiling features of VisualVM.

By default, the Profiler UI in VisualVM doesn't display any settings, the initial configuration ensures that even an unexperienced user will get some useful results with zero configuration by just clicking the CPU or Memory button. However, an advanced user still has the possibility to fine-tune the settings and fully utilize the power of the NetBeans profiler engine.

To display the settings area, Settings checkbox has to be selected in the topright corner of Profiler tab. The settings area consists of two tabs, CPU settings and Memory settings, containing controls for each profiling mode.

VisualVM Settings

Note that the settings can only be changed when the profiling session is not in progress, during profiling the settings area is disabled.

There's also a Restore Defaults button available at the bottom of the area which restores the initial configuration. This is useful when the values were customized incorrectly and no profiling data are being collected.

Profiling Performance

For performance profiling there are two important settings available: profiling roots and instrumentation filter. In general, profiling roots define when the profiler should start collecting the data whereas instrumentation filter defines packages/classes from which the data should/shouldn't be collected.

VisualVM CPU Settings

Start profiling from classes textbox defines the profiling roots. If all the methods were instrumented, the profiling overhead would be enormous and profiling would eventually become unusable. That's why the NetBeans profiler introduces a notion of Root Methods - a way to profile just the code of interest while the rest of the application runs at full speed.

A typical usecase is for example profiling performance of an action invoked by a button click - if the appropriate actionPerformed() method is set as a root method, the profiler starts instrumenting and profiling the application once the actionPerformed() method is invoked and only profiles the methods called from the actionPerformed(). This way the profiling overhead is very small and only relevant profiling results are collected which greatly improves their readability.

Whereas in NetBeans profiler selecting a root method is very natural because of its tight integration with the editor, in VisualVM this might be a bit tricky and not exactly user friendly. That's why VisualVM defines profiling roots by classes - all methods defined in these classes and their innerclasses become root methods. In the similar way, package names can be used as profiling roots. That's how the default profiling roots are defined: <main_class_package>.** which means that all methods from <main_class_package> and subpackages are profiled.

The default settings work in most cases, but for large applications like application servers it might be useful to tweak the profiling roots to point to a concrete code of interest. For example, when profiling a web application it's much better to set the servlet's package or classname as a profiling root instead of profiling the whole server.

The format for entering the profiling roots in VisualVM is following:

  • org.mypackage.** defines all methods in org.mypackage and all subpackages as root methods
  • org.mypackage.* defines all methods in org.mypackage as root methods
  • org.mypackage.MyClass defines all methods in org.mypackage.MyClass and its innerclasses as root methods
  • empty textbox means that all methods executed in the application will be profiled
    Multiple profiling roots (separated by a space and/or comma and/or newline) can be defined for a single profiling session.

Note that it doesn't make sense to define org.mypackage.MyClass.* or org.mypackage.MyClass.** as profiling roots since the tool always selects all methods of the class and its innerclasses as profiling roots.

Profile only classes / Do not profile classes textbox defines the instrumentation filter. This is exactly the same option as Quick Filter in the NetBeans profiler. It either defines the only packages/classes to be profiled or packages/classes not to be profiled. For example, if only an application's code should be profiled without any outgoing calls to other frameworks or JDK code, this option should be set to Profile only classes, <application_package>.. If all the code except JDK classes from java and javax packages should be profiled, this option should be set to Do not profile classes, java., javax.. The default instrumentation filter is Do not profile classes, java., javax., sun., sunw., com.sun. (+ com.apple., apple.awt. on Mac OS X) which filters out all the JDK code.

The format for entering instrumentation filter is following:

  • org.mypackage.* selects all classes from org.mypackage and subpackages to be profiled or to be excluded
  • org.mypackage. selects all classes from org.mypackage to be profiled or to be excluded
  • empty textbox selects all classes of the application to be profiled
    Multiple filter values (separated by a space and/or comma and/or newline) can be defined for a single profiling session.

##Profiling Memory

For memory profiling there are three important settings available: profiling scope, density of tracked objects and recording allocations stack traces.

VisualVM Memory Settings

The profiling scope is defined by one of two choices: for the Profile object allocations option only object allocations are monitored. This is useful when evaluating how many objects are allocated by the application. For checking and identifying memory leaks the second choice is more useful - Profile object allocations and GC. In this mode the allocated objects are monitored during the whole lifecycle, from their allocation till garbage collection. This allows to easily identify live objects currently allocated on the heap at certain point. Also, in this mode the Surviving Generations metrics is available (see this article for more information). By default, Profile object allocations and GC option is selected.

The Track every Nth object allocations option allows to define density of tracked objects. In most applications it isn't necessary to monitor each single instance, the overall allocations behavior is the same when tracking just each Nth object. The advantage of this approach is lower profiling overhead. To get exact number of allocated or live objects, this option has to be set to 1. By default, each 10th object is tracked.

The third control is the Record allocations stack traces checkbox. When selected, a stack trace is taken for each monitored object when allocated. This allows to identify the concrete method (and its call tree) which has allocated the object. By default the collecting of allocation stack traces is disabled to lower the profiling overhead.

Allocation Stack Trace in Visual VM

Note that by default some columns of memory results are hidden. You can display more metrics using the top-right corner button in Profiling results or memory snapshot tables.

Comparison With The NetBeans Profiler

This section compares profiling settings available in the NetBeans profiler and VisualVM. For the settings which cannot be customized in VisualVM the predefined value is listed.

CPU profiling settings:

SettingsNetBeans profilerVisualVM
Profiling rootspackages/classes/methods/project methodspackages/classes
Instrumentation filterpredefined sets/user defined/project codeuser defined
Profiling pointsenabled/disabledN/A
Profiling techniqueinstrumentation only/instrumentation & sampled timeinstrumentation only
Exclude Thread.sleep() & Object.wait() timeon/offalways on
Profile underlying framework startupon/offalways off
Profiling techniqueinstrumentation only/instrumentation & sampled timeinstrumentation only
Profile new Threads/Runnableson/offon/off
Profiled threads limit1 to unlimitedalways 32
Thread CPU timer (Solaris only)on/offalways off
Instrumentation schemetotal/eager/lazyalways lazy
Instrument Method.invoke()on/offalways on
Instrument getters/setteron/offalways off
Instrument empty methodson/offalways off

Memory profiling settings:

SettingsNetBeans profilerVisualVM
Profiling scopeobject allocations / object allocations & GCobject allocations / object allocations & GC
Density of tracked objectstrack every to every Nth objecttrack every to every Nth object
Allocations stack traceson/offon/off
Profiling pointsenabled/disabledN/A
Limit stack traces depth1 frame to unlimited depthalways unlimited depth
Run GC when getting resultson/offalways on

Note that you can easily determine the used profiling settings in both NetBeans profiler and VisualVM by selecting the Info tab of the CPU or memory snapshot.

Further Reading

For more information see the VisualVM Documentation, especially the Profiling Applications section.

You may also find the NetBeans Profiler 5.5 documentation useful, especially the Instrumenting a Root Method, CPU Profiling settings or Memory Profiling Settings sections.

短叹 2024-11-26 01:24:33

我注意到 Visual VM 报告了错误的方法调用样本计数(我相信这些意味着调用方法的次数)。

I noticed Visual VM reporting incorrect method call sample counts (I believe those mean the number of times a method was called).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文