java-performance
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseJava 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
undefinedbash
undefinedHigh-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
undefinedProfiling Commands
性能剖析命令
bash
undefinedbash
undefinedThread 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>
undefinedJMH 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对比
| GC | Latency | Throughput | Heap Size |
|---|---|---|---|
| G1 | Medium | High | 4-32GB |
| ZGC | Very Low | Medium | 8GB-16TB |
| Shenandoah | Very Low | Medium | 8GB+ |
| Parallel | High | Very High | Any |
| GC | 延迟 | 吞吐量 | 堆大小 |
|---|---|---|---|
| G1 | 中等 | 高 | 4-32GB |
| ZGC | 极低 | 中等 | 8GB-16TB |
| Shenandoah | 极低 | 中等 | 8GB+ |
| Parallel | 高 | 极高 | 任意 |
Troubleshooting
故障排查
| Problem | Cause | Solution |
|---|---|---|
| GC thrashing | Heap too small | Increase heap |
| High latency | GC pauses | Switch to ZGC |
| Memory leak | Object retention | Heap dump + MAT |
| CPU spikes | Hot loops | Profile + optimize |
| 问题 | 原因 | 解决方案 |
|---|---|---|
| GC抖动 | 堆内存过小 | 增大堆内存 |
| 高延迟 | GC停顿 | 切换到ZGC |
| 内存泄漏 | 对象被持续持有 | 堆转储 + MAT分析 |
| CPU峰值 | 热点循环 | 性能剖析 + 优化 |
Usage
使用方式
Skill("java-performance")Skill("java-performance")