java-performance

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Java Performance Skill

Java性能技能

Optimize JVM performance through profiling, GC tuning, and memory analysis.
通过性能剖析、GC调优和内存分析来优化JVM性能。

Overview

概述

This skill covers JVM performance optimization including garbage collection tuning, memory analysis, CPU profiling, and benchmarking with JMH.
本技能涵盖JVM性能优化相关内容,包括垃圾回收(GC)调优、内存分析、CPU性能剖析以及使用JMH进行基准测试。

When to Use This Skill

适用场景

Use when you need to:
  • Tune GC for low latency or throughput
  • Profile CPU hotspots
  • Analyze memory leaks
  • Benchmark code performance
  • Optimize container settings
当你需要以下操作时使用本技能:
  • 针对低延迟或吞吐量进行GC调优
  • 剖析CPU热点
  • 分析内存泄漏
  • 对代码性能进行基准测试
  • 优化容器配置

Quick Reference

快速参考

GC Presets

GC预设配置

bash
undefined
bash
undefined

High-throughput

高吞吐量

-XX:+UseG1GC -XX:MaxGCPauseMillis=200 -Xms4g -Xmx4g -XX:+AlwaysPreTouch
-XX:+UseG1GC -XX:MaxGCPauseMillis=200 -Xms4g -Xmx4g -XX:+AlwaysPreTouch

Low-latency

低延迟

-XX:+UseZGC -XX:+ZGenerational -Xms8g -Xmx8g
-XX:+UseZGC -XX:+ZGenerational -Xms8g -Xmx8g

Memory-constrained

内存受限场景

-XX:+UseSerialGC -Xms512m -Xmx512m -XX:+UseCompressedOops
-XX:+UseSerialGC -Xms512m -Xmx512m -XX:+UseCompressedOops

Container-optimized

容器优化配置

-XX:+UseContainerSupport -XX:MaxRAMPercentage=75.0 -XX:+ExitOnOutOfMemoryError
undefined
-XX:+UseContainerSupport -XX:MaxRAMPercentage=75.0 -XX:+ExitOnOutOfMemoryError
undefined

Profiling Commands

性能剖析命令

bash
undefined
bash
undefined

Thread dump

线程转储

jstack -l <pid> > threaddump.txt
jstack -l <pid> > threaddump.txt

Heap dump

堆转储

jmap -dump:format=b,file=heap.hprof <pid>
jmap -dump:format=b,file=heap.hprof <pid>

GC analysis

GC分析

jstat -gcutil <pid> 1000 10
jstat -gcutil <pid> 1000 10

Flight recording

飞行记录

jcmd <pid> JFR.start duration=60s filename=app.jfr
jcmd <pid> JFR.start duration=60s filename=app.jfr

Async profiler

异步性能剖析器

./profiler.sh -d 30 -f profile.html <pid>
undefined
./profiler.sh -d 30 -f profile.html <pid>
undefined

JMH Benchmark

JMH基准测试

java
@BenchmarkMode(Mode.Throughput)
@Warmup(iterations = 3, time = 1)
@Measurement(iterations = 5, time = 1)
@State(Scope.Benchmark)
public class MyBenchmark {

    @Benchmark
    public void testMethod(Blackhole bh) {
        bh.consume(compute());
    }
}
java
@BenchmarkMode(Mode.Throughput)
@Warmup(iterations = 3, time = 1)
@Measurement(iterations = 5, time = 1)
@State(Scope.Benchmark)
public class MyBenchmark {

    @Benchmark
    public void testMethod(Blackhole bh) {
        bh.consume(compute());
    }
}

GC Comparison

GC对比

GCLatencyThroughputHeap Size
G1MediumHigh4-32GB
ZGCVery LowMedium8GB-16TB
ShenandoahVery LowMedium8GB+
ParallelHighVery HighAny
GC延迟吞吐量堆大小
G1中等4-32GB
ZGC极低中等8GB-16TB
Shenandoah极低中等8GB+
Parallel极高任意

Troubleshooting

故障排查

ProblemCauseSolution
GC thrashingHeap too smallIncrease heap
High latencyGC pausesSwitch to ZGC
Memory leakObject retentionHeap dump + MAT
CPU spikesHot loopsProfile + optimize
问题原因解决方案
GC抖动堆内存过小增大堆内存
高延迟GC停顿切换到ZGC
内存泄漏对象被持续持有堆转储 + MAT分析
CPU峰值热点循环性能剖析 + 优化

Usage

使用方式

Skill("java-performance")
Skill("java-performance")