使用 Eclipse Memory Analyzer Tool (MAT) 深度分析 Java 堆转储文件(.hprof),提供完整的内存分析报告。
- 必需: Eclipse MAT 1.15+ 已安装
- 环境变量:
MAT_HOME指向 MAT 安装目录
- 下载 Eclipse MAT: https://www.eclipse.org/mat/downloads.php
- 解压到本地目录(如
D:\Develop\MAT\mat) - 设置环境变量
MAT_HOME
# 临时设置(当前会话)
$env:MAT_HOME = "D:\Develop\MAT\mat"
# 永久设置
[System.Environment]::SetEnvironmentVariable("MAT_HOME", "D:\Develop\MAT\mat", "User")# 1. 检测 MAT 环境(自动搜索常见路径)
$matPaths = @(
"C:\Program Files\Eclipse\MAT",
"C:\MAT",
"D:\Develop\MAT\mat",
"D:\MAT"
)
$foundMat = $null
foreach ($path in $matPaths) {
if (Test-Path "$path\ParseHeapDump.bat") {
$foundMat = $path
break
}
}
if ($foundMat) {
$env:MAT_HOME = $foundMat
Write-Host "找到 MAT: $foundMat"
} else {
Write-Host "未找到 MAT,请安装或手动设置 MAT_HOME"
}
# 2. 执行 MAT 分析(生成 Leak Suspects Report)
& "$env:MAT_HOME\ParseHeapDump.bat" C:\path\to\heapdump.hprof org.eclipse.mat.api:suspects
# 3. 解压报告
Expand-Archive -Path "C:\path\to\heapdump.hprof_Leak_Suspects.zip" -DestinationPath "mat-report" -Force
# 4. 分析报告文件
# - mat-report/index.html - 泄漏嫌疑点摘要
# - mat-report/pages/System_Overview2.html - 系统概览
# - mat-report/pages/Class_Histogram7.html - 类直方图
# - mat-report/pages/Thread_Overview5.html - 线程概览
# - mat-report/pages/Top_Consumers6.html - 大对象分析# Windows
.\scripts\mat-analyzer.bat C:\path\to\heapdump.hprof C:\output\dir
# Linux/Mac
./scripts/mat-analyzer.sh /path/to/heapdump.hprof /output/dir分析报告包含以下所有维度(无论是否有问题):
- 系统概览 (System Overview) - 堆内存、对象数、类数、GC Roots
- 泄漏嫌疑点分析 (Leak Suspects) - 内存泄漏检测
- 类直方图 (Class Histogram) - Top N 内存占用类
- 线程概览 (Thread Overview) - 所有线程状态和堆栈
- 大对象分析 (Top Consumers) - 单个最大对象
- 支配树分析 (Dominator Tree) - 对象引用关系
- GC Roots 分析 - 阻止回收的根引用
- 系统属性 (System Properties) - JVM 参数和环境变量
$env:MAT_HOME = "D:\Develop\MAT\mat"编辑 MemoryAnalyzer.ini,调整 -Xmx 参数:
-Xmx8g
Uses Eclipse Memory Analyzer Tool (MAT) to deeply analyze Java heap dump files (.hprof) and provide comprehensive memory analysis reports.
- Required: Eclipse MAT 1.15+ installed
- Environment Variable:
MAT_HOMEpointing to MAT installation directory
- Download Eclipse MAT: https://www.eclipse.org/mat/downloads.php
- Extract to local directory (e.g.,
D:\Develop\MAT\mat) - Set environment variable
MAT_HOME
# Temporary (current session)
$env:MAT_HOME = "D:\Develop\MAT\mat"
# Permanent
[System.Environment]::SetEnvironmentVariable("MAT_HOME", "D:\Develop\MAT\mat", "User")# 1. Detect MAT environment (auto-search common paths)
$matPaths = @(
"C:\Program Files\Eclipse\MAT",
"C:\MAT",
"D:\Develop\MAT\mat",
"D:\MAT"
)
$foundMat = $null
foreach ($path in $matPaths) {
if (Test-Path "$path\ParseHeapDump.bat") {
$foundMat = $path
break
}
}
if ($foundMat) {
$env:MAT_HOME = $foundMat
Write-Host "Found MAT: $foundMat"
} else {
Write-Host "MAT not found, please install or set MAT_HOME manually"
}
# 2. Execute MAT analysis (generate Leak Suspects Report)
& "$env:MAT_HOME\ParseHeapDump.bat" C:\path\to\heapdump.hprof org.eclipse.mat.api:suspects
# 3. Extract report
Expand-Archive -Path "C:\path\to\heapdump.hprof_Leak_Suspects.zip" -DestinationPath "mat-report" -Force
# 4. Analyze report files
# - mat-report/index.html - Leak suspects summary
# - mat-report/pages/System_Overview2.html - System overview
# - mat-report/pages/Class_Histogram7.html - Class histogram
# - mat-report/pages/Thread_Overview5.html - Thread overview
# - mat-report/pages/Top_Consumers6.html - Large object analysis# Windows
.\scripts\mat-analyzer.bat C:\path\to\heapdump.hprof C:\output\dir
# Linux/Mac
./scripts/mat-analyzer.sh /path/to/heapdump.hprof /output/dirThe analysis report includes all the following dimensions (regardless of issues):
- System Overview - Heap memory, object count, class count, GC Roots
- Leak Suspects Analysis - Memory leak detection
- Class Histogram - Top N memory-consuming classes
- Thread Overview - All thread states and stacks
- Top Consumers - Individual largest objects
- Dominator Tree Analysis - Object reference relationships
- GC Roots Analysis - Root references preventing collection
- System Properties - JVM parameters and environment variables
$env:MAT_HOME = "D:\Develop\MAT\mat"Edit MemoryAnalyzer.ini, adjust -Xmx parameter:
-Xmx8g
SKILL.md- 中文版本 | Chinese versionSKILL_EN.md- English version