Performance analysis and tuning with VisualVM

Source: Internet
Author: User
Tags stack trace visualvm

In the process of developing large-scale Java applications, memory leaks and performance bottlenecks are unavoidable, such as file, network, database connection is not released, the algorithm is not optimized and so on.

As the application continues to run, it can cause the overall system to run less efficiently and, in severe case, cause the system to crash. In order to identify these problems hidden in the program, performance analysis tools are often used to analyze and optimize the performance of the application later in the project development. VisualVM is a free performance analysis tool. It uses Jvmstat, JMX, SA (serviceability Agent), and Attach APIs to get real-time data from the program as it runs, enabling dynamic performance analysis. At the same time, it automatically chooses faster and lighter technologies to minimize the impact of performance analysis on applications and improve the accuracy of performance analysis. This paper introduces the main functions of VisualVM and discusses how to use the obtained data for performance analysis and tuning.

Background knowledge

The main way of performance analysis

Monitoring: Monitoring is a general way to view the runtime behavior of an application. There are often multiple views (view) that show CPU usage, memory usage, thread status, and other useful information in real time, respectively, so that users can quickly discover the key to the problem.

Dump: The profiling tool obtains current state data from memory and stores it in a file for static performance analysis. A Java program triggers a dump operation by adding the appropriate condition parameters when the Java program is started. It consists of the following three types:

System dump: The dump of the local system generated by the JVM, also known as the core dump. In general, the amount of system dump data is large and requires platform-related tools to analyze, such as WinDbg on Windows and gdb on Linux.

Java dump: Formatted data generated internally by the JVM, including thread information, class loading information, and heap statistics. It is also often used to detect deadlocks.

Heap Dump: The JVM stores the heap contents of all objects to a file.

Snapshot: After the application starts, the profiling Tools begin to collect various runtime data, some of which are displayed directly in the monitoring view, while most of the data is stored internally until the user asks for a snapshot, and statistics based on the saved data are displayed. A snapshot contains information about the execution of an application over a period of time, typically with both CPU and memory snapshots.

CPU snapshots: Mainly includes the call relationships and run times of functions in the application, which can often be viewed in the CPU snapshot view.

Memory Snapshot: Mainly includes the allocation and use of memory, all loaded classes, the existence of object information and the reference relationship between objects and so on. This information can often be viewed in a memory snapshot view.

Performance Analysis: Performance analysis is to help developers locate programs that need to be optimized by collecting execution data from the program runtime to improve the speed of the program or the efficiency of memory usage, mainly in the following three areas:

CPU Performance Analysis: The main purpose of CPU performance analysis is to count the function's invocation and execution time, or, more simply, to count the application's CPU usage. There are usually two ways of CPU monitoring and CPU snapshots to display CPU performance analysis results.

Memory Performance Analysis: The primary purpose of memory performance analysis is to detect possible memory leaks through statistical memory usage and to determine the direction to optimize memory usage. There are usually two ways to display memory profiling results, memory monitoring and memory snapshots.

Threading Profiling: Threading profiling is primarily used to identify memory problems in multithreaded applications. This generally includes the state change of the thread, the deadlock condition, and the distribution of the state during the lifetime of a thread.

VisualVM Installation

VisualVM is a profiling tool that has been part of the Oracle JDK since JDK 6 Update 7 and is located under the Bin folder of the JDK root directory. The VisualVM itself will run on more than JDK6 versions, but it will be able to monitor applications that are more than JDK1.4. Here's how to install VisualVM and plug-ins on various VisualVM.

Installing VisualVM

The official website of the VisualVM project is currently available in English and multi-lingual support versions. The multilingual version is available in three languages: English, Japanese and Chinese. If you download and install the multilingual version of VisualVM, the installer will install the appropriate VisualVM language version based on the operating system's current locale. The most recent VisualVM editions are supported by the following operating systems: Microsoft Windows (7, Vista, XP, Server), Linux, Sun Solaris, Mac OS X, HP-UX 11i. This article takes Microsoft Windows XP as an installation environment and supports Chinese.

    • Download the VisualVM installer from the official website of the VisualVM program.
    • Unzip the VisualVM installer to the local system.
    • Navigate to the Bin directory of the VisualVM installation directory and start Jvisualvm.exe.
Installing plugins on the VisualVM

The VisualVM Plugin Center provides many plugins for installation to add functionality to VisualVM. You can install through the VisualVM application or manually download the plugin from the VisualVM plug-in center and then install it offline. In addition, users can install third-party plug-ins to add features to VisualVM by downloading plug-in distribution files (. nbm files).

Install the plug-in installation steps from the VisualVM plug-in center:

    • From the main menu, choose Tools > Plugins.
    • In the Available Plugins tab, select the Install check box for the plug-in. Click Install.
    • Step through the plugin installer.
Figure 1. VisualVM Plugin Manager

Install the third-party plug-in installation steps according to the. nbm file:

    • From the main menu, choose Tools > Plugins.
    • In the Downloaded tab, click the Add Plugin button, select the downloaded plug-in distribution file (. nbm) and open it.
    • Select the open plug-in distribution file and click the Install button to step through the plug-in setup process.
Figure 2. Installing the VisualVM plugin through an. nbm file

Back to top of page

function Introduction

Below we will cover several common ways of profiling and how to use the VisualVM profiling tool for analysis.

Memory analysis

VisualVM helps us analyze memory usage by detecting class and object information loaded in the JVM, and we can perform memory analysis of the application through VisualVM's monitoring tags and Profiler tags.

Within the Monitoring tab, we can see the real-time application memory heap as well as the use of a permanent reserved zone.

Figure 3. Memory heap usage Figure 4. Permanent Retention of zone usage

In addition, we can right-click the application node through the Applications window to enable the "heap dump when oome occurs" feature, and VisualVM will automatically generate a heap dump when an application OutOfMemory exception occurs.

Figure 5. Turn on the "build heap when Oome appears" feature

On the Profiler tab, clicking on the "Memory" button launches a memory analysis session, and so on VisualVM collects and statistics the relevant performance data information,

will be displayed in the performance analysis results. With the results of memory profiling, we can see which objects occupy more memory, live longer, etc.

For further optimization.

In addition, we can filter the analysis results by the class name filter below the performance analysis results.

Figure 6. Memory Analysis Results CPU analysis

VisualVM is able to monitor the application's CPU usage over time, showing CPU usage, method execution

Data such as efficiency and frequency help us discover the performance bottlenecks of the application. We can pass VisualVM's surveillance tag.

and Profiler tags for CPU performance analysis of the application.

Within the Monitoring tab, we can look at CPU usage and the performance impact of garbage collection activities. Too high a CPU

Usage rates may be due to inefficient code in our project, which can be analyzed by the Profiler tag's CPU performance

function for detailed analysis. If the garbage collection activity is too frequent and consumes high CPU resources, it may be

Insufficient memory or the generation of new and old generation and the distribution of unreasonable causes.

Figure 7. CPU usage

On the Profiler tab, click on the "CPU" button to start a CPU profiling session, VisualVM will detect all applications

The method that is called. When entering a method, the thread issues a "method entry" event when exiting the method

A "Method Exit" event is also issued, and these events contain timestamps. Then VisualVM will put each

The total execution time and number of invocations of the called method are displayed according to the run-time length.

In addition, we can filter the analysis results by the method name filter below the performance analysis results.

Figure 8. CPU Performance Analysis Results Threading analysis

The Java language enables multithreaded applications to be implemented very well. When we debug or develop a multithreaded application

When performing performance tuning later, it is often necessary to understand the running state of all the threads in the current program, whether there are deadlocks,

Jes and so on, thus analyzing the possible problems of the system.

Within the VisualVM monitoring tab, we can view real-time information such as the number of active and daemon threads in the current application.

Figure 9. Active thread Condition

VisualVM's thread label provides three views, which are displayed by default in the timeline. The other two views are the table view, respectively.

Diagram and Details view.

The toolbar above the Timeline view provides zoom out, zoom in and fit three buttons, and a drop-down box where we can select

Displays all threads, active threads, or completed threads in the view.

Figure 10. Thread timeline view diagram 11. Thread Table View

We can view detailed data for all threads, active threads, and end threads in the details view, as well as

You can view the details of a thread.

Figure 12. Thread Detail View

Snapshot features

We can use the snapshot feature of VisualVM to generate any personality profiling snapshot and save it locally to assist us

Performance analysis. Snapshots provide a convenient way to capture application profiling data because once a snapshot is generated

Can be opened and viewed offline at any time, or circulated to each other.

VisualVM provides two types of snapshots:

    • Profiler Snapshot: When there is a profiling session (memory or CPU) in progress, we can pass performance
    • The snapshot button of the Analysis Results toolbar generates Profiler snapshots to capture profiling data at that time.
Figure 13. Profiler Snapshot
    • Application Snapshot: We can right click on the application node in the Left Applications window and select "Application
    • Snapshot to generate an application snapshot. Application snapshots Collect heap dumps for a moment, thread dumps, and
    • Profiler snapshots, and also captures some basic information about the JVM.
Figure 14. Application Snapshot

Dump function generation and analysis of thread dumps

VisualVM can generate a thread dump on a running local application, print the stack trace of the active thread,

Help us to understand the situation of thread running, diagnose deadlock, application paralysis and so on.

Figure 15. Thread label and thread dump function

When VisualVM counts the data about the application's internal thread, it displays the new threads dump label.

Figure 16. Generation and analysis of heap dump of thread dump result

VisualVM is able to generate heap dumps to count the object information in the JVM at a particular moment to help us

Analyze the referential relationships of objects, the occurrence of memory leaks, and so on.

Figure 17. Monitoring label and heap dump functions

After the VisualVM has counted the object data in the heap, the heap dump information is displayed in the new heap dump label.

We can see the summary, the class, the number of instances and other information, and execute the query statement function through the OQL console.

A summary of heap dumps includes the file size, path, and other basic information about the dump, the system environment information that is running,

You can also display all the thread information.

Figure 18. Summary view of Heap dumps

From Class View, you can get the number of instances of each class and the number of heap sizes, and analyze the usage of memory space.

Identify memory bottlenecks and avoid excessive use of memory.

Figure 19. Class View of Heap dumps

The number of instances view allows you to get the value of each member variable within each instance and where the instance is referenced.

You first need to select the class that you want to view the instance in Class View.

Figure 20. Select the class for the number of query instances in Figure 21. Number of Instances View

In addition, two heap dump files can be compared. By comparison we can analyze two points of time

Which objects are created or destroyed massively.

Figure 22. Comparison of Heap dumps figure 23. Comparison results for heap dumps

Both the thread dump and the heap dump can be saved as files for offline analysis.

Figure 24. Export of dump files

Summarize

This article first briefly enumerates some background knowledge related to performance analysis. Then we introduce the download and installation of VisualVM.

Finally, from four aspects of memory performance, CPU performance, snapshot function and dump function,

Further explains how to use VisualVM for performance analysis. Through the introduction of this article, I believe the reader's performance analysis

will have a certain understanding, and can use VisualVM for performance analysis.

Performance analysis and tuning with VisualVM

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.