Yige

Yige

Build

JVM Series - JVM Memory Structure

JVM Series - JVM Memory Structure#

Content from:

  1. JVM Memory Structure
  2. JVM Memory Structure and Java Memory Model

Basic Concepts#

The memory space of the Java Virtual Machine is divided into 5 parts:

  • Program Counter
  • Java Virtual Machine Stack
  • Native Method Stack
  • Heap
  • Method Area

Referencing the diagram from the book "Understanding the Java Virtual Machine":
image.png

Program Counter (PC Register)#

Definition of Program Counter#

The program counter is a small memory space that holds the address of the bytecode instruction currently being executed by the thread. If the current thread is executing a native method, then the program counter is Undefined.

Function of Program Counter#

  • The bytecode interpreter reads instructions sequentially by changing the program counter, thus controlling the flow of code.
  • In a multi-threaded situation, the program counter records the position of the current thread's execution, so when the thread switches back, it knows where the last thread left off.

Characteristics of Program Counter#

  • It is a small memory space.
  • It is thread-private; each thread has its own program counter.
  • Lifecycle: Created with the thread and destroyed when the thread ends.
  • It is the only memory area that will not encounter OutOfMemoryError.

Java Virtual Machine Stack#

Definition of Java Virtual Machine Stack#

The Java Virtual Machine Stack is a memory model that describes the execution process of Java methods.

The Java Virtual Machine Stack creates a space called "stack frame" for each Java method that is about to run, used to store some information during the method's execution, as shown in the diagram:
image.png

Characteristics of Java Virtual Machine Stack#

  • The local variable table is created with the stack frame, and its size is determined at compile time; it only needs to allocate a predetermined size upon creation. The size of the local variable table does not change during the method's execution.
  • The Java Virtual Machine Stack can throw two exceptions: StackOverFlowError and OutOfMemoryError.
  • StackOverFlowError occurs if the size of the Java Virtual Machine Stack does not allow dynamic expansion, and when the thread requests a stack depth that exceeds the maximum depth of the current Java Virtual Machine Stack, it throws a StackOverFlowError exception (when StackOverFlowError occurs, there may still be a lot of memory space available).
  • OutOfMemoryError occurs if dynamic expansion is allowed, and when the thread requests stack memory but it is exhausted and cannot be dynamically expanded, it throws an OutOfMemoryError exception.
  • The Java Virtual Machine Stack is also thread-private, created with the thread and destroyed when the thread ends; data is not shared among threads, so there is no need to worry about data consistency issues, and there will be no synchronization lock issues.

Native Method Stack (C Stack)#

When Java uses C or C++ to write interface services, the code runs in this area.

The Native Method Stack is the space prepared for the JVM to run native methods. Since many native methods are implemented in C, it is often called the C Stack. It serves a similar function to the Java Virtual Machine Stack, but the Native Method Stack describes the memory model of the execution process of native methods.

Heap#

Definition of Heap#

The heap is the memory space used to store objects, and almost all objects are stored in the heap. The heap memory is divided into Young Generation, Old Generation, and Permanent Generation. The Young Generation is further divided into: Eden Area + Survivor1 Area + Survivor2 Area. In JDK 1.8, the entire Permanent Generation was removed, replaced by an area called Metaspace (the Permanent Generation used the JVM's heap memory space, while Metaspace uses physical memory, directly limited by the physical memory of the host).

Characteristics of Heap#

  • Thread-shared; there is only one heap for the entire Java Virtual Machine, and all threads access the same heap. In contrast, the program counter, Java Virtual Machine Stack, and Native Method Stack are one thread per instance.
  • Created when the virtual machine starts.
  • It is the main area for garbage collection.

Different areas store objects with different lifecycles, allowing for the use of different garbage collection algorithms based on the area, making it more targeted.

The size of the heap can be fixed or expandable, but for mainstream virtual machines, the heap size is expandable. Therefore, when a thread requests memory allocation but the heap is full and cannot be expanded, it will throw an OutOfMemoryError exception.

The memory used by the Java heap does not need to be contiguous. Since the heap is shared among all threads, access to it needs to consider synchronization issues, and methods and corresponding attributes need to ensure consistency.

Method Area#

Definition of Method Area#

The Java Virtual Machine specification defines the method area as a logical part of the heap. The method area stores the following information:

  • Class information that has been loaded by the virtual machine
  • Constants
  • Static variables
  • Code compiled by the Just-In-Time compiler

Characteristics of Method Area#

  • Thread-shared: The method area is a logical part of the heap, so like the heap, it is shared among threads. There is only one method area in the entire virtual machine.
  • Permanent Generation: The information in the method area generally needs to exist for a long time, and since it is a logical partition of the heap, it is referred to as the "Permanent Generation."
  • Low memory recovery efficiency. The information in the method area generally needs to exist for a long time, and after a round of garbage collection, only a small amount of information may be invalid. The main recovery targets are: recovery of the constant pool; unloading of types.
  • The Java Virtual Machine specification has relatively loose requirements for the method area. Like the heap, it allows for fixed sizes, dynamic expansion, and does not require garbage collection.

Runtime Constant Pool#

The method area stores: class information, constants, static variables, and code compiled by the Just-In-Time compiler. Constants are stored in the runtime constant pool.

When a class is loaded by the Java Virtual Machine, the constants in the .class file are stored in the runtime constant pool of the method area. Moreover, during runtime, new constants can be added to the constant pool. For example, the intern() method of the String class can add string constants to the constant pool during runtime.

JVM Control Parameters#

image.png
As shown in the figure, some commonly used control parameters are as follows:

  • -Xms sets the minimum size of the heap.
  • -Xmx sets the maximum size of the heap.
  • -XX:NewSize sets the minimum size of the Young Generation.
  • -XX:MaxNewSize sets the maximum size of the Young Generation.
  • -XX:PermSize sets the minimum size of the Permanent Generation.
  • -XX:MaxPermSize sets the maximum size of the Permanent Generation.
  • -Xss sets the stack size for each thread.

There are no direct parameters to set the Old Generation, but you can indirectly control it by setting the heap size and Young Generation size parameters. The size of the Old Generation = Heap size - Young Generation size.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.