代码内容:
public class EdenDemo {
private static final int _1MB = 1024 * 1024;
/**
* vm arguments:-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();
}
}
JVM参数如下:
-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
为什么PSYoungGen区域的大小是9216k(9M) 而不是10M呢? 我明明已经设置"-Xmn 10M"
如上面的GC日志
... eden space 8192K, 77% used ...
... from space 1024K, 96% used ...
... to space 1024K, 0% used ...
Young space -Xmn is a sum of eden, S0, S1 (from and to are same as S0, S1, though S0 have from role for half of collections and to role for others, same for S1).
eden + from + to = 10MiB as expected.
Though, due to logic of young collection, to space should have zero utilization at all times, so effective capacity of young space is eden + from thus 9 MiB.
In general, though, some GC variant can dynamically adjust young space size (only reduce it compared to -Xmn) so effective young space at runtime could be smaller than configured.
-Xmn 年轻代的大小是由eden,S0(from), S1(to) 三部分组成, eden + from + to = 10M 是符合你设置的JVM参数的(-Xmn10M) -Xmn控制的是新生代的总容量,由于垃圾回收算法的原因,需要预留一半的survivor区容量帮助GC,所以同一时间新生代的可用空间等于eden区+50%Survivor区,但是如果使用G1垃圾回收算法的话就不是这样了,G1因为回收策略的调整,可以在同一时间真正使用100%的新生代容量
来源:stackoverflow
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。