PSYoungGen不是"eden"、"from"和"to"的总和吗?

PSYoungGen is not the sum of "eden", "from" and "to"?

我有一个简单的演示来检查 JVM 内存分配和释放的细节。

Java版本

$ java -version
java version "1.8.0_201"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)

演示

/**
 * VM Options: -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8
 */
public class DefaultCollector {
    private static final int _1MB = 1024 * 1024;
    public static void main(String... args) {
        byte[] arr1 = new byte[2 * _1MB];
        byte[] arr2 = new byte[2 * _1MB];
        byte[] arr3 = new byte[2 * _1MB];
        byte[] arr4 = new byte[4 * _1MB];
    }
}

手动使用CLI编译运行程序

$ javac DefaultCollector.java 
$ java -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 DefaultCollector

GC 日志

Heap
 PSYoungGen      total 9216K, used 6979K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
  eden space 8192K, 85% used [0x00000000ff600000,0x00000000ffcd0f68,0x00000000ffe00000)
  from space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000)
  to   space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000)
 ParOldGen       total 10240K, used 4096K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
  object space 10240K, 40% used [0x00000000fec00000,0x00000000ff000010,0x00000000ff600000)
 Metaspace       used 2468K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 265K, capacity 386K, committed 512K, reserved 1048576K

我的问题

  1. -Xms20M -Xmx20M -Xmn10M -XX:SurvivorRatio=8已经设置好了,eden: 8M, from: 1M, and to: 1M也显示的很清楚,为什么PSYoungGentotal 9216K而不是10240K
  2. 为什么 Metaspace 占用这么多内存 Metaspace used 2468K?里面到底有什么?是否只有类型信息

对于第一个问题,@Holger 提供了非常直接的实验来证明fact

The total size reported for the Young Generation always includes the eden and from space only, ignoring the always-empty to space, which is inconsistent to the addresses reported behind the sizes, which include the complete span, covering all three spaces.

那么说到第二个

why Metaspace taking so much memory Metaspace used 2468K? what's in there exactly? Are there only type information?

我在 Java Platform, Standard Edition HotSpot Virtual Machine Garbage Collection Tuning Guide 中找到了它的规格,因为

In the line beginning with Metaspace, the used value is the amount of space used for loaded classes...The line beginning with class space line contains the corresponding values for the metadata for compressed class pointers.