1 Generate the Dump file
1.1 VisualVM
Start the Java program without exiting, and you’ll see it when you open VisualVM. As shown below, double click
1.2 Using JMAP
Jmap -dump:format=b,file=' path ' 'path'Copy the code
1.3 mat
2 Shallow heap and deep heap
2.1 shallow pile
Refers to the memory occupied by the object itself, not the size of its internal reference object
2.2 deep pile
The sum of the shallow heap of all objects that can only be accessed (directly or indirectly) by the object. That is, the total memory that can be reclaimed by the garbage collector when the object is no longer referenced, including memory occupied by the object itself and memory occupied by other objects that can only be referenced by the object.
As shown in the figure below, the shallow heap size of A is A itself, and since C is referenced by B, the deep heap size of all A is A+D; Similarly, the shallow heap size of B can be calculated as B and the deep heap size as B+E
3. Two important views — Histogram and Dominator tree
As shown in the following figure, the Shallow heap column and Retained heap column are displayed in both views.
3.1 Histogram
MAT’s histogram, like jmap’s -histo subcommand, shows the number of instances of each class and the total Shallow heap of those instances. However, MAT’s histogram can also calculate the Retained heap and allow ordering based on the number of instances or Retained heap (Shallow heap by default). In addition, MAT can group the classes in the histogram by superclass, classloader, or package name.
3.2 Dominator Tree
In the case where A dominates B and A is different from B (i.e., A strictly dominates B), A directly dominates B if there are no other dominating nodes in all paths from node A to node B. The dominant tree here refers to the tree structure composed of the direct dominant nodes of nodes.
Basic properties of dominant tree:
- The subtree of object A (the collection of all objects governed by object A) represents the retained set of object A, also known as the deep heap
- If object A governs object B, then the direct dominator of object A governs only object B
- The edges of the ruling tree do not correspond directly to the edges of the object reference graph
In the figure below, the left figure represents the object reference graph and the right figure represents the dominance tree corresponding to the left figure. Objects A and B are directly dominated by the root object, and since the path to object C can go through either A or B, the direct dominator of object C is also the root object. Object F and object D refer to each other, because all paths to object F must pass through object D, so object D is the direct dominator of object F. All paths to object D must go through object C, and even the reference from object F to object D, starting from the root node, also goes through object C. Therefore, object C is the direct dominator of object D.
4 Check GC Root
Open a dump file and view the GC Root list as shown below
The following image shows the thread executing the main method. If you expand it, you can see what properties are in the main method as GC Root
For a more detailed explanation, see the link below: [https://help.eclipse.org/2020-03/topic/org.eclipse.mat.ui.help/concepts/gcroots.html?cp=59_2_4](https://help.eclipse.or g/2020-03/topic/org.eclipse.mat.ui.help/concepts/gcroots.html?cp=59_2_4)
5 View the inbound reference and outbound reference
In the image below, right-click any object and select List Objects to display “with outgoing Referances “and” With incoming Referances “. Represent references (external objects referenced by the current object) and inreferences (objects that refer to the current object)
5.1 Sample Code
The program is used to simulate the user to visit the website, the website URL down. The 100 records are initialized and mod based on the user ID. After the conditions are met, the records are saved to the user history access record set.
public class WebPage { private String url; private String content; } public class Student {private int id; private String name; private List history = new Vector(); public Student(int id, String name) { this.id = id; this.name = name; }} public class TraceStudent {static List webPages = new Vector<>(); public static void createWebPages(){ for(int i = 0; i < 100; i++){ WebPage wp = new WebPage(); wp.setUrl("http://www."+ Integer.valueOf(i) + ".com"); wp.setContent(Integer.toString(i)); webpages.add(wp); } } public static void main(String[] args) { createWebPages(); Student st3 = new Student(3,"billy"); Student st5 = new Student(5,"alice"); Student st7 = new Student(7,"taotao"); for(int i = 0; i < webpages.size(); i++){ if(i % st3.getId() == 0){ st3.getHistory().add(webpages.get(i)); } if(i % st5.getId() == 0){ st5.getHistory().add(webpages.get(i)); } if(i % st7.getId() == 0){ st7.getHistory().add(webpages.get(i)); } } System.gc(); }}Copy the code
5.2 Operating Parameters
Run the program and use the following JVM parameters to get the heap information before the program exits
-XX:+HeapDumpBeforeFullGC -XX:HeapDumpPath=D:/tmp/tmp/stu.hprof
Copy the code
5.3 Viewing the Thread corresponding to the Main method
Refer to summary 4 to open the GC Root page, expand the Thread list, select the main Thread, and see the corresponding instance object
Since 0 mod any number is 0, all three Student objects in all programs store this URL. Select “With incoming Referances” to see which objects reference urls with subscript 0
Four are shown here, in addition to the three Student objects. There is also a collection of user-initialized stores that WebPages reference, so there are four in total.
Use “With outgoing Referances” on any Student object to see which external objects are referenced by the current object
This feature is used less often because in dominator Tree mode, you can see directly which external objects are referenced by the current object