Metaspace replaced PermGen from Java 8 onwards.
PermGen had fixed memory allocation issues, whereas Metaspace uses native memory dynamically,
reducing
OutOfMemory problems related to class metadata storage.
OLD (Before Java 8)
PermGen (Permanent Generation)
Used in:
· Java 7
· Java 6
· older JVMs
It stored:
· class metadata
· method metadata
· static variables
· interned strings (older versions)
Problem:
· fixed size memory
· difficult tuning
· frequent:
java.lang.OutOfMemoryError:
PermGen space
NEW (Java 8 onwards)
Metaspace
From:
· Java 8+
· Java 11
· Java 17
· Java 21
Oracle REMOVED PermGen and replaced it with Metaspace.
Main Difference
|
PermGen |
Metaspace |
|
fixed JVM memory |
uses native OS memory |
|
easy OutOfMemory |
more scalable |
|
difficult tuning |
auto-expandable |
|
part of JVM heap |
outside heap |
|
Java 7 and below |
Java 8+ |
Before Java 8, class metadata was stored in PermGen.
From Java 8
onwards, PermGen was removed and replaced by Metaspace, which uses native
memory and scales dynamically.
In Java 7 and earlier, JVM used PermGen to store class metadata.
In Java 8, PermGen was removed and replaced by Metaspace.
Unlike
PermGen, Metaspace uses native memory outside the heap and can grow
dynamically, reducing OutOfMemory issues related to class metadata.


Heap vs Stack
|
Heap |
Stack |
|
Objects |
Method calls |
|
Shared |
Thread-specific |
|
GC managed |
Auto cleanup |
|
Bigger |
Smaller |

Garbage Collection
Purpose
Automatically removes unreachable objects from heap memory.
GC Types
|
Collector |
Usage |
|
Serial GC |
single thread |
|
Parallel GC |
throughput |
|
G1GC |
modern default |
|
ZGC |
low latency |
|
Shenandoah |
low pause |
Mark and Sweep Concept
1. Mark
reachable objects
2. Sweep unreachable objects
3. Compact memory





All objects are stored in Heap
Primitive values are stored in stack









You will see compiler error at c = new Customer(“Susan”); due to final variable.

But it is valid.







Even it is cleared, still it will return values
Because getCustomers() is creating new copy of the instance.


Record.getCustomers().clear() will throw error. Because we cannot modify



