jvm系列java
垃圾回收基础算法
JVM的编译策略shell
GC的三大基础算法segmentfault
GC的三大高级算法数据结构
GC策略的评价指标app
JVM信息查看jvm
GC通用日志解读测试
Java类初始化顺序spa
本文演示survivor空间溢出的场景。
class MemoryObject{ private byte[] bytes; public MemoryObject(int objectSize){ this.bytes = new byte[objectSize]; } } /** * eden 13184K ,s0/s1 1600k 共16M * old 24m -Xms40m -Xmx40m -Xmn16m -verbose:gc -XX:+PrintGCDetails -XX:+PrintHeapAtGC -XX:+PrintTenuringDistribution * @throws InterruptedException */ public static void survivorNotEnough2OldSpace() throws InterruptedException { // Thread.sleep(50000); MemoryObject object = new MemoryObject(1024*1024); MemoryObject m2Object = new MemoryObject(1024*1024*2); happenMinorGC(9); Thread.sleep(2000); } private static void happenMinorGC(int happenMinorGCIndex) throws InterruptedException { for(int i=0;i<happenMinorGCIndex;i++){ if(i == happenMinorGCIndex - 1){ // Thread.sleep(2000); System.out.println("minor gc should happen:"+printCurrent()); } new MemoryObject(1024*1024); System.out.println("allocate last:"+printCurrent()); } }
allocate last:2014-11-04 15-43-18-829 allocate last:2014-11-04 15-43-18-830 allocate last:2014-11-04 15-43-18-830 allocate last:2014-11-04 15-43-18-831 allocate last:2014-11-04 15-43-18-831 allocate last:2014-11-04 15-43-18-832 allocate last:2014-11-04 15-43-18-833 {Heap before GC invocations=0 (full 0): def new generation total 14784K, used 13182K [0x00000007f8600000, 0x00000007f9600000, 0x00000007f9600000) eden space 13184K, 99% used [0x00000007f8600000, 0x00000007f92df8b0, 0x00000007f92e0000) from space 1600K, 0% used [0x00000007f92e0000, 0x00000007f92e0000, 0x00000007f9470000) to space 1600K, 0% used [0x00000007f9470000, 0x00000007f9470000, 0x00000007f9600000) tenured generation total 24576K, used 0K [0x00000007f9600000, 0x00000007fae00000, 0x00000007fae00000) the space 24576K, 0% used [0x00000007f9600000, 0x00000007f9600000, 0x00000007f9600200, 0x00000007fae00000) compacting perm gen total 21248K, used 3994K [0x00000007fae00000, 0x00000007fc2c0000, 0x0000000800000000) the space 21248K, 18% used [0x00000007fae00000, 0x00000007fb1e6950, 0x00000007fb1e6a00, 0x00000007fc2c0000) No shared spaces configured. [GC [DefNew Desired survivor size 819200 bytes, new threshold 1 (max 15) - age 1: 1638400 bytes(1600K), 1638400 total 这个状况是survivor空间溢出,survivor空间被占满,而后溢出的部分,直接放到了年老代(2401K),溢出以后,计算survivor空间里头对象的年龄分布,发现年龄为1的对象大小总和超过了survivor的desired值,因而设置新的阈值为该age=1。 (每次young gc以后打印survivor区域内对象的年龄分布) 若是底下age的total大小大于Desired survivor size的大小,那么就表明了survivor空间溢出了,被填满,而后会从新计算threshold。 : 13182K->1600K(14784K), 0.0057500 secs] 13182K->4001K(39360K), 0.0057720 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] Heap after GC invocations=1 (full 0): def new generation total 14784K, used 1600K [0x00000007f8600000, 0x00000007f9600000, 0x00000007f9600000) eden space 13184K, 0% used [0x00000007f8600000, 0x00000007f8600000, 0x00000007f92e0000) from space 1600K, 100% used [0x00000007f9470000, 0x00000007f9600000, 0x00000007f9600000) to space 1600K, 0% used [0x00000007f92e0000, 0x00000007f92e0000, 0x00000007f9470000) tenured generation total 24576K, used 2401K [0x00000007f9600000, 0x00000007fae00000, 0x00000007fae00000) the space 24576K, 9% used [0x00000007f9600000, 0x00000007f98586d8, 0x00000007f9858800, 0x00000007fae00000) compacting perm gen total 21248K, used 3994K [0x00000007fae00000, 0x00000007fc2c0000, 0x0000000800000000) the space 21248K, 18% used [0x00000007fae00000, 0x00000007fb1e6950, 0x00000007fb1e6a00, 0x00000007fc2c0000) No shared spaces configured. } allocate last:2014-11-04 15-43-18-839 minor gc should happen:2014-11-04 15-43-18-839 allocate last:2014-11-04 15-43-18-839 Heap def new generation total 14784K, used 4202K [0x00000007f8600000, 0x00000007f9600000, 0x00000007f9600000) eden space 13184K, 19% used [0x00000007f8600000, 0x00000007f888a820, 0x00000007f92e0000) from space 1600K, 100% used [0x00000007f9470000, 0x00000007f9600000, 0x00000007f9600000) to space 1600K, 0% used [0x00000007f92e0000, 0x00000007f92e0000, 0x00000007f9470000) tenured generation total 24576K, used 2401K [0x00000007f9600000, 0x00000007fae00000, 0x00000007fae00000) the space 24576K, 9% used [0x00000007f9600000, 0x00000007f98586d8, 0x00000007f9858800, 0x00000007fae00000) compacting perm gen total 21248K, used 4001K [0x00000007fae00000, 0x00000007fc2c0000, 0x0000000800000000) the space 21248K, 18% used [0x00000007fae00000, 0x00000007fb1e8710, 0x00000007fb1e8800, 0x00000007fc2c0000) No shared spaces configured.
并非都得等到对象年龄达到晋升阈值才提高到年老代
若是在Survivor空间中相同年龄全部对象大小的总和>Survivor空间的一半( -XX:TargetSurvivorRatio)时,年龄>=该年龄的对象就能够直接进入年老代
Desired survivor size = (survivor_capacity TargetSurvivorRatio) / 100 sizeof(a pointer):survivor_capacity(一个survivor space的大小)乘以TargetSurvivorRatio
目标存活率,默认为50%
代表全部age的survivor space对象的大小若是超过Desired survivor size,则从新计算threshold,以age和MaxTenuringThreshold的最小值为准,不然以MaxTenuringThreshold为准.
目标Survivor空间占用是HotSpot尝试在MinorGC以后仍然维持的Survivor空间占用,默认值为50是由于HotSpot研发团队对不一样类型的应用程序进行了大量的负荷测试,结果代表50%的目标Survivor空间占用能适应大多数应用程序,能应对MinorGC时存活对象的急速增长。