JVM & Memory ModelIntermediate
1Question 1 of 1
javaWhat is the JVM Memory Model (Heap, Stack, Metaspace) and how do they interact?
Explanation & Concept
The JVM divides runtime memory into distinct regions:
• **Stack**: Thread-private memory. Stores method stack frames, primitive local variables, and object references. Allocation and deallocation are instant (LIFO). • **Heap**: Shared memory where all object instances live. Managed by the Garbage Collector. • **Metaspace**: Native memory holding class metadata, bytecode definitions, and static variables (replaces PermGen in Java 8+).
Code Example
java
public class MemoryModelDemo {
// Static reference stored in Metaspace / Heap
private static final String APP_NAME = "LearnaApp";
public void processOrder() {
int orderCount = 5; // Local primitive: stored in thread Stack
Order order = new Order(); // 'order' reference on Stack; Order object instance on Heap!
}
}
class Order {}How It Works & Key Takeaways
When `processOrder()` finishes, its stack frame is instantly popped. The `Order` object on the heap becomes eligible for Garbage Collection.
Mock Test · 1 Questions · Optional
Ready for Garbage Collection?
Take a quick test to evaluate yourself, or move straight to the next module.
Intermediate2 questions