2.9 JVM Memory Model Architecture
← Back to Chapter 2: Java Overview & Memory Model
The Java Virtual Machine organizes physical system memory into distinct logical runtime data areas defined by the JVM Specification.
1. JVM Runtime Data Areas Overview
┌────────────────────────────────────────────────────────────────────────┐
│ JVM Process Memory │
│ │
│ ┌────────────────────────────────────────────────────────────── ────┐ │
│ │ Shared Across All Threads │ │
│ │ ┌───────────────────────────────────────┐ ┌────────────────────┐ │ │
│ │ │ Heap Space │ │ Metaspace │ │ │
│ │ │ • Young Gen (Eden, S0, S1) │ │ (Class metadata, │ │ │
│ │ │ • Old Gen (Tenured) │ │ static fields) │ │ │
│ │ └───────────────────────────────────────┘ └────────────────────┘ │ │
│ └──────────────────────────────────────────────────────────────────┘ │
│ │
│ ┌──────────────────────────────────────────────── ──────────────────┐ │
│ │ Per-Thread Isolated Memory (Created per active Java thread) │ │
│ │ ┌───────────────────┐ ┌───────────────────┐ ┌──────────────────┐ │ │
│ │ │ Thread Stack │ │ PC Register │ │ Native Method │ │ │
│ │ │ (Stack Frames) │ │ (Next Bytecode IP)│ │ Stack │ │ │
│ │ └───────────────────┘ └───────────────────┘ └──────────────────┘ │ │
│ └──────────────────────────────────────────────────────────────────┘ │
└────────────────────────────────────────────────────────────────────────┘
2. Deep Dive: Memory Regions
A. Java Heap
- Shared across all threads.
- Stores all class instances, arrays, and associated instance fields.
- Managed and reclaimed automatically by the Garbage Collector (GC).
- Throws
OutOfMemoryError: Java heap spacewhen full.
B. Thread Stack (JVM Stack)
- Private to each thread. Created when the thread spawns; destroyed on termination.
- Composed of Stack Frames. Each method invocation pushes a new frame:
- Local Variable Array (LVA): Primitives and object reference pointers.
- Operand Stack: Workspace where mathematical operations and bytecode evaluations occur.
- Frame Data: Method return address and reference to runtime Constant Pool.
- Throws
StackOverflowErroron deep or infinite recursion.
C. Metaspace (Native Memory, Java 8+)
- Replaced the old contiguous
PermGen(Permanent Generation) in Java 8. - Located in Native OS Memory, outside the JVM heap limit.
- Stores class metadata, bytecode definitions, runtime constant pools, and method definitions.
- Automatically expands up to physical OS memory unless capped via
-XX:MaxMetaspaceSize.
D. Program Counter (PC) Register
- Holds the address of the current JVM bytecode instruction being executed by the thread.
- If executing a native method, the PC register value is
undefined.