Intellij Idea 简明教程
Intellij Idea - Profiling
Profiler 会提供有关您的应用程序的信息,例如它的 CPU、内存和堆栈使用情况。它还会提供有关应用程序线程的详细信息。本章讨论 VisualVM 工具用于 Java 应用程序分析的用法。它可以分析 CPU 和堆栈等实体。
Profiler gives insights about your application like its CPU, memory and heap usage. It also gives details about the application threads. This chapter discusses the usage of VisualVM tool for Java application profiling. It can profile entities such as CPU and heap.
建议本教程的读者熟悉应用程序分析器概念。
It is recommended that the readers of this tutorial are familiar with the application profiler concepts.
Introduction
VisualVM 是一款可视化工具,它集成了 JDK 工具并为您提供了强大的分析功能。它允许您生成和分析堆数据,追踪内存泄漏,监控垃圾回收,并执行内存和 CPU 分析。
VisualVM is a visual tool that integrates JDK tools and gives you powerful profiling capabilities. It allows you to generate and analyze heap data, track down memory leaks,monitor the garbage collector and perform memory and CPU profiling.
Benefits
-
Visual interface for local and remote Java applications running on JVM.
-
Monitoring of application’s memory usage and application’s runtime behavior.
-
Monitoring of application threads
-
Analyzing the memory allocations to different applications.
-
Thread dumps − very handy in case of deadlocks and race conditions.
-
Heap dumps − very handy in analyzing the heap memory allocation.
Configuration
在本部分中,我们将学习配置 VisualVM 所执行的步骤。步骤如下:
In this section, we will learn the steps performed to configure VisualVM. The steps are as follow −
-
Download it from here.
-
Extract the zip file.
-
Navigate to etc/visualvm.conf file and add the following line in this file −
visualvm_jdkhome=<path of JDK>
-
If your JDK is installed in the C:\Program Files\Java\jdk-9.0.1 directory then it should look like this −
visualvm_jdkhome="C:\Program Files\Java\jdk-9.0.1"
Monitoring application
现在让我们看看如何监视应用程序。根据以下步骤来理解相同的操作:
Let us now see how to monitor the application. Consider the following steps to understand the same −
-
Double-click on the visualvm.exe file.
-
Select the application from left pane.
-
Select the monitor tab.
您将被定向到一个窗口,您将在其中获取有关 CPU、堆、类和线程的详细信息。具体来说,将鼠标悬停在任何图表上方即可。我们在上面的屏幕截图中可以看到堆的使用率。
You will be directed to a window where you will get the details about CPU, Heap, Classes and threads. To be specific with the usage, hover the mouse over any graph. We can see the usage of Heap in the above screenshot.
Insights about Threads
Java 应用程序可以包含多个执行线程。要了解有关线程的更多信息,请选择特定应用程序的“线程”选项卡。它将提供有关线程的各种统计信息,例如活动线程数和守护线程数。不同的线程状态有:正在运行,正在休眠,正在等待,正在停放和正在监视。
Java application can contain multiple threads of execution. To know more about threads, select the Threads tab of a particular application. It will give various statistics about threads like number of live threads and daemon threads. The different thread states are Running, Sleeping, Waiting, Park and Monitor.
Sampling Application
VisualVM 支持 CPU、内存采样和内存泄漏检测。要对应用程序进行采样,请选择应用程序并选择“采样”选项卡:
VisualVM supports CPU, memory sampling and memory leak detection. To sample application, select application and choose the sample tab −
CPU sampling
对于 CPU 采样,点击 CPU 按钮,如下面屏幕截图所示:
For CPU sampling, click on the CPU button as show in the following screenshot −
Memory profiling
对于内存分析,点击内存按钮,如下面屏幕截图所示:
For memory profiling, click on the Memory button as shown in the following screenshot −
Memory leaks
当应用程序在运行时,缓慢地用程序不会自动删除的对象填充堆时,就会发生内存泄漏。
A memory leak occurs when an application, while running, slowly fills up the heap with objects that are not automatically deleted by the program.
如果程序不使用的对象未被删除,那么它将保留在内存中,GC 无法回收其空间。如果应用程序中的字节数和实例数持续大幅增加,以至于用尽所有空间,则这可能是内存泄漏的征兆。
If an object that is not used by the program is not deleted, then it remains in memory and the GC cannot reclaim its space. If the number of bytes and number of instances in your application were to increase constantly and significantly in your program to the point of using up all the space, this can be an indication of a memory leak.
Profiling application
在本部分中,我们将学习如何对应用程序进行分析。要分析应用程序,请从左侧窗格中选择应用程序,然后单击“分析”选项卡:
In this section, we will learn how to profile an application. To profile an application, select application from left pane and click the profile tab −