Understanding JVM Memory Structure: Heap, Metaspace, Stack, Direct Memory and Code Cache
This article provides a comprehensive overview of the Java Virtual Machine memory layout, explaining the roles and allocation strategies of the heap, metaspace, stack frames, program counter, direct memory, and code cache, and includes practical code examples for heap tuning and OOM demonstration.
The JVM memory layout is a critical system resource that bridges the CPU and disk, governing how Java applications allocate, manage, and reclaim memory during execution.
Heap (堆区) is the largest shared memory area where all object instances and arrays reside. It is managed by the garbage collector and divided into Young and Old generations (Eden, Survivor, and Tenured spaces). The article explains how to adjust heap size using -Xms and -Xmx parameters, why setting them equal can reduce GC‑induced pressure, and shows a default allocation example that calculates Young and Old generation sizes based on default ratios.
Heap OOM Demonstration – a sample program public class HeapOOMTest { ... } is provided with VM arguments -Xms10m -Xmx10m -XX:+HeapDumpOnOutOfMemoryError to trigger a java.lang.OutOfMemoryError: Java heap space and generate a heap dump.
Metaspace (元空间) replaces the permanent generation (PermGen) in Java 8. It stores class metadata, method data, and constant pools in native memory, eliminating the fixed size limitation of PermGen. The article discusses why PermGen was removed, how Metaspace grows dynamically, and shows related JVM tuning flags.
Java Virtual Machine Stack – each thread gets its own stack containing stack frames. A stack frame holds the local variable table, operand stack, dynamic linking information, and return address. The article details the lifecycle of a stack frame, the meaning of StackOverflowError , and provides a simple method example with its bytecode disassembly.
Local Method Stack serves native methods; many JVM implementations merge it with the Java stack. It can also throw StackOverflowError or OutOfMemoryError .
Program Counter (PC) Register is a tiny thread‑local memory that points to the next bytecode instruction to execute, enabling thread context switching without losing execution state.
Direct Memory (直接内存) is allocated outside the Java heap via NIO's DirectByteBuffer . It is not limited by -Xmx but still bound by the host OS memory limits and can cause OOM if over‑used.
Code Cache stores JIT‑compiled native code (nmethods). When the cache fills, a java.lang.OutOfMemoryError: code cache may occur. Diagnostic options and reference links for monitoring the code cache are provided.
The article concludes with a diagnostic options table, several reference links (including "深入理解Java虚拟机" and Baeldung’s JVM Code Cache guide), and a brief promotional footer.
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.