Java memory analysis tool MAT Usage Details
This is a note for reading MAT helper. Heap dump is a memory snapshot of a java Process at a specific time. Generally, a full gc is performed before heap dump is triggered, so that the dump content contains the object after gc.
Dump File Content:
1. All objects: Class, domain, native value and reference;
2. All classes: classloader, class name, superclass, static fields;
3. GC root: The reachable object defined by JVM;
4. Thread stack and local variables: call stack of the thread, information of each frame of the local object.
The dump file does not contain the memory allocation information. Therefore, you cannot query the information such as who created the object.
Shallow heap is the memory space occupied by an object. An object requires 32 or 64 bits.
Retained set of X is a group of objects that X is removed after being recycled by jvm gc.
Retained heap of X is the sum of shallow heap size of all objects in retained set of X. In other words, it is the memory space required to keep X alive.
In general, shallow heap is the actual space of an object in the memory, and retained heap is the space released by the memory after the object is recycled by gc.
This figure shows what leading set is and what retained set is.
Dominator tree: defines an object x dominate object y. When each path from root to y passes through x. To put it bluntly, As long as y objects survive, there must be an x object. Dominator tree is the tree structure that converts an object reference graph. It helps to discover the alive dependency between objects and also recognizes the largest chunk of retained memory. Immediate dominator x of y is the dominator closest to y.
Dominator tree has several attributes:
1. The child tree of object x contains the object set of object x dominate), representing the retained set of x;
2. If x is the immediate dominator of y, then the immediate dominator of x is also dominate y, and so on;
3. the edges in the dominate tree do not represent the edges corresponding to the object reference sequence. They are not strictly direct object references.
This figure shows an example of converting an object reference graph into a dominator tree.
Gc root: a gc root is an object that can be accessed and read from outside the heap. The following methods make an object the gc root.
1. System class: class loaded by Bootstrap or system class loaders, such as java. util. * In rt. jar .*;
2. JNI local: The local variable in the native code, such as the User-Defined JNI code and JVM internal code;
3. JNI global: global variable in native code;
4. Thread block: The objects referenced in the currently active Thread block;
5. Thread: the Thread that has been started and has no stop;
6. busy monitor: the object that is called wait () or synchronized y () or synchronized by synchronized. If synchronized is used, the static method refers to the class, non-static methods refer to objects;
7. java local: local variable, such as the input parameter of the method and the variable created in the method;
8. native stack: Access parameters in native code, such as file/net/IO methods and reflection parameters;
9. finalizable: the object waiting for its finalizer to run in a queue;
10. unfinalized: an object with the finalize method. It has not been finalize, nor has it entered the finalizer queue to wait for finalize;
11. unreachable: objects that will not be touched, marked as root in MAT for retain object, otherwise they will not appear in analysis;
12. java stack frame: the java stack frame contains local variables. It is generated only when the dump is parsed and the stack frame is set as an object in preferences;
13. unknown: root Type of location.
The following are some methods to get dump:
1. dump: JVM parameter at oom:-XX: + HeapDumpOnOutOfMemoryError
2. dump in an interactive environment:
1) JVM parameter:-XX: + HeapDumpOnCtrlBreak
2) use external tools: jmap-dump: format = B, file = <filename. hprof> <pid>
3) use external tools: jconsole
4) use external tools: MAT
5) kill-3 <pid>
6) jstack-l <pid> <dumpfile>
Some troubleshooting methods:
1. Search for large objects through top consumers. You can perform group by based on class, classloader, and package;
2. Finding the responsible object through immediate dominator is very useful for quickly locating the owner of a group of objects. This operation directly solves the problem of "who makes these objects alive, rather than the question of "who has the reference of these objects", it is more direct and efficient;
3. Run classloader analysis. This importance is highlighted in the following figure: first, the application uses different classloader loading classes. Second, classes loaded by different classloader are stored in different permanent generations, this can also be recycled theoretically. When a class is loaded by different classloader, it is necessary to determine which loader is more important based on the number of instances under each loader, so as to recycle the other one;
4. Analyze the thread. heap dump contains the thread information. You can use MAT to view the overview and detail of threads. In detail, there are thread heap memory information and thread stacks, the local stack of the operating system is also included. If heap dump is not performed, we check that there is a problem in the system. How can we troubleshoot it from the thread perspective? First, top-H-p <pid> checks the running status of java applications in thread mode, finds the thread that occupies cpu or memory, and records the thread id, then, printf % x <tid> is converted to hexadecimal, and then jstack-l <pid> thread. log dumps the thread of the java Process and finds tid from it to analyze which thread occupies system resources.
5. Analyze the java container class. Because the java container class is the most commonly used to store objects, the risk of Memory leakage is also the highest in theory. From several points of view: 1) array filling rate query filling rate fill ratio is the ratio of non-empty elements in the array), print the filling rate distribution of non-native arrays, so as to check the array utilization rate in the system; 2) query arrays by size group, print a histogram grouped by size; 3) query the filling rate of collection, arrayList/HashMap/Hashtable/Properties/Vector/WeakHashMap/ConcurrentHashMap $ Segment; 4) collection groups histograms by size; 5) view all objects in a list; 6) view all objects in a hashmap; 7) view objects in a hashset; 8) Check the collision rate of a map; 9) check that all objects have only one regular array.
6. Analyze Finalizer, 1) query the objects being processed by finalizer; 2) query the objects to be processed by finalizer; 3) view the finalizer thread directly; 4) view the local object of the finalizer thread.