In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article is to share with you about how to use VisualVM for performance analysis and tuning, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
Using VisualVM for performance Analysis and tuning
VisualVM is a free visualization tool that integrates multiple JDK command line tools, which can provide you with powerful analysis capabilities, performance analysis and tuning of Java applications. These functions include generating and analyzing large amounts of data, tracking memory leaks, monitoring the garbage collector, performing memory and CPU analysis, and supporting browsing and manipulation on the MBeans. This paper mainly introduces how to use VisualVM for performance analysis and tuning.
Develop and deploy your next application on the IBM Bluemix cloud platform.
Start your trial
Overview
It is hard to avoid memory leaks and performance bottlenecks in the process of developing large-scale Java applications, such as unreleased connections to files, networks and databases, unoptimized algorithms and so on. With the continuous operation of the application, the running efficiency of the whole system may be reduced, and in serious cases, the system will crash. In order to find out these hidden problems in the program, performance analysis tools are often used to analyze and optimize the performance of the application in the later stage of project development.
VisualVM is a free performance analysis tool. It obtains real-time data from the running time of the program through jvmstat, JMX, SA (Serviceability Agent), Attach API and other ways, so as to carry out dynamic performance analysis. At the same time, it can automatically select 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 one by one and discusses how to use the obtained data for performance analysis and tuning.
Go back to the top of the page
The main ways of performance Analysis of background knowledge
Monitoring: monitoring is a general way to view the behavior of an application at run time. There are usually multiple views (View) that display CPU usage, memory usage, thread status, and other useful information in real time, so that users can quickly identify the crux of the problem.
Dump: the performance analysis tool gets the current state data from memory and stores it in a file for static performance analysis. The Java program triggers the dump operation by adding appropriate conditional parameters when starting the Java program. It includes the following three categories:
System dump: a dump of a local system generated by JVM, also known as a core dump. In general, the system dumps a large amount of data and needs platform-related tools to analyze, such as windbg on Windows and gdb on Linux.
Java dump: formatted data generated within JVM, including thread information, class loading information, and heap statistics. It is also commonly used to detect deadlocks.
Heap dump: JVM stores the heap contents of all objects to a file.
Snapshot: after the application starts, the performance analysis tool begins to collect all kinds of run-time data, some of which is displayed directly in the monitor view, while most of the other data is kept internally until the user requests a snapshot. Statistics based on these saved data are displayed. Snapshots contain information about the execution of an application over a period of time, and there are usually two types: CPU snapshots and memory snapshots.
CPU snapshot: mainly contains the call relationship and run time of the function in the application, which can usually be viewed in the CPU snapshot view.
Memory snapshot: mainly includes the allocation and usage of memory, all loaded classes, existing object information and reference relationships between objects. This information can usually be viewed in the memory snapshot view.
Performance analysis: performance analysis is to help developers locate the parts of the program that need to be optimized by collecting execution data when the program is running, so as to improve the running speed of the program or the efficiency of memory use. There are three main aspects:
CPU performance analysis: the main purpose of CPU performance analysis is to count the function calls and execution time, or, more simply, the CPU usage of the application. There are usually two ways to display the results of CPU performance analysis: CPU monitoring and CPU snapshot.
Memory performance analysis: the main purpose of memory performance analysis is to detect possible memory leaks and determine the direction of optimizing memory usage by counting memory usage. There are usually two ways to display the results of memory performance analysis: memory monitoring and memory snapshot.
Thread performance analysis: thread performance analysis is mainly used to identify memory problems in multithreaded applications. It generally includes the change of the state of the thread, the deadlock and the distribution of the state of a thread during the lifetime of the thread.
Go back to the top of the page
VisualVM installation
VisualVM is a performance analysis tool that has been part of Oracle JDK since JDK 6 Update 7 and is located in the bin folder of the JDK root directory. VisualVM itself runs on versions above JDK6, but it can monitor applications above JDK1.4. The following is mainly about how to install VisualVM and plug-ins on various VisualVM.
Install VisualVM
The official website of the VisualVM project is currently available for download in English and multilingual support. The multilingual version mainly supports English, Japanese and Chinese. If you download and install a multilingual version of VisualVM, the installer will install the language version of the corresponding VisualVM based on the current locale of the operating system. The main operating systems supported by the latest VisualVM version include: Microsoft Windows (7, Vista, XP, Server), Linux, Sun Solaris, Mac OS X, HP-UX 11i. This article uses Microsoft Windows XP as the installation environment and supports Chinese.
Download the VisualVM installer from the official website of the VisualVM project.
Extract the VisualVM installer to the local system.
Navigate to the bin directory of the VisualVM installation directory and start jvisualvm.exe.
Install plug-ins on VisualVM
The VisualVM plug-in Center provides a number of plug-ins for installation to add functionality to VisualVM. You can install the plug-in through the VisualVM application, or download the plug-in manually from the VisualVM plug-in Center and install it offline. In addition, users can install third-party plug-ins to add functionality to VisualVM by downloading the plug-in distribution file (. Nbm file).
Install the plug-in installation steps from the VisualVM plug-in Center:
Select tools > plug-ins from the main menu.
In the available plug-ins tab, select the install check box for the plug-in. Click install.
Step through the plug-in installer.
Figure 1. VisualVM plug-in Manager
Install third-party plug-ins according to the .nbm file installation steps:
Select tools > plug-ins from the main menu.
In the downloaded tab, click the add plug-in 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 complete the plug-in installer step by step.
Figure 2. Install the VisualVM plug-in from a .nbm file
Go back to the top of the page
Function introduction
Below we will introduce several common ways of performance analysis and how to use the VisualVM performance analysis tool for analysis.
Memory analysis
VisualVM helps us analyze memory usage by detecting the information of classes and objects loaded in JVM. We can analyze the memory of applications through the monitoring tags and Profiler tags of VisualVM.
Within the monitoring tab, we can see the use of the real-time application memory heap and the permanent reserved area.
Figure 3. Memory heap usage
Figure 4. Permanent reserved area usage
In addition, we can also enable the "generate heap Dump when OOME occurs" feature by right-clicking the application node through the Applications window, and VisualVM will automatically generate a heap dump when the application has an OutOfMemory exception.
Figure 5. Turn on the "generate heap when OOME occurs" function
In the Profiler tab, clicking the "memory" button will start a memory analysis session, and when VisualVM has collected and counted the relevant performance data, it will be displayed in the performance analysis results. Through the results of memory performance analysis, we can see which objects occupy more memory, live longer, and so on, in order to do further optimization.
In addition, we can filter the analysis results through the class name filter under the performance analysis results.
Figure 6. Memory analysis results
CPU analysis
VisualVM can monitor the application's CPU usage over a period of time, and show related data such as CPU utilization, method execution efficiency and frequency to help us find application performance bottlenecks. We can analyze the CPU performance of the application through the VisualVM monitoring tag and the Profiler tag.
Within the monitoring tab, we can see the utilization of CPU and the impact of garbage collection activities on performance. The excessive CPU utilization may be due to inefficient code in our project, which can be analyzed in detail through the CPU performance analysis function of the Profiler tag. If garbage collection activities are too frequent and occupy high CPU resources, it may be caused by insufficient memory or unreasonable allocation between the new generation and the old generation.
Figure 7. CPU usage
In the Profiler tab, click the "CPU" button to start a CPU performance analysis session, and VisualVM will detect all the called methods of the application. When entering a method, the thread emits a "method entry" event, and when exiting the method, it also emits a "method exit" event, all of which contain a timestamp. VisualVM then shows the total execution time of each called method and the number of calls according to the run time.
In addition, we can also filter the analysis results through the method name filter under the performance analysis results.
Figure 8. CPU performance analysis results
Thread analysis
Java language can well implement multithreaded applications. When we debug a multi-threaded application or do performance tuning in the later stage of development, we often need to know the running status of all threads in the current program and whether there are deadlocks, hot locks and so on. in order to analyze the problems that may exist in the system.
Within the monitoring tab of VisualVM, we can view real-time information such as the number of active and daemon threads in the current application.
Figure 9. Active thread situation
The thread tab of VisualVM provides three views, which are displayed as timelines by default. The other two views are the table view and the details view.
The toolbar above the timeline view provides zoom out, zoom in, and adaptive buttons, as well as a drop-down box, where we can choose to display all threads, active threads, or completed threads in the view.
Figure 10. Thread timeline view
Figure 11. Thread table view
In the details view, we can view not only the details of all threads, active threads, and ending threads, but also the details of a particular thread.
Figure 12. Thread detail view
Go back to the top of the page
Snapshot function
We can use VisualVM's snapshot feature to generate any performance analysis snapshot and save it locally to assist us in performance analysis. Snapshots provide a convenient way to capture application performance analysis data because once snapshots are generated, they can be opened and viewed offline at any time, and can also be circulated to each other.
VisualVM provides two types of snapshots:
Profiler snapshot: when there is a performance analysis session (memory or CPU) in progress, we can use the "snapshot" button of the performance analysis results toolbar to generate a Profiler snapshot to capture the performance analysis data at that time.
Figure 13. Profiler snapshot
Application Snapshot: we can right-click the application node in the left Applications window and select "Application Snapshot" to generate an application snapshot. Application snapshots collect heap dumps, thread dumps, and Profiler snapshots at some point, as well as capture some basic information about JVM.
Figure 14. Application snapshot
Go back to the top of the page
Generation and Analysis of dump function Thread dump
VisualVM can generate thread dumps for running local applications and print out the stack trace of active threads, which helps us to effectively understand the running situation of threads and diagnose deadlocks, application paralysis and other problems.
Figure 15. Thread label and thread dump function
When VisualVM has counted the data related to the threads in the application, the information will be displayed with a new thread dump label.
Figure 16. Thread dump result
Generation and analysis of heap dump
VisualVM can generate heap dump, count the object information in JVM at a particular time, and help us to analyze the reference relationship of objects, whether there are memory leaks and so on.
Figure 17. Monitoring tags and heap dump functions
When VisualVM has finished counting the object data in the heap, the heap dump information will be displayed in the new heap dump tag, and we can see the summary, class, number of instances and other information as well as execute the query statement function through the OQL console.
The summary of the heap dump includes basic information such as the file size and path of the dump, the running system environment information, and can also display all thread information.
Figure 18. Summary view of heap dump
The number of instances and heap size of each class can be obtained from the class view, the usage of memory space can be analyzed, the bottleneck of memory can be found, and the excessive use of memory can be avoided.
Figure 19. Class view of heap dump
Through the instance count view, you can get the value of each member variable within each instance and the location where the instance is referenced. First, you need to select the class for which you want to view the instance in the class view.
Figure 20. Select the class that queries the number of instances
Figure 21. Number of instances view
In addition, you can compare the two heap dump files. By comparison, we can analyze which objects are created or destroyed in large numbers at two time points.
Figure 22. Comparison of heap dumps
Figure 23. Comparison results of heap dumps
Both thread dump and heap dump can be saved as a file for offline analysis.
Figure 24. Export of dump file
First of all, some background knowledge related to performance analysis is briefly listed. Then it introduces the download and installation of VisualVM. Finally, it further explains how to use VisualVM for performance analysis from four aspects: memory performance, CPU performance, snapshot function and dump function.
The above is how to use VisualVM for performance analysis and tuning. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.