JVM memory has the following regions:
- a. Young Generation
- b. Old Generation
- c. Metaspace
- d. Others region
When you encounter ‘java.lang.OutOfMemoryError: Metaspace’, it indicates that the Metaspace region in the JVM memory is getting saturated. Metaspace is the region where metadata details that are required to execute your application are stored. In a nutshell, it contains class definitions, method definitions, and other metadata of your application.
The java.lang.OutOfMemoryError: Metaspace indicates that the amount of native memory allocated for Java class metadata is exausted.
In general terms, the more classes you are loading into the JVM, the more memory will be consumed by the metaspace. In Java 8 and later, the maximum amount of memory allocated for Java classes (MaxMetaspaceSize) is by default unlimited, so in most cases there is no need to change this setting.
-XX:MaxMetaspaceSize=3200m
In order to find the Root cause, its recommended that you follow these steps:
- Understand how the JVM manages the MetaSpace data
- Learn how to monitor the MetaSpace in a running Java application, there are tools available to monitor that like visualGC
- Check the Heap dump of your application to find offending Classes
For any OOM exception it is necessary to analize the Heap dump.