Help us improve
Share bugs, ideas, or general feedback.
From java
Java性能优化规范 - JFR诊断、JMH基准测试、ZGC/G1GC垃圾回收调优、GraalVM Native Image、async-profiler火焰图分析。排查性能瓶颈、内存泄漏或优化吞吐延迟时加载。
npx claudepluginhub lazygophers/ccplugin --plugin javaHow this skill is triggered — by the user, by Claude, or both
Slash command
/java:performancesonnetThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
- **java:dev** - 性能感知的代码编写
JVM performance tuning - GC optimization, profiling, memory analysis, benchmarking
Scans Java code for performance issues including N+1 queries, unbounded data loads, string/memory inefficiencies, suboptimal collections, threading problems, and transaction scopes.
Enforces Java development conventions including naming rules, exception handling, Spring Boot best practices, DTO/VO Lombok usage, batch query limits, and N+1 query prevention.
Share bugs, ideas, or general feedback.
JFR 是 JDK 内置的生产级 profiling 工具,开销 < 1%。
# 启动时开启 JFR
java -XX:StartFlightRecording=duration=120s,filename=app.jfr \
-XX:FlightRecorderOptions=stackdepth=256 \
-jar app.jar
# 运行时开启 JFR
jcmd <pid> JFR.start duration=60s filename=recording.jfr
# 持续记录(生产推荐)
java -XX:StartFlightRecording=disk=true,maxsize=500m,maxage=1d \
-jar app.jar
# 分析 JFR 文件
jfr print --events jdk.CPULoad,jdk.GarbageCollection recording.jfr
jfr summary recording.jfr
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@State(Scope.Benchmark)
@Warmup(iterations = 3, time = 1)
@Measurement(iterations = 5, time = 1)
@Fork(2)
public class CollectionBenchmark {
private List<String> data;
@Setup
public void setup() {
data = IntStream.range(0, 10_000)
.mapToObj(i -> "item-" + i)
.toList();
}
@Benchmark
public long streamCount(Blackhole bh) {
return data.stream()
.filter(s -> s.contains("5"))
.count();
}
@Benchmark
public long parallelStreamCount(Blackhole bh) {
return data.parallelStream()
.filter(s -> s.contains("5"))
.count();
}
@Benchmark
public long forLoopCount(Blackhole bh) {
long count = 0;
for (String s : data) {
if (s.contains("5")) count++;
}
return count;
}
}
// build.gradle JMH 配置
plugins {
id 'me.champeau.jmh' version '0.7.2'
}
jmh {
warmupIterations = 3
iterations = 5
fork = 2
resultFormat = 'JSON'
}
# ZGC Generational(Java 21+ 默认分代模式)
java -XX:+UseZGC \
-XX:+ZGenerational \
-Xmx4g -Xms4g \
-jar app.jar
# ZGC 特性:
# - 最大停顿 < 1ms(亚毫秒级)
# - 堆大小 8MB ~ 16TB
# - 适合延迟敏感型应用
# G1GC(吞吐和延迟平衡)
java -XX:+UseG1GC \
-XX:MaxGCPauseMillis=200 \
-XX:G1HeapRegionSize=16m \
-XX:+G1UseAdaptiveIHOP \
-Xmx4g -Xms4g \
-jar app.jar
# 启用 GC 日志(统一日志框架 Java 9+)
java -Xlog:gc*:file=gc.log:time,uptime,level,tags:filecount=5,filesize=100m \
-jar app.jar
# 分析工具:GCViewer、GCEasy.io
# 编译 Native Image(Spring Boot 3+)
./gradlew nativeCompile
# 特性:
# - 启动时间 < 100ms(vs JVM ~2s)
# - 内存占用降低 50-80%
# - 适合 Serverless、CLI 工具
# CDS(Class Data Sharing)- 轻量替代方案
# 步骤 1: 创建共享归档
java -XX:ArchiveClassesAtExit=app.jsa -jar app.jar
# 步骤 2: 使用共享归档启动(启动提速 20-40%)
java -XX:SharedArchiveFile=app.jsa -jar app.jar
// 1. 集合初始化容量(避免 resize)
List<User> users = new ArrayList<>(expectedSize);
Map<String, User> map = HashMap.newHashMap(expectedSize); // Java 19+
// 2. 字符串优化
String result = String.join(",", list); // 替代循环拼接
String formatted = "name=%s, age=%d".formatted(name, age); // 替代 String.format
// 3. 避免装箱/拆箱
IntStream.range(0, 100).sum(); // 使用原始类型 Stream
OptionalInt max = IntStream.of(1, 2, 3).max(); // OptionalInt
// 4. Stream 性能
list.stream().toList(); // 返回不可变 List(Java 16+)
list.parallelStream() // 仅用于 CPU 密集 + 大数据集 (>10K)
// 5. 连接池配置(HikariCP)
// spring.datasource.hikari.maximum-pool-size=10
// spring.datasource.hikari.minimum-idle=5
// spring.datasource.hikari.idle-timeout=30000
// 6. JPA 批量操作(避免 N+1)
@Query("SELECT u FROM User u JOIN FETCH u.orders WHERE u.id IN :ids")
List<User> findAllWithOrdersByIds(@Param("ids") List<Long> ids);
// Micrometer 自定义指标
@Bean
public TimedAspect timedAspect(MeterRegistry registry) {
return new TimedAspect(registry);
}
@Timed(value = "user.query", percentiles = {0.5, 0.95, 0.99})
public Optional<User> findById(Long id) { ... }
// 关键指标(四大黄金指标)
// 1. 延迟:P50 / P95 / P99 / P999
// 2. 吞吐量:QPS / TPS
// 3. 错误率:4xx / 5xx 比例
// 4. 饱和度:CPU%、内存%、线程池使用率
# CPU 火焰图
./asprof -d 30 -f cpu.html <pid>
# 内存分配火焰图
./asprof -d 30 -e alloc -f alloc.html <pid>
# 锁竞争火焰图
./asprof -d 30 -e lock -f lock.html <pid>
# Wall clock(包含等待时间)
./asprof -d 30 -e wall -f wall.html <pid>
| AI 可能的理性化解释 | 实际应该检查的内容 |
|---|---|
| "凭经验优化就行" | 是否使用 JFR/async-profiler 数据驱动? |
| "这段代码看起来慢" | 是否确认是 profiling 热点? |
| "优化完了更快了" | 是否 JMH 统计验证了显著性? |
| "G1GC 够用了" | Java 21+ 是否评估了 ZGC Generational? |
| "JVM 默认参数就好" | 是否根据负载特征调优 GC 和堆大小? |
| "parallelStream 更快" | 数据量是否足够大(>10K)且是 CPU 密集? |
| "Native Image 不需要" | Serverless 场景是否评估了启动时间? |