Unlocking Java: A Deep Dive into JVM Memory Architecture
This article provides a comprehensive introduction to the Java Virtual Machine’s memory architecture, detailing the five runtime areas—Program Counter, JVM Stack, Native Method Stack, Heap, and Method Area—their thread scope, memory allocation, garbage‑collection behavior, and common errors such as OutOfMemoryError and StackOverflowError.
JVM Memory Structure Overview
Java Virtual Machine divides runtime memory into five logical areas: Program Counter Register, JVM Stack, Native Method Stack, Heap, and Method Area. The first three are thread‑private, while Heap and Method Area are shared among threads.
Understanding this diagram gives you a solid grasp of half the JVM memory model.
Heap (GC Heap)
The Heap is the largest memory region, shared by all threads, and stores Java objects. It is the primary target of garbage collection and is divided into generations (young and old). The young generation further contains Eden, Survivor‑S0, and Survivor‑S1 spaces. The JVM specification allows the heap to be physically non‑contiguous as long as it appears contiguous logically. When the heap cannot expand and no free space remains, an OutOfMemoryError is thrown.
Garbage collection in the heap follows a generational collection algorithm.
Method Area
The Method Area (often called Non‑Heap) stores class metadata, the constant pool, static variables, and JIT‑compiled code. Like the heap, it is shared and can be expanded, but when it cannot grow an OutOfMemoryError occurs. Its reclamation focuses on constant‑pool cleanup and class unloading, which is notoriously difficult.
Program Counter Register
The PC Register holds the address of the next bytecode instruction for each thread. It is the only JVM area that never throws OutOfMemoryError. For native methods the PC is undefined.
JVM Stack
Each thread has its own JVM Stack, which stores stack frames. A stack frame contains a local variable table, an operand stack, a reference to the runtime constant pool for dynamic linking, and the return address. Local variables include primitive types, object references, and return addresses; long and double occupy two slots. Stack overflow or insufficient native memory results in StackOverflowError or OutOfMemoryError.
Native Method Stack
The Native Method Stack serves native (C/C++) methods similarly to the JVM Stack and can also throw StackOverflowError or OutOfMemoryError.
By mastering these components, developers can better diagnose memory‑related bugs, tune JVM parameters, and succeed in technical interviews.
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.
Senior Brother's Insights
A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.
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.
