GC调优:G1/ZGC/Shenandoah
GC调优:G1/ZGC/Shenandoah
G1垃圾回收器
G1(Garbage-First)是JVM默认的垃圾回收器,适合大多数应用场景。
// G1 GC配置
@Component
public class G1GCTuner {
// G1 GC调优参数
public GCConfig getRecommendedConfig(ApplicationProfile profile) {
GCConfig config = new GCConfig();
switch (profile) {
case LOW_LATENCY:
config.add("-XX:+UseG1GC");
config.add("-XX:MaxGCPauseMillis=100");
config.add("-XX:G1HeapRegionSize=8m");
config.add("-XX:InitiatingHeapOccupancyPercent=35");
config.add("-XX:G1ReservePercent=15");
break;
case HIGH_THROUGHPUT:
config.add("-XX:+UseG1GC");
config.add("-XX:MaxGCPauseMillis=500");
config.add("-XX:G1HeapRegionSize=32m");
config.add("-XX:InitiatingHeapOccupancyPercent=45");
config.add("-XX:ParallelGCThreads=16");
config.add("-XX:ConcGCThreads=8");
break;
case BALANCED:
config.add("-XX:+UseG1GC");
config.add("-XX:MaxGCPauseMillis=200");
config.add("-XX:G1HeapRegionSize=16m");
config.add("-XX:InitiatingHeapOccupancyPercent=40");
config.add("-XX:ParallelGCThreads=8");
config.add("-XX:ConcGCThreads=4");
break;
}
return config;
}
// G1 GC监控
public G1Metrics collectMetrics() {
List<GarbageCollectorMXBean> gcBeans =
ManagementFactory.getGarbageCollectorMXBeans();
for (GarbageCollectorMXBean gcBean : gcBeans) {
if (gcBean.getName().contains("G1")) {
return G1Metrics.builder()
.youngGenCount(gcBean.getCollectionCount())
.youngGenTime(gcBean.getCollectionTime())
.oldGenCount(getOldGenCollections(gcBean))
.oldGenTime(getOldGenTime(gcBean))
.build();
}
}
return null;
}
}
ZGC垃圾回收器
ZGC是面向低延迟的垃圾回收器,暂停时间不超过10毫秒。
// ZGC配置
@Component
public class ZGCTuner {
// ZGC调优参数
public GCConfig getZGCConfig(ApplicationProfile profile) {
GCConfig config = new GCConfig();
config.add("-XX:+UseZGC");
config.add("-XX:+ZGenerational"); // 分代ZGC
switch (profile) {
case LOW_LATENCY:
config.add("-XX:SoftMaxHeapSize=8G");
config.add("-XX:ZUncommitDelay=300");
config.add("-XX:ConcGCThreads=4");
break;
case HIGH_MEMORY:
config.add("-XX:SoftMaxHeapSize=16G");
config.add("-XX:ZUncommitDelay=600");
config.add("-XX:ConcGCThreads=8");
break;
}
return config;
}
// ZGC特殊监控
public ZGCStats collectZGCStats() {
// ZGC特有的指标
MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean();
MemoryUsage heapUsage = memoryBean.getHeapMemoryUsage();
return ZGCStats.builder()
.heapUsed(heapUsage.getUsed())
.heapCommitted(heapUsage.getCommitted())
.heapMax(heapUsage.getMax())
.maxPauseTime(getMaxPauseTime())
.gcCycles(getGCCycles())
.build();
}
private long getMaxPauseTime() {
// ZGC暂停时间通常在10ms以内
// 通过GC日志或JFR获取精确值
return 0; // 实现略
}
}
Shenandoah垃圾回收器
Shenandoah是Red Hat开发的低延迟GC,与ZGC类似但实现方式不同。
// Shenandoah配置
@Component
public class ShenandoahTuner {
public GCConfig getShenandoahConfig(ApplicationProfile profile) {
GCConfig config = new GCConfig();
config.add("-XX:+UseShenandoahGC");
switch (profile) {
case LOW_LATENCY:
config.add("-XX:ShenandoahGCHeuristics=compact");
config.add("-XX:ShenandoahMinFreeThreshold=10");
config.add("-XX:ShenandoahGuaranteedGCInterval=300000");
break;
case BALANCED:
config.add("-XX:ShenandoahGCHeuristics=adaptive");
config.add("-XX:ShenandoahMinFreeThreshold=5");
config.add("-XX:ShenandoahGuaranteedGCInterval=600000");
break;
case THROUGHPUT:
config.add("-XX:ShenandoahGCHeuristics=aggressive");
config.add("-XX:ShenandoahMinFreeThreshold=20");
config.add("-XX:ShenandoahGuaranteedGCInterval=1200000");
break;
}
return config;
}
}
GC对比与选择
# GC对比分析
gc_comparison:
g1:
name: "G1 (Garbage-First)"
max_pause: "100-500ms"
throughput: "高"
memory_overhead: "中等"
best_for: "通用场景,大堆内存(>8GB)"
pros:
- 成熟稳定
- 可预测的暂停时间
- 适合大多数应用
cons:
- 暂停时间相对较长
- 内存开销较大
zgc:
name: "ZGC"
max_pause: "<10ms"
throughput: "高"
memory_overhead: "较高"
best_for: "超低延迟,大堆内存(>8GB)"
pros:
- 极低的暂停时间
- 可扩展性好
- 适合对延迟敏感的应用
cons:
- 内存开销较大
- 需要JDK 11+
shenandoah:
name: "Shenandoah"
max_pause: "<10ms"
throughput: "中等"
memory_overhead: "中等"
best_for: "低延迟,中等堆内存(4-16GB)"
pros:
- 低暂停时间
- 内存开销适中
- 适合响应式应用
cons:
- 吞吐量可能略低
- 需要JDK 12+
# 选择建议
selection_guide:
- scenario: "Web应用,堆内存<4G"
recommendation: "G1 GC"
reason: "G1在小堆内存下表现良好"
- scenario: "微服务,低延迟要求"
recommendation: "ZGC或Shenandoah"
reason: "两者都能提供亚毫秒级暂停"
- scenario: "大数据处理,大堆内存"
recommendation: "G1 GC"
reason: "G1在大堆内存下吞吐量高"
- scenario: "金融交易系统"
recommendation: "ZGC"
reason: "ZGC提供最稳定的低延迟"
GC调优工具
// GC调优工具
@Component
public class GCTuningTools {
// GC日志分析
public GCLogAnalysis analyzeGCLog(String logPath) throws IOException {
GCLogParser parser = new GCLogParser();
List<GCEvent> events = parser.parse(logPath);
return GCLogAnalysis.builder()
.totalGCEvents(events.size())
.totalGCTime(events.stream().mapToLong(GCEvent::getDuration).sum())
.avgPauseTime(events.stream()
.mapToLong(GCEvent::getPauseTime)
.average().orElse(0))
.maxPauseTime(events.stream()
.mapToLong(GCEvent::getPauseTime)
.max().orElse(0))
.gcFrequency(calculateFrequency(events))
.build();
}
// JFR(Java Flight Recorder)分析
public JFRAnalysis analyzeJFR(String jfrPath) throws IOException {
try (RecordingFile file = new RecordingFile(Path.of(jfrPath))) {
List<RecordedEvent> events = new ArrayList<>();
while (file.hasMoreEvents()) {
events.add(file.readEvent());
}
return analyzeEvents(events);
}
}
// 生成调优建议
public List<TuningSuggestion> generateSuggestions(GCAnalysis analysis) {
List<TuningSuggestion> suggestions = new ArrayList<>();
if (analysis.getMaxPauseTime() > 200) {
suggestions.add(TuningSuggestion.builder()
.issue("最大GC暂停时间过长")
.suggestion("考虑使用ZGC或Shenandoah,或调整G1的MaxGCPauseMillis")
.priority("HIGH")
.build());
}
if (analysis.getGCFrequency() > 100) {
suggestions.add(TuningSuggestion.builder()
.issue("GC频率过高")
.suggestion("增加堆内存大小,或调整InitiatingHeapOccupancyPercent")
.priority("MEDIUM")
.build());
}
if (analysis.getThroughput() < 95) {
suggestions.add(TuningSuggestion.builder()
.issue("GC吞吐量不足")
.suggestion("调整GC参数,或考虑使用更适合的GC算法")
.priority("HIGH")
.build());
}
return suggestions;
}
}
GC调优需要根据应用场景、堆内存大小和延迟要求选择合适的垃圾回收器,并通过监控和分析持续优化。