MAT practical application

foreword

In the production environment, especially the JVM that eats a lot of memory, once there are problems such as memory leaks, it is very easy to cause OutofMemory. If there is no good tool for developers to locate and analyze the problem, then this will be a  nightmare . At present, JDK actually comes with some memory leak analysis tools specially designed to help developers locate memory leaks and other problems, but these tools are often not able to meet some status quo. The status quo that the author refers to here is more rapid, convenient and efficient positioning If there is a problem, it is convenient for developers to adjust quickly.

 

There are generally two reasons for OutOfMemoryError:

1. Memory leaks, the object is dead and cannot be automatically recycled by the garbage collector. Only by finding out the location and cause of the leaked code can the solution be determined;

2. Memory overflow, the objects in the memory must still survive, which means that the Java heap allocation space is insufficient, check the heap setting size (-Xmx and -Xms), and check whether the code has an object whose life cycle is too long and the holding time is too long. long case.

 

Note: The generated dump file is a .hprof file . Once tomcat is set, the size of the generated dump is the same as that of the built-in memory. In one location,

 

-server -Xms1024m -Xmx1024m -Xmn384m -XX:+UseParallelOldGC

-XX:+PrintGCApplicationStoppedTime -XX:+PrintGCDateStamps -XX:+PrintGCDetails

-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=c:/heap.bin

 

Shouda c drive has heap.bin folder

 

content

1. Use the jmap tool to generate a dump file

2. Download and install the MAT tool

3. Use the MAT tool for memory leak analysis

 

First, use the jmap tool to achieve heap dump,

what is jmap? Simply put, jmap is a tool that comes with JDK for generating memory image files. With this tool, developers can quickly generate dump files. Developers can use the command "jmap -help" to view the common commands of jmap as follows:

Usage:
    jmap [option] <pid>
        (to connect to running process)
    jmap [option] <executable <core>
        (to connect to a core file)
    jmap [option] [server_id@]<remote server IP or hostname>
        (to connect to remote debug server)

where <option> is one of:
    <none>               to print same info as Solaris pmap
    -heap                to print java heap summary
    -histo[:live]        to print histogram of java object heap; if the "live"
                         suboption is specified, only count live objects
    -permstat            to print permanent generation statistics
    -finalizerinfo       to print information on objects awaiting finalization
    -dump:<dump-options> to dump java heap in hprof binary format
                         dump-options:
                           live         dump only live objects; if not specified,
                                        all objects in the heap are dumped.
                           format=b     binary format
                           file=<file>  dump heap to <file>
                         Example: jmap -dump:live,format=b,file=heap.bin <pid>
    -F                   force. Use with -dump:<dump-options> <pid> or -histo
                         to force a heap dump or histogram when <pid> does not
                         respond. The "live" suboption is not supported
                         in this mode.
    -h | -help           to print this help message
    -J<flag>             to pass <flag> directly to the runtime system

 

It should be noted here that some commands of the jmap tool are limited to Linux and Solaris platforms, while the commands that can be used on Windows platforms are only "jmap -histo<pid>" and "jmap -dump:<dump-options><pid>" . However, generally speaking, using the command "jmap -dump:<dump-options><pid>" to generate a dump file should be one of the most commonly used commands. Since it is time-consuming to generate a dump file, you need to wait patiently, especially It takes a longer time to complete the dump file generated by the large memory image.

 

2. Download and install the MAT tool

The MAT (Memory Analyzer Tool) tool is a plug-in of eclipse, which is very convenient to use, especially when analyzing dump files with large memory, you can intuitively see the memory size and the number of class instances occupied by each object in the heap space , object reference relationship, use OQL object query, and can easily find out the relevant information of object GC Roots, of course, the most attractive thing is that it can quickly generate a memory leak report for developers, which is convenient for locating and analyzing problems.

The download address of the MAT tool is:  http://www.eclipse.org/mat/downloads.php

The download address of the MAT plugin is:  http://download.eclipse.org/mat/1.3/update-site/

 

The version of the MAT tool used by the author is the latest 1.4.0. In order to avoid some unnecessary exceptions in the actual use process, the author recommends that you use the same version of the MAT tool as the author to analyze the dump file.

 

Just make sure that the JDK is successfully installed on the machine and the relevant environment variables are configured, and then run MemoryAnalyzer.exe in the "MemoryAnalyzer-1.4.0.20140604-win32.win32.x86\mat\" directory to successfully start the MAT tool.

 

3. Use the MAT tool for memory leak analysis

After you have successfully downloaded and installed MAT, the next thing to do is to use the jmap+MAT tool to analyze the memory leak work together. First, I prepare a piece of test code, as shown below:

/**
 * -server -Xms1024m -Xmx1024m -Xmn384m -XX:+UseParallelOldGC
 * -XX:+PrintGCApplicationStoppedTime -XX:+PrintGCDateStamps -XX:+PrintGCDetails
 * -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=c:/heap.bin
 */
public class GCTest {
	public static void main(String[] args) {
		final int _1mb = 1024 * 1024;
		byte[] value1 = new byte[_1mb * 100];
		byte[] value2 = new byte[_1mb * 100];
		byte[] value3 = new byte[_1mb * 100];
		new Thread() {
			public void run() {
				byte[] value4 = new byte[_1mb * 400];
			}
		}.start();
		byte[] value5 = new byte[_1mb * 200];
	}
}

 

In the above code example, the specific VM refers to the class comment, and the options "-XX:+HeapDumpOnOutOfMemoryError" and -"XX:HeapDumpPath" mean that when OutofMemory occurs in the program, a copy will be generated in the corresponding directory. dump file, and if the option "XX:HeapDumpPath" is not specified, a dump file will be generated in the current directory. It should be noted here that although the MAT tool can directly generate dump files without the help of the jmap tool, considering that it is almost impossible to analyze it online in the production environment, most of them use offline analysis, so using the jmap+MAT tool is the best way to common combination.

 

When the above program is executed, OutofMemory will inevitably be triggered, and then after finding the generated dump file in the specified directory, we can analyze it through the MAT tool. When MAT is successfully started, after opening the specified dump file through the menu option "File->Open heap dump...", the Overview option will be generated, as shown in Figure 1-1:

Figure 1-1 Overview option

 

In the Overview option, some basic information of program memory consumption is listed in the form of a pie chart, in which each pie block of different colors represents a different proportion of memory consumption. If we need to locate the code points of memory leaks, we can use the Dominator Tree menu option to troubleshoot (MAT tool is just an aid, there is no fixed method and criteria for analyzing OutofMemory, so careful observation and analysis can find the problem. ), as shown in Figure 1-2:

  

Figure 1-2 Dominator Tree Options

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326158513&siteId=291194637