Posted on

Understanding Java Metaspace

In Java, Metaspace is a part of the native memory where class metadata and class bytecode are stored. Unlike the permanent generation (PermGen) in older versions of Java, Metaspace is not a fixed size and can dynamically increase or decrease its size depending on the application’s needs. This tutorial will cover what Metaspace is, how it works, and how you can manage it effectively.

What is Metaspace?

Metaspace stores the following types of information:

  1. Class metadata: Information about classes and their respective methods, fields, annotations, etc.
  2. Class bytecode: The actual bytecode of loaded classes.

How Metaspace Works

Metaspace is managed by the JVM and is allocated from the native memory. Unlike the PermGen, which had a fixed size, Metaspace can dynamically resize itself based on the demand. When the Metaspace usage exceeds a certain threshold, the JVM can trigger a garbage collection event to reclaim unused space.

Managing Metaspace

You can manage Metaspace in Java by adjusting various JVM options. Here are some of the most commonly used settings:

  1. -XX:MaxMetaspaceSize: Sets the maximum size of Metaspace. If not specified, Metaspace can dynamically resize itself depending on the available native memory.Example: -XX:MaxMetaspaceSize=256m
  2. -XX:MetaspaceSize: Sets the initial size of Metaspace. This value should be chosen based on your application’s requirements.Example: -XX:MetaspaceSize=64m
  3. -XX:MinMetaspaceFreeRatio: Sets the minimum percentage of Metaspace free space after a garbage collection event. If the free space falls below this ratio, Metaspace will be expanded.Example: -XX:MinMetaspaceFreeRatio=20
  4. -XX:MaxMetaspaceFreeRatio: Sets the maximum percentage of Metaspace free space after a garbage collection event. If the free space exceeds this ratio, Metaspace will be contracted.Example: -XX:MaxMetaspaceFreeRatio=50
  5. -XX:MetaspaceSizeIncrement: Sets the amount by which Metaspace will be expanded if more space is needed.Example: -XX:MetaspaceSizeIncrement=16m

Monitoring Metaspace

You can monitor Metaspace usage and performance using various tools like JConsole, VisualVM, or by enabling JVM flags for detailed logging.

  1. -XX:+PrintGCDetails: Prints detailed information about garbage collection events, including Metaspace usage.
  2. -XX:+PrintGCTimeStamps: Prints timestamps for garbage collection events, useful for tracking Metaspace usage over time.
  3. -XX:+PrintMetaspace: Prints Metaspace-specific information during garbage collection events.

Conclusion

Metaspace in Java is a dynamic part of the native memory used for storing class metadata and bytecode. By understanding how Metaspace works and utilizing the available JVM options, you can effectively manage Metaspace to optimize memory usage and improve the performance of your Java applications.

To learn more about solving common issues like an out of memory in the Metaspace, check this article: How to solve out of memory issues in the Metaspace