OutOfMemoryError: Metaspace Solved




Contents of page >




1) Exception in thread threadName - java.lang.OutOfMemoryError: Metaspace in java
java.lang.OutOfMemoryError:Metaspace is thrown when there is no space to allocate metaspace for java class metadata.


2) What is Java class metadata?
Java class metadata is the JVM’s (Java virtual machine) internal presentation of Java class.


3) What is metaspace?
Java class metadata is allocated in native memory called metaspace.


4) When OutOfMemoryError: Metaspace is thrown in java?
OutOfMemoryError: Metaspace is thrown when -
  • Too many class are loaded or
  • Classes loaded very huge in size.
In this case there is no space to allocate metaspace for java class metadata.


5) How Metaspace to be allocated is decided in java?
Metaspace available for java class metadata is limited by VM (JVM) argument -XX:MaxMetaspaceSize,


6) Example of using MaxMetaSpaceSize in java >
-XX:MaxMetaspaceSize=64m


Whenever this specified metaspace becomes full and there is no further space to allocate metaspace for java class metadata OutOfMemoryError: Metaspace is thrown.


7) How to avoid OutOfMemoryError - Metaspace in java?
You may increase the value of metaspace by passing the above VM argument (-XX:MaxMetaspaceSize).


8) Tradeoff between java heap and MetaSpace in java >
MetaSpace is allocated from the same address spaces as the Java heap. Reducing the size of the Java heap will make more space available for MetaSpace. This is only a correct trade-off if there is an excess of free space in the Java heap.


9) Example/program in java to generate OutOfMemoryError: Metaspace >


Before executing program pass this vm parameter in eclipse.
-XX:MaxMetaspaceSize=5m  (So that we could easily produce OutOfMemoryError: Metaspace)


We will use javassist.ClassPool to create new classes.
import javassist.ClassPool;
/**
*
* Write a program which could throw
* java.lang.OutOfMemoryError : Metaspace
*
*/
public class OutOfMemoryErrorMetaspace {
   //ClassPool objects hold all the CtClasses.
   static ClassPool classPool = ClassPool.getDefault();
   public static void main(String[] args) throws Exception {
          for (int i = 0; i < 100000; i++) {
                 //makeClass method - Creates a new class (or interface) from the given class file.
                 Class clas = classPool.makeClass(
                              i + " outofmemory.OutOfMemoryErrorMetaspace ").toClass();
                 //Print name of class loaded
                 System.out.println(clas.getName());
          }
   }
}
Download Jar used in the program javassist.jar


OUTPUT of program >
0 outofmemory.OutOfMemoryErrorMetaspace
1 outofmemory.OutOfMemoryErrorMetaspace
2 outofmemory.OutOfMemoryErrorMetaspace
3 outofmemory.OutOfMemoryErrorMetaspace
4 outofmemory.OutOfMemoryErrorMetaspace
5 outofmemory.OutOfMemoryErrorMetaspace
6 outofmemory.OutOfMemoryErrorMetaspace
7 outofmemory.OutOfMemoryErrorMetaspace
8 outofmemory.OutOfMemoryErrorMetaspace
9 outofmemory.OutOfMemoryErrorMetaspace
10 outofmemory.OutOfMemoryErrorMetaspace
11 outofmemory.OutOfMemoryErrorMetaspace
12 outofmemory.OutOfMemoryErrorMetaspace
13 outofmemory.OutOfMemoryErrorMetaspace
Exception in thread "main" java.lang.OutOfMemoryError: Metaspace
So, from output we can clearly see that only 13 classes were loaded and then OutOfMemoryError: Metaspace was thrown.




The solution is simply to increase the Xmx parameter to -XX:MaxMetaspaceSize=512m as we discussed above.


10) Summary -
So in this tutorial we learned OutOfMemoryError: Metaspace.


java.lang.OutOfMemoryError:Metaspace is thrown when there is no space to allocate metaspace for java class metadata.


OutOfMemoryError: Metaspace is thrown when -
  • Too many class are loaded or
  • Classes loaded very huge in size.


Java class metadata is the JVM’s (Java virtual machine) internal presentation of Java class.


Java class metadata is allocated in native memory called metaspace.


Metaspace available for java class metadata is limited by VM (JVM) argument -XX:MaxMetaspaceSize,


Example of using MaxMetaSpaceSize in java > -XX:MaxMetaspaceSize=64m


To avoid OutOfMemoryError - Metaspace in java > increase the value of metaspace by passing the above VM argument (-XX:MaxMetaspaceSize).



Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.


RELATED LINKS>


How to write java program to pass VM/JVM parameters through CMD



Most important and frequently used VM (JVM) PARAMETERS with examples in JVM Heap memory in java | Command Line arguments

How to monitor and analyze the garbage collection in 10 ways in java


Detecting and fixing memory leak in java

eEdit
Must read for you :