JVM Memory Model
A deeper dive into the Heap and Stack. Understand Young Generation, Old Generation, and Metaspace.
A deeper dive into the Heap and Stack. Understand Young Generation, Old Generation, and Metaspace. This hands-on tutorial focuses on practical implementation of jvm memory model concepts.
JVM Memory Model
We touched on Stack vs Heap. Now lets get granular.
Heap Structure
The Heap is divided into generations to optimize Garbage Collection.
- Young Generation:
- Eden Space: Where all new objects start.
- Survivor Spaces (S0, S1): Objects that survive a GC cycle move here.
- Old Generation (Tenured): Objects that survive long enough in Young Gen are moved here. They are "long-lived" objects.
Metaspace (Java 8+)
Replaced the "PermGen". It stores class metadata (static variables, method data). It grows automatically by default.
Stack
Stores primitive variables and references to objects in the Heap for the current thread execution.
Types of References
- Strong Reference: Default.
Object o = new Object();. GC won't touch it. - Weak Reference: GC collects it if only weakly reachable.
- Soft Reference: Collected only if memory is low.
AI Mentor
Confused about "JVM Memory structure: Eden, Survivor spaces, Old Generation and Metaspace"? Ask our AI mentor for a simplified explanation.
Quiz
Quiz
Question 1 of 3Where are new objects initially created?
Next Steps
Who cleans up this memory? The Garbage Collector.