Java Memory Analysis Tool MAT use detailed

Source: Internet
Author: User

This is a note to read Mat helper. Heap dump is a memory snapshot of the Java process at a specific time. A full GC is usually performed before the heap dump is triggered, so the contents of the dump include the object after the GC.

What the dump file contains:

1, all objects: class, domain, native value and reference;

2, all classes: ClassLoader, class name, super class, Static domain;

3,GC Root: A touch-accessible object defined by the JVM;

4, line stacks and local variables: Call stack for threads, information per frame for local objects.

The dump file does not contain allocation information for memory, so it cannot query information about who created which object.

The shallow heap is a memory space occupied by an object that requires 32 or 64bits.

Retained set of X is a set of object that X is removed after being reclaimed by the JVM GC.

Retained heap of X is the and of the shallow heap size of all objects in retained set of X. In other words, keeping x alive requires memory space.

In layman's words, the shallow heap is the actual space in memory of an object, and the retained heap is the space in which an object is freed up by GC after it is reclaimed.

This figure can read what is leading set and what is retained set.

Dominator Tree: Defines an object x dominate object Y, when each path from root to Y is x. The white is that as long as there is a Y object to survive, then there must be an X object. Dominator trees are the tree structures in which the object reference graph is converted. It helps to discover the alive dependencies between objects, while also identifying the largest chunk of retained memory. Immediate Dominator X of Y is the nearest dominator from Y.

Dominator Tree has several properties:

1, the subtree of object x contains the object (set of objects x dominate), which represents the retained set of x;

2, if X is immediate dominator of Y, then x immediate dominator same dominate Y, etc.;

The edges in the 3,dominate tree do not represent the corresponding edges in the object's reference map, and are not strictly direct object references.

This diagram reflects an example of an object reference graph being converted into a dominator tree.

GC Root: A GC roots is an object that can be accessed from outside the heap. Here are some ways to make an object a GC root.

1,system class: Classes loaded by bootstrap or the System class loader, such as java.util.* in Rt.jar;

2,jni The local variables in the local:native code, such as the user-defined JNI code and the internal code of the JVM;

3,JNI the global variable in the Global:native code;

4,thread BLOCK: The object referenced in the current active thread block;

5,thread: The thread that has been started and does not have a stop;

6,busy Monitor: Called Wait () or notify () or synchronized synchronized object, if it is the synchronized method, then static method refers to the class, non-static method refers to the object;

7,java local:local variables, such as the method's entry parameters and the variables created within the method;

8,native the entry and exit parameters in the Stack:native code, such as the File/net/io method and the reflection parameters;

9,finalizable: The object that waits for its finalizer to run in a queue;

10,unfinalized: An object with a Finalize method, has not yet been finalize, and has not entered the finalizer queue to wait for finalize;

11,unreachable: Objects that are not touched, are marked as root in the mat to retain object, otherwise they will not appear in the analysis;

The 12,java stack Frame:java stack frame contains local variables that are generated when dump is parsed and the stack frame is set as an object in the preferences;

13,unknown: The root type of the location.

Here are some ways to get the dump:

1, in Oom DUMP:JVM parameter:-xx:+heapdumponoutofmemoryerror

2, under interactive environment dump:

1) JVM Parameters:-xx:+heapdumponctrlbreak

2) with external tools:jmap-dump:format=b,file=<filename.hprof> <pid>

3) with external tools:jconsole

4) with external tools: MAT

5) kill-3 <pid>

6) jstack-l <pid> > <dumpfile>

Some troubleshooting methods:

1, find large objects through top consumers, can be group by according to class, ClassLoader and package;

2, find the responsibility object through immediate dominator, it is very useful for quickly locating the holder of a set of objects, this operation directly solves the problem of "who makes these objects alive", rather than "who has the reference of these objects" problem, more direct and efficient;

3, running ClassLoader analysis, this importance is reflected in the highlights: first, the application uses different ClassLoader load classes, second, different classloader loaded classes stored in different permanent generations, which can theoretically be recycled. When a class is loaded by different classloader, it is necessary to determine which loader is more important according to the number of instance under their respective loader, and to reclaim the other;

4, the parsing thread, itself heap dump contains the thread information, can be viewed through the Mat threads overview and Detail,detail in the heap memory information, also wired stacks, but also contains the operating system local stack. Suppose we don't do heap dump, we check for problems with the system, how can we troubleshoot it from a thread perspective? First Top-h-P <pid> View Java application running in thread mode, find a thread that consumes CPU or memory, record thread ID, then printf%x <tid> to 16, then Jstack-l <pid > > Thread.log to dump the thread of the Java process, find the TID from the inside, and analyze which thread is consuming system resources.

5, the Java container classes are analyzed because Java's container classes are the most commonly used to store objects, so the risk of memory leaks in theory is the highest. Can be viewed from several angles: 1) The array fill rate query (fill rate fill ratio is the ratio of non-empty elements in the array), print the fill rate frequency distribution of an array of not native types, and thus troubleshoot the utilization of array in the system; 2) The array is queried by size, printing a A histogram grouped by size; 3) Collection fill rate query, arraylist/hashmap/hashtable/properties/vector/weakhashmap/concurrenthashmap$ SEGMENT;4) Collection The histogram according to size; 5) View all objects in a list, 6) View all objects in the HashMap, 7) View objects in HashSet; 8) Check the collision rate of the map; 9) Check all only one common Array of quantities.

6, Analysis finalizer,1) query finalizer the object being processed, 2) query finalizer ready to process objects; 3) view finalizer thread directly; 4) View the thread local object of the finalizer threads.

Java Memory Analysis Tool MAT use detailed

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.