Open In App
Related Articles

MetaSpace in Java 8 with Examples

Improve Article
Save Article
Like Article

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

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: 

  1. 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.
  2. 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.


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 30 Sep, 2021
Like Article
Save Article
Similar Reads
Complete Tutorials