MetaSpace in Java 8 with Examples
Last Updated :
15 Jul, 2025
In every programming language, memory is a vital resource and is also scarce in nature. Hence it’s essential that the memory is managed thoroughly without any leaks. In this article, we will understand what metaspace is and how is it different from permgen.
Before understanding the metaspace, lets first understand the JVM memory structure.
JVM Memory Structure:
JVM defines various run-time data area which are used during execution of a program. Some of the areas are created by the JVM whereas some are created by the threads that are used in a program. However, the memory area created by JVM is destroyed only when the JVM exits. The data areas of thread are created during instantiation and destroyed when the thread exits. JVM Memory Structure is divided into multiple memory area like heap area, stack area, method area, PC Registers etc. The following image illustrates the different memory areas in Java:
JVM Memory area parts
Here, the heap area is one of the most important memory areas of JVM. Here, all the java objects are stored. The heap is created when the JVM starts. The heap is generally divided into two parts. That is:
- Young Generation(Nursery): All the new objects are allocated in this memory. Whenever this memory gets filled, the garbage collection is performed. This is called as Minor Garbage Collection.
- Old Generation: All the long lived objects which have survived many rounds of minor garbage collection is stored in this area. Whenever this memory gets filled, the garbage collection is performed. This is called as Major Garbage Collection.

Apart from the heap memory, JVM also contains another type of memory which is called as Permanent Generation or "PermGen".

PermGen Memory: This is a special space in java heap which is separated from the main memory where all the static content is stored in this section. Apart from that, this memory also stores the application metadata required by the JVM. Metadata is a data which is used to describe the data. Here, garbage collection also happens like any other part of the memory. String pool was also part of this memory before Java 7. Method Area is a part of space in the PermGen and it is used to store the class structure and the code for methods and constructors. The biggest disadvantage of PermGen is that it contains a limited size which leads to an OutOfMemoryError. The default size of PermGen memory is 64 MB on 32-bit JVM and 82 MB on the 64-bit version. Due to this, JVM had to change the size of this memory by frequently performing Garbage collection which is a costly operation. Java also allows to manually change the size of the PermGen memory. However, the PermGen space cannot be made to auto increase. So, it is difficult to tune it. And also, the garbage collector is not efficient enough to clean the memory.
Due to the above problems, PermGen has been completely removed in Java 8. In the place of PermGen, a new feature called Meta Space has been introduced. MetaSpace grows automatically by default. Here, the garbage collection is automatically triggered when the class metadata usage reaches its maximum metaspace size.
The following table describes the difference between metaspace and PermGen:
PermGen | MetaSpace |
---|
It is removed from java 8. | It is introduced in Java 8. |
PermGen always has a fixed maximum size. | Metaspace by default auto increases its size depending on the underlying OS. |
Contiguous Java Heap Memory. | Native Memory(provided by underlying OS). |
Inefficient garbage collection. | Efficient garbage collection. |
Similar Reads
javap tool in Java with Examples javap tool The javap tool is used to get the information of any class or interface. The javap command (also known as the Java Disassembler) disassembles one or more class files. Its output depends on the options used (â-câ or â-verboseâ for byte code and byte code along with innards info, respective
2 min read
Java Native Interface with Example In the Programming World, it is often seen in the comparison of Java vs C/C++. Although the comparison doesn't make any such sense, as both the languages have their Pros and Cons, but what if we can use multiple languages in a single Program?In this article, we will learn How to use JNI(Java Native
4 min read
Class getMethod() method in Java with Examples The getMethod() method of java.lang.Class class is used to get the specified method of this class with the specified parameter type, which is the method that is public and its members. The method returns the specified method of this class in the form of Method objects. Syntax: public Method getMetho
2 min read
Class getMethod() method in Java with Examples The getMethod() method of java.lang.Class class is used to get the specified method of this class with the specified parameter type, which is the method that is public and its members. The method returns the specified method of this class in the form of Method objects. Syntax: public Method getMetho
2 min read
Class getModule() method in Java with Examples The getModule() method of java.lang.Class class is used to get the module of this entity. This entity can be a class, an array, an interface, etc. The method returns the module of the entity.Syntax:Â Â public Module getModule() Parameter: This method does not accept any parameter.Return Value: This m
2 min read
Class getModule() method in Java with Examples The getModule() method of java.lang.Class class is used to get the module of this entity. This entity can be a class, an array, an interface, etc. The method returns the module of the entity.Syntax:Â Â public Module getModule() Parameter: This method does not accept any parameter.Return Value: This m
2 min read