Understanding Java JVM Memory Model: All Runtime Data Areas Explained
This article provides a comprehensive overview of the Java Virtual Machine memory model, detailing each runtime data area—including the heap, stacks, method area, program counter, and direct memory—and explains their purposes and interactions within Java applications.
Memory Model & Partitions
When a Java program runs, the JVM manages a runtime data area that is divided into several partitions.
Java Virtual Machine Stack
Native Method Stack
Java Heap
Method Area
Program Counter
Java Heap
The heap stores all object instances and arrays. It is the main area for dynamic memory allocation.
Additional illustration of the heap structure.
Java Virtual Machine Stack
The JVM stack stores frames for each method invocation, including local variables, the operand stack, and the return address.
Illustration of the JVM stack layout.
Native Method Stack
Similar to the JVM stack but dedicated to native (non‑Java) method calls.
Method Area
The method area holds class structures, static variables, and the runtime constant pool.
The constant pool stores literals and symbolic references used by the JVM.
Program Counter
The program counter (PC) register points to the current instruction being executed for each thread.
Extra Knowledge: Direct Memory
Definition: Off‑heap memory allocated via NIO’s DirectByteBuffer using native functions.
Features: Not limited by the Java heap size.
Use case: Frequent I/O operations to avoid copying between Java heap and native memory.
Exception: OutOfMemoryError when total memory usage exceeds physical limits.
Summary
This article comprehensively explains the JVM memory model and its partitions, covering the heap, stacks, method area, program counter, and direct memory.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
