Use Eclipse Memory Analyzer for heap Dump File Analysis

Source: Internet
Author: User
Overview

For large JAVA applications, it is difficult to block all the vulnerabilities even if we perform a lot of effective work in the test phase, many problems will still be exposed in the production environment, and it is difficult to reproduce in the testing environment. The JVM can record part of the running status of the system when a problem occurs and store it in the Heap Dump file, which provides an important basis for us to analyze and diagnose the problem.

Generally, Memory Leak analysis is considered a very difficult task, which is generally conducted by senior personnel in the team. However, the MAT (Eclipse Memory Analyzer) we are going to introduce today is considered to be a "silly" heap dump file analysis tool, you only need to click to generate a professional analysis report. Compared with other memory leak analysis tools, MAT is easy to use and can be easily used with one click. Even new users can use it quickly.

MAT is so easy to use. Are you also very interested to feel it yourself? The first step is to install MAT first.

Prepare environment and test data

We use Eclipse Memory Analyzer V0.8 and Sun JDK 6.

Install MAT

Similar to the installation of other plug-ins, the MAT supports two installation methods, one is the "single-host version", that is, you do not have to install the Eclipse IDE environment, MAT runs as an independent Eclipse RCP application. The other is the integrated version. That is to say, MAT can also be used as part of Eclipse IDE and integrated with the existing development platform.

Update Manager is required for integrated version installation.

1. Start the Software update management wizard through Help-> Software Updates.

Figure 1. Step 1 of plug-in Installation

Select "Available Software" and add the MAT update address http://download.eclipse.org/policy/mat/0.8/update-site/as shown in figure 2 /.

Figure 2. Install the plug-in step 2

3. Next, select the MAT function you want to install. Note that Memory Analyzer (Chart) is an optional installation project, which is mainly used to generate related reports, however, if you need this function, you also need to install the BIRT Chart Engine.

Figure 3. Step 3 of plug-in Installation

After installing the plug-in, you need to restart the Eclipse working platform.

In comparison, the installation method of the standalone version is very simple. You only need to download the corresponding installation package and decompress it to run it. This is also a widely used installation method. In the following example, we use the single-host MAT. For specific download requirements and addresses, see the product download page: http://www.eclipse.org/mat/downloads.php.

In addition, if you need to use MAT to analyze the dump file generated by ibm jvm, you also need to install the IBM Diagnostic Tool Framework. For detailed download and installation configuration steps, see http://www.ibm.com/developerworks/java/jdk/tools/dtfj.html

Configure Environment Parameters

After the installation is complete, we still need to do some configuration work to use MAT more efficiently. Generally, analyzing a heap dump file consumes a lot of heap space. To ensure analysis efficiency and performance, we recommend that you allocate as many memory resources as possible to MAT. You can use the following two methods to allocate more memory resources to MAT.

One is to modify the startup parameter MemoryAnalyzer.exe-vmargs-Xmx4g

The other is to edit the MemoryAnalyzer. ini file and add similar information-vmargs-Xmx4g in it.

Now, the MAT has been successfully installed and configured. Let's get started.

Obtain heap dump files

It's hard for us to get a heap dump file. For convenience, this article uses Sun JDK 6. Generally, as long as you set the following JVM parameters:

-XX: + HeapDumpOnOutOfMemoryError

JVM captures the memory status when Memory leakage occurs, that is, the desired heap dump file.

If you do not want to obtain the heap dump file when a collapse error occurs, you can also set the following JVM parameters to obtain the heap dump file as needed.

-XX: + HeapDumpOnCtrlBreak

In addition, many tools, such as JMap and JConsole, can help us get a heap dump file. This example uses JMap to directly obtain the heap dump file of the Eclipse Galileo process. You can run the following command:

JMap-dump: format = B, file = <dumpfile> <pid>

However, you need to know that the heap dump files generated by JVM from different manufacturers have many differences in data storage formats and data storage content. MAT is not a 10 thousand-level tool, it cannot process all types of Heap Storage files. However, the HPROF binary Heap Storage files used by mainstream manufacturers and formats, such as Sun, HP, and SAP, And the ibm phd Heap Storage files can be well parsed.

Everything is ready. Next, we can start to experience the one-click Heap Storage analysis function.

Generate Analysis Report

First, start the configured Memory Analyzer tool and select the menu item File-Open Heap Dump to load the Heap Dump File to be analyzed. After the file is loaded, you can see the interface shown in 4:

Figure 4. Overview

Through the above overview, we have a general understanding of memory usage. First, check a series of files generated by MAT.

Figure 5. File List

We can see that the MAT tool provides a very considerate function. It compresses and packs the report content into a zip file and stores it in the directory where the original heap dump file is stored, in this way, if you need to analyze the memory problem with your colleagues, you only need to send the small zip package to him, instead of sending the entire heap file to him. The entire report is an HTML file, which can be opened easily in a browser.

Next, let's take a look at what the generated report contains. Can you help us find the problem. You can click the Leak Suspects menu item on the toolbar to generate a memory Leak analysis report, or click the Reports-> Leak Suspects link below the pie chart to generate a report.

Figure 6. toolbar menu

Analysis of Three Steps

We usually use the following "Three Steps" To Analyze memory leaks:

First, get an overall impression of the system memory status when the problem occurs.

Step 2: Find the culprit most likely to cause memory leakage, which is usually the most memory-consuming object.

Next, let's further look at the details of this large memory user and see if there are any abnormal behaviors.

The following uses a basic example to show how to use "Three Steps" to view the production analysis report.

View one of the reports: overall memory consumption

Figure 7. Memory leakage Analysis Report

As shown in figure 7, the most eye-catching report is a concise pie chart, from which we can clearly see that a suspicious object consumes 99% of the system's memory.

Further descriptions of this suspicious object are shown below the figure. We can see that the memory is consumed by java. util. Vector instances. com. ibm. oti. vm. BootstrapClassLoader is responsible for loading this object. The description is very short, but I believe you can find many clues, such as which class occupies the vast majority of memory and which component it belongs.

Next, we should further analyze the problem, why does a Vector occupy 99% of the system's memory and who has prevented the garbage collection mechanism from recycling it.

View Report 2: locate the problem

First, let's briefly review the JAVA memory collection mechanism. The Garbage collection in the memory space is completed by the Garbage Collector (Garbage Collector, GC). Its core idea is: identifies the available memory space of the VM, that is, the objects in the heap space. If the object is being referenced, it is called a surviving object. If the object is no longer referenced, It is a junk object, the space occupied by it can be recycled for redistribution.

In the garbage collection mechanism, a group of elements are called root element sets. They are a group of objects directly referenced by virtual machines, such as running thread objects, objects in the system Call Stack and those loaded by system class loader. Each object in the heap space is called layer by layer from a root element as the starting point. Therefore, if an object is referenced by a surviving root element, it is considered a surviving object and cannot be recycled for memory release. Therefore, we can analyze the reference path from an object to the root element to analyze why the object cannot be recycled smoothly. If an object is not required by any program logic but still referenced by the root element, we can say that memory leakage exists here.

Now, let's start looking for memory leaks. Click the "Details" link to see the detailed analysis report on suspicious object 1 shown in 8.

Figure 8. Detailed analysis report of suspicious object 1

Let's look at the shortest path from the GC root element to the memory consumption aggregation point:

Figure 9. shortest path from the root element to the memory consumption aggregation point

We can clearly see the entire reference chain. The memory aggregation point is a collection with a large number of objects. If you are familiar with the code, I believe this information should provide you with some ideas about memory leakage.

Next, let's continue to see what is stored in this object set and why it consumes so much memory.

Figure 10. memory consumption aggregation Object Information

In this figure, we can clearly see that this object set stores a large number of Person object references, that is, it causes memory leakage.

At this point, we have enough information to find the leak point and return to the code. We found that the following code causes memory leakage:

Listing 1. Memory leakage code segment

while(1 < 2) { Person person = new Person("name", "address",i); v.add(person); person = null; } 

Summary

From the above example, we can see that it is very easy to use MAT to analyze heap dump files and find memory leaks. Especially for beginners, this is a good auxiliary analysis tool. However, MAT is definitely not just a "silly" memory analysis tool. It also provides many advanced functions. For example, MAT supports querying objects in heap dump using OQL (Object Query Language, supports thread analysis. For more information about how to use these functions, see the help documentation of MAT.

Transferred from

Http://tech.ddvip.com/2010-07/1279867858157995.html

Related Article

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.