jvm系列
序
本文演示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
-XX:TargetSurvivorRatio
目标存活率,默认为50%
表明所有age的survivor space对象的大小如果超过Desired survivor size,则重新计算threshold,以age和MaxTenuringThreshold的最小值为准,否则以MaxTenuringThreshold为准.
-XX:TargetSurvivorRatio
目标Survivor空间占用是HotSpot尝试在MinorGC之后仍然维持的Survivor空间占用,默认值为50
是因为HotSpot研发团队对不同类型的应用程序进行了大量的负荷测试,结果表明50%的目标Survivor空间占用能适应大多数应用程序,能应对MinorGC时存活对象的急速增加。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。