This article summarizes the MAT tool for troubleshooting memory overflow problems. First, take a look at the contents of this article:

Java heap memory analysis tool.

1@RestController 2public class MatController { 3 4 List<User> list = new ArrayList<>(); 5 6 @GetMapping("/mat") 7 public String mat() { 8 for (int i = 0; i < 10000000; i++) { 9 User user = new User(); 10 user.setId(1L); 11 user.setusername (" old "+ I); 12 list.add(user); 13 } 14 return "ok"; 16 15}}Copy the code

Set JVM startup parameters

1-Xms20m -Xmx20m -XX:MaxPermSize=20m -XX:+HeapDumpOnOutOfMemoryError
Copy the code

Go to http://localhost:8080/mat

It was only a few minutes before we reported an anomaly. then

At the same time, a file is generated

Actual combat MAT

Download address:

www.eclipse.org/mat/downloa…

Download it locally and unzip it

Double-click memoryAnalyzer.exe to open

Import the javA_pid4928.hprof file generated earlier

Click finish

My god, it immediately pinpointed the MatController class as the most likely candidate for the problem (although online environments are usually not this easy).

Click on the largest area in the diagram

  • Outgoing References The outgoing References object
  • Incoming References Indicates the import of the object

In addition, Path to GC Roots is a common feature for fast analysis, showing the Path to GC Roots.

tree

There’s an icon in the upper left corner, a tree

Enter the tree

We knew we had a problem with our MatController, and it was at the top. Two other key attributes:

  • Shallow Heap: indicates the memory footprint of the object itself.
  • Retained Heap: indicates the memory footprint of an object

There’s also a Regex on the MatController class that supports regular expressions, so I typed in my own name tian and hit Enter:

Then, all tian related output is output.

Similarly, if we are troubleshooting online problems, we can also input our unique keywords to quickly locate problems. Each company basically has its own separate package catalog. We can use that particular keyword to search.

So if I open the MatController class, I have a List, and the List holds the User object, and that’s what we have here, right?

A histogram

Again, there’s a bar graph in the upper left corner

Click on the inside

Regex = Regex; Regex = Regex;

And we found that

1com.tian.dto.User
Copy the code

The number of objects is 132033.

OQL

MAT supports a SQL-like Query Language OQL (Object Query Language), which is also supported by VisualVM tools.

OQL represents classes as tables, objects as rows, and fields as columns.

Grammar:

1SELECT *
2FROM [ INSTANCEOF ] <class name="name">
3[ WHERE <filter-expression> ]
4</filter-expression></class>
Copy the code

Use F5 or Ctrl+ Enter or click the red exclamation mark to execute OQL immediately.

Typing keywords can also prompt:

The query results