Video in progress
What is Metaspace, and why did it replace PermGen in Java 8?
Metaspace is the native memory area where class metadata (class structure, methods, bytecode) is stored. PermGen was a fixed-size area inside the heap and often filled up; Metaspace grows by default.
📖 Answer
Up to Java 7, class data lived in PermGen (the Permanent Generation) inside the heap. Its size was fixed with -XX:MaxPermSize, and in applications that load many classes (repeated deploys to Tomcat, frameworks that create many proxies) it produced the famous OutOfMemoryError: PermGen space.
PermGen was removed in Java 8. Class metadata moved to native memory outside the heap, to Metaspace. Interned strings and static fields had already moved to the heap earlier, in Java 7.
Metaspace is unbounded by default and grows as far as the OS allows. That is why limiting it is useful in a container:
java -XX:MaxMetaspaceSize=256m -jar app.jar
If the limit is exceeded you get OutOfMemoryError: Metaspace. This usually signals a ClassLoader leak or too many dynamically generated classes.
Sign up to mark studied questions and save your progress.