Build a simple Springboot application, simulate OOM scenario, and export heap dump file for Mat analysis.

Build a simple Springboot to simulate an OOM scenario

Build a simple SpringBoot project and add objects in the Controller layer until OOM.

// Loop to add objects, simulate OOM
List<UserVo> userVoListNew = new ArrayList<>();
for (int i = 0; i < 1024 * 1024 * 1024; i++) {
  userVoListNew.addAll(userVoList);
}
Copy the code

Specific code address, as long as the local start mysql, Redis will run (generally half a cigarette directly docker start, convenient and fast) : gitee.com/yclxiao/blo…

Set idea & export dump file

1. Configure VM options for IDEA

In VM Options, set the maximum memory and set the dump file location

-Xmx512m
-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=/Users/yclxiao/Desktop/java_error_in_idea.hprof
Copy the code

2. Start the project

To start the project, Swagger calls the userController method and waits for OOM to appear. Finally, the javA_ERROR_in_idea.hprof file appears in the configured location.

Mat analyzes dump files

Install the Memory Analyzer

Download address: www.eclipse.org/mat/downloa…

Importing dump files

When importing dump files, you may encounter the following errors:

An internal error occurred during: "Parsing heap dump from '/Users/yclxiao/Desktop/heapdump-1586847376115.hprof'".
Java heap space
Copy the code

Solution: modify the Applications/mat. The app/Contents/Eclipse/MemoryAnalyzer ini the biggest memory configuration

Note: Half cigarette is MAC platform, window platform file location slightly different, friends to find it, haha!

Dump file

1. Mat analysis overview

The exported dump file is about 400 MB. After MAT analysis, a pie chart will be shown showing the top large objects

2, clickDominator Tree & Leak Suspects

Click on the Dominator Tree to list the ranking of the largest objects and which objects they hold. Click on the Leak Suspects for a more suspect Leak and an overview of the system.

3, clickDominator Tree

You can see the largest Object[], the left side shows the Object it holds, according to this UserVo we can probably check the specific code location.

4, clickLeak Suspects

You can roughly see which thread the request is coming from, which large object it is, which specific business code it is, and also have stack information, which can be used to quickly locate the problematic business code.

conclusion

This article introduces the common methods to analyze OOM. If you encounter an online problem, first calm down and do not panic, first reserve the thread (set OOM dump location), and then restart the service, then use Mat to analyze the problem.

Of course, the problem may also appear in other places, half a cigarette encountered problems are generally top, df, free these three commands run through, first look at the machine’s CPU, hard disk, memory and other conditions, and then do follow-up analysis.

If more good suggestions, welcome to communicate with me.

The original link