This is the fourth day of my participation in the August Text Challenge.More challenges in August
Memory Analyzer (MAT)
The Eclipse Memory Analyzer is a fast and feature-rich Java heap analyzer that helps you find memory leaks and reduce memory consumption.
The installation
Official website: www.eclipse.org/mat/
At present, MAT has been iterated to version 1.12.0 and already supports parsing heap information of OpenJDK 15.
Note that MAT is itself a Java application, and the minimum Java version required to run it is Jdk 11.
configuration
Once unpacked, resize the Xmx in MemoryAnalyzer.ini as appropriate, based on your computer configuration and the size of the heap dump file.
Get the heap dump file
Introduce several ways:
- Dump directly from the server, usually with the help of o&M students, to the local analysis.
Jmap -dump:format=b,file={dumpfile.hprof} {pid} Example: jmap -dump:format=b,file=20210823.hprof 55244
Copy the code
- You can also configure startup parameters to dump files automatically when a memory overflow occurs.
-XX:+HeapDumpOnOutOfMemoryError
Copy the code
- With VisualVM, it is recommended to download an IDEA plug-in:
After the plug-in is installed, you can find the two new orange buttons behind the Debug button, which will open Java visualVM at startup, compared to the normal Run and Debug.
Switch to the monitoring TAB and click the button in the illustrated position to perform the dump operation.
Load the file
The most commonly used functions are Histogram and Leak Suspects.
First post the code:
@Controller
public class HelloWorldController {
@GetMapping("index")
public void index(a) throws InterruptedException {
hi();
}
public void hi(a) throws InterruptedException {
int size = 1024 * 1024;
List<byte[]> list = new ArrayList<>();
for (int i = 0; i < 100; i++) {
System.out.println("Stored" + (i + 1) + "M data");
list.add(new byte[size]);
Thread.sleep(1000); }}}Copy the code
Leak Suspects:
The yellow part of the figure is the problem description. A Tomcat task thread saved 33M of local variables, and even gave the specific class name and line number in the Keywords.
Click on details to see the complete stack information
Histogram
If there is a Strong reference chain on an object, it is necessary to manually start GC before exporting HeapDump. If there is a Strong reference chain on an object, it is necessary to manually start GC before exporting HeapDump. There is a memory leak, and then the specific reference is checked.
You can clearly see that there are 33 byte arrays in the List, each taking up 1M memory.
Well, that’s all for today, and we’ll see you next time at 👋🏻.