最近在看《深入理解java虚拟机》这本书,书中关于GC的一段代码让我产生了点疑惑,直接上代码
`public class EdenDemo {
private static final int _1MB = 1024 * 1024;
/**
* vm参数:-verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8
*/
public static void testAllocation(){
byte[] allocation1, allocation2, allocation3, allocation4;
allocation1 = new byte[2 * _1MB];
allocation2 = new byte[2 * _1MB];
allocation3 = new byte[2 * _1MB];
allocation4 = new byte[4 * _1MB];
}
public static void main(String[] args) {
testAllocation();
}
}`
vm参数如下
-verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8
运行后产生的gc日志如下:
`[GC (Allocation Failure) [PSYoungGen: 6794K->990K(9216K)] 6794K->5094K(19456K), 0.0041458 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap
PSYoungGen total 9216K, used 7372K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
eden space 8192K, 77% used [0x00000000ff600000,0x00000000ffc3b718,0x00000000ffe00000)
from space 1024K, 96% used [0x00000000ffe00000,0x00000000ffef7910,0x00000000fff00000)
to space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000)
ParOldGen total 10240K, used 4104K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
object space 10240K, 40% used [0x00000000fec00000,0x00000000ff002020,0x00000000ff600000)
Metaspace used 3244K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 350K, capacity 388K, committed 512K, reserved 1048576K
`
ok,我的问题是,在vm中通过-Xmn参数设置新生代大小是10M,我理解的新生代应该是:eden区+2个Survivor区。
按照8:1:1的比例分配的话,应该是eden区大小为8M,survivor(from)大小为1M,survivor(to)大小为1M
但是看gc日志,"[PSYoungGen: 6794K->990K(9216K)]",新生代大小是9216k,也就是9M?
看书里是这么说的
"新生代总可用空间为9216k(Eden区+1个Survivor区的总容量)"
求解惑
还是贴下stackoverflow的答案吧,解释的很清楚
https://stackoverflow.com/que...