Understanding JVM Memory Layout and Allocation
This article provides a comprehensive overview of the Java Virtual Machine memory architecture, covering heap layout, allocation, tuning parameters, garbage collection, stack frames, method area, metaspace, native method stack, program counter, direct memory, and code cache, with practical code examples and diagnostics.
The author, a senior architect, shares notes from his study of the Java Virtual Machine (JVM), aiming to help readers quickly grasp key JVM memory concepts.
Key topics include:
JVM memory region overview
Heap allocation and OOM demonstration
Object creation memory flow
Method area to Metaspace
Stack frames and their contents
Native method stack
Program counter
Direct memory
Code Cache and diagnostics
Heap (堆区) is the largest shared memory area where most objects and arrays reside. It is managed by the garbage collector and can be divided into Young and Old generations, with further subdivisions such as Eden, Survivor (S0, S1), and TLAB.
/**
* 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 the program with the above VM arguments triggers an java.lang.OutOfMemoryError: Java heap space and produces a heap dump, illustrating how heap exhaustion occurs.
JVM runtime parameters can be inspected with:
java -XX:+PrintFlagsFinal -versionRelevant flags for heap sizing include -Xms (initial heap size) and -Xmx (maximum heap size). In production, setting -Xms and -Xmx to the same value avoids costly heap resizing during GC.
Object allocation flow shows that a new object is first allocated in the Eden space; when Eden fills, a Young GC moves surviving objects to Survivor spaces or promotes them to the Old generation based on the -XX:MaxTenuringThreshold setting.
public int test(int a, int b) {
Object obj = new Object();
return a + b;
}The operand stack is demonstrated with a simple sum method:
public int sum(int a, int b) {
return a + b;
}Disassembling the compiled class shows the maximum stack depth and local variable count:
stack=2, locals=3, args_size=3
0: iload_1
1: iload_2
2: iadd
3: ireturnMethod area / Metaspace replaces the former PermGen in Java 8. Class metadata, static fields, and constant pools are now stored in native memory (Metaspace), which grows until it reaches the limits of the underlying OS memory.
Native method stack serves native (non‑Java) code similarly to the JVM stack; many JVM implementations merge the two.
Program Counter (PC) Register is a thread‑local pointer indicating the next bytecode instruction to execute, enabling correct thread resumption after context switches.
Direct Memory is off‑heap memory allocated via NIO’s DirectByteBuffer . It bypasses the Java heap, improving I/O performance but is still subject to OS memory limits.
Code Cache stores JIT‑compiled native code (nmethods). Excessive code cache usage can cause java.lang.OutOfMemoryError: code cache .
Diagnostic options and visual tools are provided to monitor these memory regions and troubleshoot related OOM errors.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.