Understanding Java 8 Memory Structure: JVM Areas, Stack, Heap, and Native Memory
This article explains Java 8's memory architecture, detailing the differences between JVM-managed memory and native memory, and describing each runtime data area—including the program counter, JVM stack, native method stack, heap, method area, and direct memory—along with common questions about variable storage and native methods.
Java 8 Memory Structure Diagram
Difference Between JVM Memory and Native Memory
When the JVM executes, it divides the memory it manages into several regions called JVM memory; memory that the JVM does not manage directly but can still use is called native memory, and the two have distinct differences.
JVM Memory
Its size is controlled by JVM parameters; exceeding the configured size triggers an OutOfMemoryError (OOM).
Native Memory
It is not limited by JVM parameters, only by the physical memory capacity.
If native memory usage exceeds physical memory, an OOM is also reported.
Java Runtime Data Areas
According to the Java Virtual Machine Specification, the runtime data area is divided into several regions, some tied to thread lifecycle and others to the Java process.
Program Counter Register
The program counter indicates the line number of the bytecode currently executed by a thread. Changing its value selects the next instruction, enabling jumps, loops, and thread resumption.
Each processor core can run only one thread at a time; multithreading is achieved by time-sliced thread switching, which requires a marker to remember each thread's execution point.
The program counter is thread‑private; each thread has its own program counter.
JVM Stack (JVM Stacks)
The JVM stack is thread‑private and created/destroyed with the thread. It models the memory for methods in a thread.
When a method is executed, a stack frame is synchronously created in the JVM stack.
Each stack frame contains:
Local Variable Table – stores primitive types and object references defined in the method.
Operand Stack
Dynamic Linking
Method Return Address
Methods are pushed onto the stack when invoked and popped after execution.
The JVM stack can throw two kinds of errors:
If a thread requests a stack depth greater than the configured limit, a StackOverflowError occurs.
If the stack cannot obtain memory (even when dynamically expandable), an OutOfMemoryError occurs.
Native Method Stack (Native Method Stacks)
The native method stack executes native methods, whereas the JVM stack executes Java methods.
Java Heap
The Java heap is the largest memory region in the JVM, shared by all threads and managed by the garbage collector. It stores object instances and, since Java 8, also holds the string constant pool and static variables.
Object Instances
Objects created during class initialization.
Arrays of primitive types (also objects).
String Constant Pool – originally in the method area, moved to the heap in JDK 7.
Static Variables – variables marked with static, moved from the method area to the heap in JDK 8.
Thread‑Local Allocation Buffer (TLAB) – thread‑private buffer that improves allocation efficiency.
The heap can be fixed‑size or expandable via -Xmx and -Xms. If it cannot expand or allocate memory, an OOM is reported.
Method Area (Method Area)
The method area is shared by all threads. Before Java 8 it was implemented as the permanent generation (PermGen) and limited by JVM parameters. In Java 8 it was replaced by Metaspace, which resides in native memory and is not limited by JVM parameters (though it can still OOM if physical memory is exhausted). The string constant pool and static variables were moved from the method area to the heap.
Class Metadata (Klass)
During compilation, class metadata is placed in the method area, containing version, fields, methods, interfaces, and the constant pool table.
The constant pool table stores literals and symbolic references generated at compile time; after class loading, they are resolved into the runtime constant pool.
Runtime Constant Pool
Stores literals and symbolic references after class loading.
It is dynamic; for example, String.intern() uses it.
Direct Memory
Direct memory resides in native memory, outside the JVM heap. Introduced in JDK 1.4 with NIO, it allows allocation of off‑heap memory via native functions and access through java.nio.DirectByteBuffer, improving I/O performance by avoiding heap‑to‑native copying.
Common Questions
What is a Native method?
Native methods are declared with the native keyword and implemented in another language (e.g., C) to access low‑level resources. Using native methods reduces portability.
Where are class variables, member variables, and local variables stored?
Class Variables
Declared with static, they exist for the lifetime of the Java process.
Before Java 8 they resided in the method area; since Java 8 they are stored in the heap.
Member Variables
Declared in a class without static; they belong to each instance and are stored in the heap together with the object.
Local Variables
Declared inside methods; they are placed in the local variable table of a stack frame and live on the JVM stack.
Where are constants declared with final stored?
The final keyword does not affect the storage location; they follow the same rules as the variable type they belong to.
How do the class constant pool, runtime constant pool, and string constant pool relate?
Both the class constant pool and runtime constant pool reside in the method area, while the string constant pool was moved to the heap in JDK 7.
What are literals and symbolic references?
During compilation, literals represent concrete data values (e.g., int a = 1; or String s = "iloveu";). Symbolic references are placeholders (e.g., class names) used when the actual memory address of a class is unknown until class loading.
int a = 1; // 1 is a literal
String b = "iloveu"; // "iloveu" is a literalSigned-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.
ITFLY8 Architecture Home
ITFLY8 Architecture Home - focused on architecture knowledge sharing and exchange, covering project management and product design. Includes large-scale distributed website architecture (high performance, high availability, caching, message queues...), design patterns, architecture patterns, big data, project management (SCRUM, PMP, Prince2), product design, and more.
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.
