JVM Memory Architecture: Overview, Heap, Metaspace, Stack, and More
This article provides a comprehensive overview of the Java Virtual Machine memory layout, covering heap regions, Metaspace, stack frames, native method stack, program counter, direct memory, and code cache, along with configuration tips, diagnostic commands, and illustrative code examples to help readers understand memory allocation and garbage collection.
Introduction: This JVM series is a personal summary of key JVM concepts to help readers quickly grasp JVM knowledge; for systematic study refer to professional books.
Overview
Memory is a crucial system resource; JVM memory layout defines allocation and management strategies to ensure efficient operation.
Typical JVM memory layout diagrams illustrate the relationship between heap, metaspace, stack, and other regions.
1. Heap
1.1 Introduction
Heap is the main area for object allocation, shared by all threads, and is the primary region where OOM occurs. It is managed by the garbage collector and divided into young and old generations, further into Eden, Survivor spaces, etc.
1.2 Adjustment
Heap size can be configured with -Xms and -Xmx parameters; setting them equal avoids runtime resizing overhead.
-Xms256M -Xmx1024M1.3 Default Allocation
Default ratios can be inspected via java -XX:+PrintFlagsFinal -version , showing parameters like InitialSurvivorRatio and NewRatio .
1.4 Heap OOM Demonstration
/**
* VM Args:-Xms10m -Xmx10m -XX:+HeapDumpOnOutOfMemoryError
* @author Richard_Yi
*/
public class HeapOOMTest {
public static final int _1MB = 1024 * 1024;
public static void main(String[] args) {
List
byteList = new ArrayList<>(10);
for (int i = 0; i < 10; i++) {
byte[] bytes = new byte[2 * _1MB];
byteList.add(bytes);
}
}
}Running with the above VM arguments triggers an OutOfMemoryError and generates a heap dump.
2. Metaspace
Metaspace replaces the permanent generation in Java 8, storing class metadata in native memory. It can be tuned with flags such as -XX:MetaspaceSize and -XX:MaxMetaspaceSize .
3. Java Virtual Machine Stack
Each thread has its own JVM stack containing stack frames, which hold local variables, operand stack, dynamic linking information, and return address.
3.1 Local Variable Table
Stores method parameters and local variables; size is fixed at compile time.
public int test(int a, int b) {
Object obj = new Object();
return a + b;
}3.2 Operand Stack
Used for intermediate calculations; example shown with OperandStackTest class.
public class OperandStackTest {
public int sum(int a, int b) {
return a + b;
}
}3.3 Dynamic Linking
Each frame holds a reference to the method's constant‑pool entry to support dynamic linking.
3.4 Return Address
Methods exit via RETURN , IRETURN , ARETURN , or via exceptions, which pop the current frame.
4. Native Method Stack
Similar to the JVM stack but used for native method execution; it can also throw StackOverflowError and OutOfMemoryError .
5. Program Counter
Thread‑private register indicating the next bytecode to execute, essential for thread scheduling and resumption after context switches.
6. Direct Memory
Off‑heap memory allocated via NIO's DirectByteBuffer , improving performance by avoiding heap copies.
7. Code Cache
Stores JIT‑compiled native code (nmethods). An OOM in this area appears as java.lang.OutOfMemoryError: code cache .
Diagnostic Options
Various JVM flags can be used to monitor and tune these memory regions, such as -XX:+PrintFlagsFinal and -XX:MetaspaceSize .
References
1. "Deep Understanding of the Java Virtual Machine" (3rd edition) 2. "Code Cache" articles 3. Metaspace in Java 8 4. JVM instruction set guides
Architect's Tech Stack
Java backend, microservices, distributed systems, containerized programming, 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.