MetaSpace in Java 8

MetaSpace in Java 8

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.
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 heap area where all the java objects are stored.
The heap is created when the JVM starts. 

The heap is generally divided into two parts. 
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.
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. 

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. 
The garbage collection is automatically triggered when the class metadata usage reaches its maximum metaspace size. 


In Java 8, Metaspace is a memory region used to store class metadata, replacing the older PermGen space. It resides in native memory (outside the heap) and grows dynamically by default.

What Is Metaspace?
Metaspace was introduced in Java8 to overcome the limitations of PermGen (Permanent Generation). 
PermGen had a fixed size and was prone to `OutOfMemoryError` when class metadata exceeded its bounds. 
Metaspace solves this by:
- Allocating memory from native OS memory, not the JVM heap.
- Growing automatically as needed, unless explicitly capped.
This change improves garbage collection efficiency and reduces memory tuning complexity.

Key Differences: PermGen vs Metaspace
Feature | PermGen (Java ≤7) | Metaspace (Java ≥8) |
Location | Inside JVM heap | Native memory (outside heap)
Default Size | Fixed (64MB/82MB) | Unlimited (bounded by system memory)
Tuning Options | `-XX:PermSize`, `-XX:MaxPermSize` | `-XX:MetaspaceSize`, `-XX:MaxMetaspaceSize`
GC Behavior | Less efficient | Improved class unloading and GC
Error Type | `OutOfMemoryError: PermGen` | `OutOfMemoryError: Metaspace`

Sources
JVM Tuning Flags for Metaspace
You can control Metaspace behavior using these JVM options:
- `-XX:MetaspaceSize=<size>` – Initial size before GC kicks in.
- `-XX:MaxMetaspaceSize=<size>` – Maximum size to prevent overgrowth.
- `-XX:MinMetaspaceFreeRatio` and `-XX:MaxMetaspaceFreeRatio` – Control GC thresholds for class metadata.

Practical Implications
- Class-heavy applications (e.g., using frameworks like Spring, Struts) may benefit from tuning Metaspace to avoid memory spikes.
- Containerized deployments (e.g., Docker, Kubernetes) should set `MaxMetaspaceSize` to avoid exhausting host memory.

> Would you like a hands-on simulation of how Metaspace behaves under class loading stress or how to tune it for Spring Boot microservices?

Comments

Popular posts from this blog

Java 25 Features

Java Version Features

Java 8 Programs