JDK 8 Metaspace tuning for JBoss EAP
Environment
- Oracle JDK or OpenJDK
- 8
- Red Hat JBoss Enterprise Application Platform (EAP)
- 6.x
- 7.x
Issue
- Looking for guidelines on tuning the
metaspacein Java 8. Specifically looking for recommendations on theMaxMetaspaceSizeflag. - Where does
Metaspacestores the information? Is it in the heap?
Resolution
Disclaimer: Links contained herein to an external website(s) are provided for convenience only. Red Hat has not reviewed the links and is not responsible for the content or its availability. The inclusion of any link to an external website does not imply endorsement by Red Hat of the website or their entities, products or services. You agree that Red Hat is not responsible or liable for any loss or expenses that may result due to your use of (or reliance on) the external site or content.
- JDK 8 onwards, there is no
"Permanent generation space". It has been removed in JDK 8 and the options of settingpermgenwere removed as well. All the class metadata is now stored in thenative spacethat is called"Metaspace". As a consequence, to tune Metaspace, some flags were added for in JDK 8:
-XX:MetaspaceSize=<NNN> where <NNN> is the initial amount of space(the initial high-water-mark) allocated for class metadata (in bytes) that may induce a garbage collection to unload classes. The amount is approximate. After the high-water-mark is first reached, the next high-water-mark is managed by the garbage collector
-XX:MaxMetaspaceSize=<NNN> where <NNN> is the maximum amount of space to be allocated for class metadata (in bytes). This flag can be used to limit the amount of space allocated for class metadata. This value is approximate. By default, there is no limit set.
-XX:MinMetaspaceFreeRatio=<NNN> where <NNN> is the minimum percentage of class metadata capacity free after a GC to avoid an increase in the amount of space (high-water-mark) allocated for class metadata that will induce a garbage collection.
-XX:MaxMetaspaceFreeRatio=<NNN> where <NNN> is the maximum percentage of class metadata capacity free after a GC to avoid a reduction in the amount of space (high-water-mark) allocated for class metadata that will induce a garbage collection.
-
By default, there is no limit of the maximum metaspace size - it starts at the relatively small platform default, and grows as needed in small increments. While the permanent generation (
PermGen) was a special part of the Java heap, therefore the maximum size is configured by-Xmxoption, Metaspace is not part of Heap. It is rather part ofNative Memory. As a corollary, this meansNative Memorytools can track it as in: JVM Native Memory Tracking. -
Since only class metadata lives in metaspace, all the Java object parts are moved to the main Java heap, the metaspace requirements are much lower than the permanent generation size (but normal heap size use is increased). In general, you should not need to set a maximum size, unless you are experiencing large metaspace leaks and would prefer it to fail rather than keep increasing memory size.
-
Increasing the size of metaspace is also a much cheaper operation than resizing the permanent generation was, which involved a Full GC event and compaction, so the initial size matters a lot less too.
-
The optimal size of the metaspace is so highly dependant on the application. That, plus the fact that the JVM can increase it as large as needed, means let the JVM handle it, unless one is encountering problems related to metaspace usage or resizing events. Part of the reason for the change from permanent generation to metaspace was so that it would become something that very few users need to touch.
-
The
MaxMetaspaceflag limits thecommittedportion of the chunk of memory, it is a soft limit. So when seeing the GC logs, be attentive for thecommittedsection of the GC and not thereserved memory, which is likely higher than the MaxMetaspace. Specific details on Metaspace organization, how to interpret Metaspace logs and how to extract its values, see solution How does the JVM divide the Metaspace in the memory?. -
The flag
MetaSpaceSizedoes not set the initial size for the metaspace, which has zero as initial size. But instead it set a cap limit when the Full GC will trigger to clean the Metaspace, this value will be resized for a higher or lower value by the JVM. See Content from docs.oracle.com is not included.Oracle Docs:
XX:MetaspaceSize=size: Sets the size of the allocated class metadata space that will trigger a garbage collection the first time it is exceeded. This threshold for a garbage collection is increased or decreased depending on the amount of Metadata used. The default size depends on the platform.
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.