最近在做JVM调优时发现个问题,感觉比较有趣,特记录下:
JVM参数配置:
-Xms2048m -Xmx2048m -XX:PermSize=256m -XX:MaxPermSize=512m -XX:+UseConcMarkSweepGC -XX:+PrintGCDetails -XX:+PrintTenuringDistribution
GC日志为
12083.607: [GC12083.607: [ParNew Desired survivor size 35782656 bytes, new threshold 6 (max 6)
- age 1: 186216 bytes, 186216 total
- age 2: 2328 bytes, 188544 total
- age 3: 21888 bytes, 210432 total
- age 4: 63880 bytes, 274312 total
- age 5: 63280 bytes, 337592 total
- age 6: 10588168 bytes, 10925760 total
: 574751K->15677K(629120K), 0.0072360 secs] 603265K->44191K(2027264K), 0.0074590 secs] [Times: user=0.12 sys=0.00, real=0.00 secs]
我们都知道默认情况下
NewRatio=2,NewRatio=Old/Yong=Old/(Eden+Survivor*2)
SurvivorRatio=8,SurvivorRatio=Eden/Survivor
TargetSurvivorRatio=0.5
根据上面的配置,Survivor的大小为 204810241024/3/10bytes=71582789,而日志中的 Desired survivor size(DSS)=SurvivorTargetSurvivorRatio=35782656,可以推算出Survivor=357826562=71565312,可以看到两个结果存在差异,那么原因在哪呢?
查看JVM的源码发现Survivor大小是这么计算的:
size_t compute_survivor_size(size_t gen_size, size_t alignment) const {
size_t n = gen_size / (SurvivorRatio + 2);
return n > alignment ? align_size_down(n, alignment) : alignment;
}
#define align_size_down_(size, alignment) ((size) & ~((alignment) - 1))
enum SomePublicConstants {
// Generations are GenGrain-aligned and have size that are multiples of
// GenGrain.
// Note: on ARM we add 1 bit for card_table_base to be properly aligned
// (we expect its low byte to be zero - see implementation of post_barrier)
LogOfGenGrain = 16 ARM_ONLY(+1),
GenGrain = 1 << LogOfGenGrain
};
原来计算survivor大小时,需要进行对齐操作;
71582789&~((1<<16)-1)=71565312