Search code examples
javajava-8garbage-collectionjvm-hotspot

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


I have a simple demo to check the details of JVM memory allocation & deallocation.

The Java Version

$ 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)

The Demo

/**
 * 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];
    }
}

I manually used the CLI to compile and run the program as

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

The GC logs

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

My Question

  1. -Xms20M -Xmx20M -Xmn10M -XX:SurvivorRatio=8 already set and eden: 8M, from: 1M, and to: 1M presented clearly, why PSYoungGen total 9216K instead of 10240K?
  2. why Metaspace taking so much memory Metaspace used 2468K? what's in there exactly? Are there only type information?

Solution

  • As for the first question, @Holger has provided quite direct experiment to demonstrate the 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.

    Then when it comes to the second

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

    I found its specification in Java Platform, Standard Edition HotSpot Virtual Machine Garbage Collection Tuning Guide as

    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.