Metaspace in Java

Metaspace is the JVM area used for class metadata and is allocated from native memory rather than the traditional Java heap. Its usage can grow as classes are loaded and is constrained by JVM configuration and available native memory.

Metaspace stores metadata associated with loaded Java classes, such as class structure information and method-related metadata. It replaced PermGen in Java 8, but it is not part of the Java heap.

Metaspace is allocated from native memory, so increasing heap size does not directly increase the space available for class metadata. The JVM can expand Metaspace as more classes are loaded, subject to configured limits and available native memory.

Class unloading can reclaim Metaspace occupied by classes that are no longer reachable through their class loaders. Class unloading is particularly important in applications that repeatedly create and discard class loaders, such as application servers, plugin systems, or systems using dynamic class generation.

A common diagnostic symptom of Metaspace pressure is `OutOfMemoryError: Metaspace`. Useful JVM options include `-XX:MaxMetaspaceSize` to impose a maximum and `-XX:MetaspaceSize` to influence the initial threshold that can trigger class-metadata-related GC activity. These settings should be interpreted separately from `-Xms` and `-Xmx`, which configure heap sizing.

Key points

Interview questions

What is Metaspace in Java?

Metaspace is the JVM memory area used for metadata describing loaded classes. Unlike ordinary Java objects, this metadata is stored outside the Java heap in native memory. It can grow as the application loads more classes. If the available or configured Metaspace is exhausted, the JVM can throw `OutOfMemoryError: Metaspace`.

How is Metaspace different from the Java heap?

The heap is primarily where Java application objects and arrays are allocated, while Metaspace holds metadata associated with loaded classes. Metaspace uses native memory rather than the Java heap. Therefore, changing `-Xmx` does not directly set the Metaspace limit. The two areas should be monitored and tuned independently.

What can cause Metaspace usage to keep increasing?

A growing number of loaded classes can naturally increase Metaspace usage. Persistent growth can also occur when applications repeatedly create class loaders whose classes cannot be unloaded because references to those class loaders remain reachable. Frameworks that generate classes dynamically can contribute as well. Diagnosis should therefore examine class-loading behavior and class-loader lifetimes rather than assuming the Java heap is responsible.

What is the difference between `MetaspaceSize` and `MaxMetaspaceSize`?

`-XX:MetaspaceSize` specifies an initial threshold associated with Metaspace sizing and can influence when garbage collection is triggered for class metadata. It is not simply a hard cap on Metaspace usage. `-XX:MaxMetaspaceSize` establishes an upper limit for Metaspace growth. Confusing these options can lead to incorrect JVM tuning.