Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use mat to analyze java heap in java

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

It is believed that many inexperienced people have no idea about how to use mat to analyze java heap in java, so this paper summarizes the causes and solutions of the problem. I hope you can solve this problem through this article.

MAT, short for memory analyzer, is a powerful java heap memory analyzer that can be used to find memory leaks and check memory consumption, which can be downloaded from the MAT website.

1. First acquaintance 1.1 Export heap application snapshot

Jmap, jconsole, jvisualvm and other tools can export heap snapshot files of java applications, and MAT also has this feature, as shown in the figure:

After clicking the "Accquire Heap Dump" menu, the current java application list pops up, and you can select the application you want to analyze, as shown in the figure:

1.2 Open the heap snapshot file

In addition to exporting a heap snapshot of the application directly in MAT, you can also use "Open Heap Dump" to open an existing heap snapshot file.

As shown in the figure, shows the interface of MAT after the heap snapshot file is opened normally:

In the right interface, the total size, classes, instances, and ClassLoader of the heap snapshot file are displayed.

In the pie chart, the largest object in the current heap snapshot is shown.

Hover over the pie chart to view the corresponding information about the object in the Inspector interface on the left.

Click an object in the pie chart to do more with the selected object.

1.3 View the memory usage of all classes

As shown in the figure, click the bar chart on the toolbar to display the memory usage of all classes in the system:

1.4 View java threads

MAT can also view Java threads, as shown in the figure:

Of course, if you look at the application threads at the java level, the system threads of the virtual machine cannot be displayed. Through the thread's stack, you can also view the information of local variables. As shown in the following figure, the marked local variable of the current frame stack may be missing.

In addition, MAT can also view detailed thread stack information:

By opening the thread_detail tab, you can clearly see the thread stack information:

1.4 View references to objects

Another common function of MAT is to cross-view in the reference list of individual objects. For a given object, the object referencing the current object (Incomming References) and the object referenced by the current object (Outgoing References) can be found through MAT, as shown in the figure:

two。 Shallow pile and deep pile

Shallow heap (Shallow Heap) and deep heap (Retained Heap) are two very important concepts, which represent the amount of memory occupied by an object structure and the amount of memory that can be actually freed after an object is reclaimed by GC.

Shallow heap refers to the memory consumed by an object. In a 32-bit system, an object reference will occupy 4 bytes, an int type variable will occupy 4 bytes, an long type variable will occupy 8 bytes, and each object header will occupy 8 bytes. Depending on the format of the heap snapshot, the size of the object may be aligned to 8 bytes.

Depending on the format of the heap snapshot, the size of the object may be aligned to 8 bytes. Take the String object as an example, as shown in the following figure, showing several properties of the String object.

String

Value:char []

Offset:int

Count:int

Hash:int

The 3 int values occupy 12 bytes, the object references occupy 4 bytes, and the first 8 bytes of the object, totaling 24 bytes. The size of the shallow heap is only related to the structure of the object, independent of the actual content of the object. That is, no matter how long the string is and what the content is, the size of the shallow heap is always 24 bytes.

The concept of deep heap (Retained Heap) is slightly complicated. To understand the deep heap, you first need to understand the Retained Set. The reserved set of object A refers to all the sets of objects that can be released (including object An itself) after object An is garbage collected, that is, the reserved set of object A can be considered as a collection of all objects that can only be accessed directly or indirectly through object A. In popular terms, it refers to the collection of objects held only by object A. A deep heap is the sum of the shallow heap sizes of all objects in an object's retention set.

Note: shallow heap refers to the memory occupied by the object itself, excluding the size of its internal reference object. The deep heap of an object refers to the sum of (directly or indirectly) the shallow heap of all objects that can only be accessed through the object, that is, the real space that can be released after the object is reclaimed.

Another common concept is the actual size of the object. Here, the actual size of an object is defined as the sum of the shallow heap sizes of all objects that an object can reach, which is what we usually call the object size. Compared with deep heap, this seems to be more intuitive and acceptable in daily development, but in fact, this concept has nothing to do with garbage collection.

As shown in the figure, a simple object reference diagram is shown, with object A referencing C and D, and object B referencing C and E. Then the shallow heap size of object An is only An itself, excluding C and D, and the actual size of An is the sum of A, C and D. The deep heap size of An is the sum of An and D, and because object C is also accessible through object B, it is not within the deep heap range of object A.

3 dominant tree

MAT provides an object graph called Dominator Tree. The domination tree reflects the dominant relationship between object instances. In the object reference graph, if all paths to object B pass through object A, it is considered that object A dominates object B. If object An is the nearest dominant object to object B, object An is considered to be the direct dominator of object B. The domination tree is based on the reference graph between objects, and has the following basic properties:

The subtree of object A (all collections of objects dominated by object A) represents the reserved set (Reatined Set) of object A, that is, deep heap.

If object A dominates object B, then the direct controller of object An also dominates object B.

The edges of the dominating tree do not directly correspond to the edges of the object reference graph.

As shown in the figure, the left represents the object reference graph, and the right represents the domination tree corresponding to the left. Objects An and B are directly controlled by the root object. Because the path to object C can pass through An or B, the direct controller 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 ruler of object F. All the paths to object D must pass through object C, even if the reference from object F to object D starts from the root node, it also passes through object C. therefore, the direct controller of object D is object C.

In MAT, click the object domination tree button on the toolbar to open the object domination tree view:

Note: in the object domination tree, the subtree of an object represents a collection of objects that will also be recycled after the object is reclaimed.

4. Case analysis of MAT heap analysis

First prepare the case code:

Package jvm.chapter07;import java.util.List;import java.util.Vector;/** * {add description here} * * @ author chengyan * @ date 2019-11-15 11:01 p.m. * / public class Demo04 {private static List webPages = new Vector (); public static void createWebPages () {for (int I = 0; I < 100; iTunes +) {WebPage webPage = new WebPage () WebPage.setUrl ("http:www." + Integer.toString (I) + ".com"); webPage.setContent (Integer.toString (I)); webPages.add (webPage);}} public static void main (String [] args) {createWebPages (); Student student3 = new Student (3, "billy"); Student student5 = new Student (5, "alice") Student student7 = new Student (7, "taotao"); for (int I = 0; I < webPages.size (); iTunes +) {if (I% student3.getId () = = 0) {student3.visit (webPages.get (I));} if (I% student5.getId () = = 0) {student5.visit (webPages.get (I)) } if (I% student7.getId () = = 0) {student7.visit (webPages.get (I));}} webPages.clear (); System.gc ();}} class Student {private int id; private String name; private List history = new Vector (); public void visit (WebPage webPage) {history.add (webPage) } public Student (int id, String name) {this.id = id; this.name = name;} public int getId () {return id;} public void setId (int id) {this.id = id;} public String getName () {return name;} public void setName (String name) {this.name = name } public List getHistory () {return history;} public void setHistory (List history) {this.history = history;}} class WebPage {private String url; private String content; public String getUrl () {return url;} public void setUrl (String url) {this.url = url;} public String getContent () {return content } public void setContent (String content) {this.content = content;}}

As you can see, in the Demo04.java class, 100 URLs are first created. In order to make it easier to read, the URLs here use numbers as domain names, respectively. After that, the program creates three students: billy, alice and taotao. They visited websites that were divisible by 3, 5 and 7, respectively. After the program runs, the 3 students should keep the web addresses they have visited in their history. Now, you want to get the heap information of the system and analyze it before the program exits, and check the URL that each student actually visits.

Run the program with the following parameters:

-XX:+HeapDumpBeforeFullGC-XX:HeapDumpPath=./stu.hprof4.1 uses MAT to open heap files

Open the resulting stu.hprof file using MAT, as shown in the figure:

The screen on the right shows the total size, classes, instances, and ClassLoader of the heap snapshot file. In the pie chart on the right, the largest object in the current heap snapshot is shown. Hover over the pie chart to view the corresponding information about the object in the Inspector interface on the left. Click an object in the pie chart to do more with the selected object.

4.2 View Thread

The references to the three students can be found through the main thread in the thread view, as shown in the figure, you can clearly see the student names of the three objects. In addition to object names, MAT also gives shallow heap sizes and deep heap sizes. As you can see, the shallow heap of all Student classes is 24 bytes, regardless of what they hold. The deep pile varies in size, which is related to the web site visited by each student.

Of course, if you look at the application thread at the Java level here, it cannot be displayed for the system thread of the virtual machine. Through the thread's stack, you can also view the information of local variables. Marked is the local variable of the current frame stack, and this information may be missing.

4.3 View the outgoing references of the object

In order to get the URLs that taotao students have visited, you can find the objects that can be accessed by taotao, that is, the URLs they have visited, by "Outgoing References" in the taotao records, as shown in the figure:

The websites visited are as follows:

You can see that the URLs in the history of all taobao students are fully displayed in the heap.

4.4 View incoming references

If you want to see which students have visited "http://www.0.com"" now, you can find it through "Incoming References" in the corresponding WebPage object:

Obviously, this website has been visited by all three students.

4.5 View the dominant tree

As shown in the figure, the object domination tree for Main Thread is shown. The objects directly controlled by the Student object are placed in the history under the object, that is, when the Student object is recycled, all the objects dominated by the object will be recycled as well.

In addition, there is a part of WebPage whose parent node is Thread, which indicates that this part of WebPage is held by multiple Student objects at the same time, for example, 84 is divisible by 3 and 7 at the same time, which indicates that the object will be held by both billy and taobao, and when one of billy or taobao is recycled, the object will not be recycled, so it will not be displayed separately in the history of billy or taobao.

4.6 View the memory usage of all classes

Click the bar chart on the toolbar to display the memory usage of all classes in the system. The figure shows the statistical information of all the classes in the system, including the number of instances and the space occupied by the class.

For ease of viewing, the bar chart also provides sorting of classes by Class Loader and package. The following figure is the output of a histogram sorted by package.

After reading the above, have you mastered how to use mat to analyze the java heap in java? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report