Mastering JVM Heap Memory Analysis with MAT Tool
Introduction
During Java application development and operations, memory leaks and out-of-memory issues are common performance bottlenecks. To quickly identify these issues, we need professional memory analysis tools. MAT (Memory Analyzer Tool) serves as an excellent open-source tool for Java heap memory analysis, helping developers efficiently identify memory leaks, analyze memory usage, and optimize performance.
Overview of MAT Tool
MAT (Memory Analyzer Tool) is a free tool provided by the Eclipse Foundation, specifically designed for analyzing Java heap dump files. It can quickly locate memory leaks, analyze memory usage, and provide detailed memory views.
Main Features
- Fast analysis of large heap dumps (supports files of several GB)
- Automatic detection of memory leak suspects
- Multiple views to display memory usage
- Rich query language OQL (Object Query Language)
Advantages
- Fast analysis speed
- High accuracy in detection
- User-friendly interface with intuitive operations
- Rich plugins with strong extensibility
MAT Installation and Configuration
Download and Installation
- Visit the Eclipse MAT official website to download the latest version
- MAT provides standalone packages and Eclipse plugin forms
- Recommended to use standalone packages without installing Eclipse
System Requirements
- Java Runtime Environment 8 or higher
- Recommended memory: Allocate at least 1.5 times heap memory to MAT
- 64-bit operating system (recommended for processing large heap dumps)
Basic Configuration
When launching MAT, you can adjust JVM parameters by modifying the mat.ini file:
1 | -Xmx8g # Set maximum heap memory |
Creating Heap Dump Files
Trigger Conditions
- Application throws OutOfMemoryError
- Manual triggering of heap dumps
- Regular automatic creation of heap dumps
Generation Methods
1. Auto-generation via JVM Parameters
1 | java -XX:+HeapDumpOnOutOfMemoryError \ |
2. Using jmap Command Manually
1 | # Get Java process ID |
3. Using jcmd Command
1 | # List all Java processes |
MAT Core Views Explained
1. Histogram
Displays the number of instances and memory usage for each class, quickly identifying objects that consume large amounts of memory.
Interpretation Points:
- Class Name: Shows the full class name
- Objects: Number of instances of the class
- Shallow Heap: Memory occupied by the object itself
- Retained Heap: Total memory occupied by the object and its associated objects
2. Dominator Tree
Shows reference relationships between objects and identifies which objects occupy the most retained memory.
Features:
- Shows complete object reference chains
- Sorted by Retained Heap
- Directly locates memory leak origins
3. Leak Suspects
MAT’s intelligent analysis feature that automatically identifies the most likely memory leak points.
4. Thread Overview
Analyzes thread-related information, including thread stacks, local variables, etc.
5. Biggest Objects by Size
Displays the largest objects by memory usage, facilitating quick identification of large object issues.
MAT Usage in Detail
1. Opening Heap Dump Files
- Launch MAT
- Select “Open a Heap Dump”
- Choose the hprof file
- Wait for analysis completion (creates indexes to improve query speed)
2. Memory Leak Detection
- View Leak Suspects report
- Look for unusually large objects in Dominator Tree
- Use Path To GC Roots function to analyze object retention reasons
3. Path to GC Roots Analysis
Right-click suspicious object → Path To GC Roots → Exclude weak/soft/phantom references
This analysis method identifies all reference paths preventing object collection.
4. Query Language (OQL) Usage
MAT provides SQL-like query language OQL:
1 | -- Find instances of specific class |
Practical Heap Dump Problem Diagnosis
Scenario 1: Locating Memory Leaks
Step 1: Use Leak Suspects Report
After opening the heap dump, MAT automatically generates a Leak Suspects report containing:
- Summary information about suspicious leak points
- Possible cause analysis
- Reference chains of related objects
Step 2: Analyze Dominator Tree
- Sort by Retained Heap
- Look for objects consuming unusually large memory
- Expand objects to view their references
Step 3: Verify Assumptions
- Use Path To GC Roots to analyze why objects aren’t collected
- Review related business logic code
- Confirm if references are not properly released
Scenario 2: Analyzing Large Objects
Step 1: Use Histogram for Finding
- Sort by Shallow Heap
- Find objects consuming large amounts of memory
- Analyze the reason for object creation
Step 2: Track Object Origins
- Use Merge Shortest Paths to GC Roots
- Find the code path that created these large objects
- Evaluate optimization opportunities
Scenario 3: Collection Class Analysis
Step 1: Locate Large Collections
- Find Collection classes (HashMap, ArrayList) in Histogram
- Check their instance sizes and numbers
Step 2: Analyze Collection Contents
- Use List Objects to view elements in collections
- Analyze collection growth trends
- Check if appropriate cleanup mechanisms exist
Advanced Techniques and Best Practices
1. Optimizing MAT Performance
- Allocate sufficient memory to MAT
- Use SSD storage for temporary files
- Appropriately adjust MAT’s JVM parameters
2. Analysis Techniques
- Check Leak Suspects report first
- Focus on Retained Heap rather than Shallow Heap
- Use comparison analysis to compare heap dumps at different time points
3. Memory Leak Prevention
- Avoid storing large objects in static collections
- Close resources promptly (InputStream, Connection, etc.)
- Use WeakReference for caching
Common Memory Problem Patterns
1. Collection Leaks
- HashMap, ArrayList, etc. not cleaned up in time
- Cache without expiration policies
2. Listener Leaks
- Registered listeners not unregistered
- Anonymous inner classes holding outer class references
3. Static Variable Leaks
- Static variables holding references to large objects
- Growing static collections
Conclusion
MAT is an essential memory analysis tool for Java developers, and mastering its usage is crucial for performance tuning. By making reasonable use of MAT’s various features, we can quickly locate memory leaks, optimize memory usage, and thereby enhance application stability and performance.
Remember that memory analysis is not only a technical issue but also a deep understanding process of application architecture and business logic. Developing good programming habits in daily development and preventing memory issues at the source is the fundamental approach.