1

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?
Holger
  • 285,553
  • 42
  • 434
  • 765
Hearen
  • 7,420
  • 4
  • 53
  • 63
  • Didn't I see this question about two or three hours ago? – KevinO Mar 15 '19 at 04:51
  • Haha, yeah... but it's different in some aspects while that question already marked **solved**. This question now is focusing on the **unanswered** parts. – Hearen Mar 15 '19 at 04:52
  • The discrepancy in the reported sizes has been noticed before and it was even worse in the past, see [here](https://stackoverflow.com/q/47192751/2711488) and [here](https://stackoverflow.com/q/43798527/2711488)… – Holger Mar 15 '19 at 09:11
  • @holger Wow, so crazy! Thank you so much for your **past** experiments which explained the first question perfectly. https://stackoverflow.com/a/47204548/2361308 – Hearen Mar 15 '19 at 09:22
  • The first question is answered [here](https://stackoverflow.com/a/27012218/3448419), the second - [here](https://stackoverflow.com/q/53678418/3448419) and [here](https://stackoverflow.com/q/54250638/3448419). – apangin Mar 22 '19 at 00:28

1 Answers1

1

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.

Hearen
  • 7,420
  • 4
  • 53
  • 63