Understanding JVM Memory Areas: A Deep Dive into Java Runtime Data
This article explains the JVM as Java's operating system, outlines its class loading process, and provides a detailed walkthrough of each runtime memory region—including program counter, stacks, heap, method area, and direct memory—highlighting their purposes and related error conditions.
Background
The JVM (Java Virtual Machine) is essential for Java development, acting as the operating system for Java. It runs class files, which are platform‑independent bytecode, and loads them according to the JVM specification.
1. Overview
Unlike C/C++ where developers manage memory manually, Java programmers rely on the JVM's automatic memory management, which reduces leaks and overflows but requires understanding of how the JVM uses memory to troubleshoot issues.
2. Java Runtime Data Areas
The JVM divides memory into several distinct regions, each with specific roles and lifetimes.
Another illustration:
Additional reference diagram:
(1) Program Counter
The program counter (PC) is a small memory space that records the current bytecode line number for each thread, enabling the interpreter to select the next instruction. Each thread has its own PC, making it thread‑private.
(2) Java Virtual Machine Stack
The JVM stack is also thread‑private and stores stack frames for each method call, containing the local variable table, operand stack, dynamic linking, and return address. It works together with the heap, which holds object instances.
(3) Native Method Stack
The native method stack serves native (non‑Java) code similarly to the JVM stack. Some JVMs merge these stacks. Both can throw StackOverflowError or OutOfMemoryError.
(4) Java Heap
The heap is the largest memory area, shared by all threads, and stores all object instances and arrays. It is managed by garbage collectors and is divided into generations (young and old), with further sub‑areas like Eden, Survivor spaces, and thread‑local allocation buffers.
(5) Method Area
The method area (also called Non‑Heap) stores class metadata, static variables, constant pool, and JIT‑compiled code. It can be fixed‑size or expandable, and may or may not be garbage‑collected.
(6) Runtime Constant Pool
Part of the method area, the runtime constant pool holds literals and symbolic references from class files, and can be dynamically expanded during execution (e.g., via String.intern()).
(7) Direct Memory
Direct memory is off‑heap memory allocated via NIO buffers. It is not part of the JVM’s managed regions but can cause OutOfMemoryError if the total native memory exceeds system limits.
Signed-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.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
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.
