The JVM (Java Virtual Machine) Memory Structure defines how memory is divided and managed when a Java program runs. JVM allocates different memory areas for storing class data, objects, methods, and execution information.
Here are the main parts of JVM memory:
✅ 1. Method Area (Class Area)
- Stores class-level information.
-
Contains:
- Class metadata
- Method information
- Static variables
- Runtime constant pool
Shared among all threads.
👉 Created when JVM starts.
✅ 2. Heap Memory
- Used to store objects and instance variables.
- Shared by all threads.
- Managed by Garbage Collector (GC).
Heap is mainly divided into:
-
Young Generation
- Eden Space
- Survivor Spaces (S0, S1)
Old (Tenured) Generation
👉 Most memory-related errors like OutOfMemoryError occur here.
✅ 3. Stack Memory (JVM Stack)
- Each thread has its own stack.
-
Stores:
- Method calls
- Local variables
- Partial results
Memory is created when a method is called and removed after execution.
👉 Error example: StackOverflowError.
✅ 4. Program Counter (PC) Register
- Each thread has a separate PC register.
- Stores the address of the current executing instruction.
- Helps JVM switch between threads.
✅ 5. Native Method Stack
- Used for executing native methods (methods written in C/C++ using JNI).
- Works alongside the JVM stack.
🧠 Simple Diagram (Conceptual)
JVM Memory
│
├── Method Area (Class Data)
├── Heap (Objects)
├── Stack (Per Thread)
├── PC Register (Per Thread)
└── Native Method Stack
⭐ Quick Interview Tip
- Heap → Objects
- Stack → Method execution
- Method Area → Class metadata
- PC Register → Current instruction
- Native Stack → Native code execution
Top comments (0)