Relation between memory host and memory arguments xms and xmx from Java

Solution 1:

Memory used by Java process (as seen by the OS) is not only limited to Java Heap. There are a lot more memory areas that should be also counted:

  • Metaspace (where class metadata resides);
  • Code Cache (storage for JIT-compiled methods and all the generated code);
  • Direct ByteBuffers;
  • Memory-mapped files, including files mapped by JVM, e.g. all JAR files on the classpath;
  • Thread stacks;
  • JVM code itself and all the dynamic libraries loaded by Java Runtime;
  • Other internal JVM structures.

Use NativeMemoryTracking JDK feature to get the detailed breakdown of memory areas used by JVM:

java -XX:NativeMemoryTracking=detail -XX:+UnlockDiagnosticVMOptions -XX:+PrintNMTStatistics