开发者社区> 问答> 正文

我测试了一些jvm参数来看GC日志,但是发现一些细节跟我预期的并不一样, 难道我理解有问题?

代码内容:

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"

展开
收起
步止 2019-11-13 17:30:14 1756 0
1 条回答
写回答
取消 提交回答
  • 阿里巴巴小二.

    如上面的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

    2019-11-13 17:53:21
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
PostgresChina2018_赖思超_PostgreSQL10_hash索引的WAL日志修改版final 立即下载
Kubernetes下日志实时采集、存储与计算实践 立即下载
日志数据采集与分析对接 立即下载