Understanding JVM Memory Structure: Areas, Allocation, and Tuning
This article provides a comprehensive overview of the Java Virtual Machine memory layout, explaining each region such as the heap, metaspace, stack, program counter, direct memory and code cache, along with allocation strategies, tuning parameters, and practical OOM demonstrations.
The article introduces the JVM memory structure, emphasizing its importance as a critical system resource that bridges the CPU and storage, and outlines the need to understand both the JVM memory layout and the Java Memory Model (JMM).
It presents a classic diagram of the JVM memory areas, distinguishing between thread‑shared regions (heap, metaspace, code cache) and thread‑private regions (stack, native stack, program counter).
Heap (堆区) : Described as the largest shared area where most objects reside, the heap is managed by garbage collectors and divided into young and old generations (Eden, Survivor, Old). The article explains TLAB, object allocation, and the impact of JIT optimizations such as escape analysis.
Heap Adjustment : Shows how to set initial and maximum heap sizes using -Xms and -Xmx , and recommends fixing both values in production to avoid runtime resizing overhead.
Default Heap Allocation : Demonstrates how to view default JVM parameters with java -XX:+PrintFlagsFinal -version and explains the default ratios for Eden, Survivor, and Old generations.
Heap OOM Demonstration : Provides a sample program that triggers java.lang.OutOfMemoryError: Java heap space and shows the use of -XX:+HeapDumpOnOutOfMemoryError to generate a heap dump.
Object Allocation Process : Details how new objects are allocated in Eden, how Young GC moves surviving objects to Survivor spaces, and how objects are promoted to the old generation based on the -XX:MaxTenuringThreshold parameter.
Metaspace (元空间) : Explains the replacement of the permanent generation (PermGen) with Metaspace in Java 8, its native‑memory allocation, and relevant tuning options.
Java Stack (虚拟机栈) : Describes stack frames, local variable tables, operand stacks, dynamic linking, return addresses, and how StackOverflowError and OutOfMemoryError can arise.
Native Method Stack : Highlights its similarity to the Java stack but dedicated to native method execution, noting that some JVMs merge the two stacks.
Program Counter (PC) Register : Clarifies its role as a thread‑private pointer to the current bytecode instruction, essential for thread scheduling and context switching.
Direct Memory : Introduces NIO’s off‑heap memory via DirectByteBuffer , its performance benefits, and the fact that it is still limited by the host’s physical memory.
Code Cache : Defines the area where the JIT compiler stores compiled native code (nmethods), and mentions the possible java.lang.OutOfMemoryError: code cache when it overflows.
The article concludes with diagnostic options, reference links, and acknowledgments.
Architect
Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.
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.