- Java Runtime Area classification
- Understand the Java Runtime Area from a threading perspective
- Understand the Java Runtime Area from the storage contents
- What exactly is stored in the method area?
- In which memory region of the JVM do member variables of the base data type reside?
Java Runtime Area classification
The Java Runtime Area is divided into six parts:
- Program Counter (PC) Register Program Counter
- Java Virtual Machine Stacks Java Virtual Machine Stacks
- Heap of Java Heap Memory
- Method Area Method Area
- Run-time Constant Pool Specifies the Constant Pool at runtime
- Native Method Stacks
The details and features of each area can be found in in-depth Understanding of the Java Virtual Machine, which is covered in great detail. Below, we classify these data areas and analyze them from different perspectives to deepen our understanding
Understand the Java Runtime Area from a threading perspective
First, we classify regions by whether they are thread private or shared by all threads:
The program counter Java virtual Machine stack The local method stack is thread private and the Java heap method area runtime constant pool is shared by all threads
Further understanding:
-
For thread-private data area program counters Java virtual machine stack local method stacks, they all have the same lifetime, being initialized as the thread starts and destroyed as the thread ends
-
For the data area shared by threads, the Java heap method area runtime constant pool, their lifetime is consistent. Memory is allocated as the JVM starts and is destroyed as the JVM shuts down
Understand the Java Runtime Area from the storage contents
There are three types of data stored in different regions
- The method area and constant pool store information about the class
- Heap memory stores object information
- Program counters, Java virtual machine stacks, and local method stacks store thread information
The diagram below illustrates this very clearly
The heap space holds object data, the method area holds class code, and the native area holds references to the code and object data. The heap stores object data, the method area stores class information and code, and the native area stores references to class information and code and references to object data
The following diagram shows the contents of the three regions in more detail:
Let’s go through an example of actual code;
Take a look at this code:
When compiled, the code is stored like this:
Confusing Java Runtime Area issues
Below we’ll address some of the confusion surrounding the Java runtime data area
What exactly is stored in the method area?
The Stack contains information about the method, such as the local variable table, but there is also the important content of the instruction code in the method. It is neither in the Stack nor in the Heap. Where is it? Actually, including method of area, in addition to what you said “has been the basic information of the loaded classes, constants, static variables, such as” outside, still include compilers compiled code, and it should be a method area in the main part of the class, after all, mainly methods and properties, and the properties in the class, if is the instance fields after the new object is stored in the Heap (Heap), Static is stored in the methods area as you said, so methods are the main part of this area, which is why the runtime data area is called the methods area.
In which memory region of the JVM do member variables of the base data type reside?
Such as
class{
private int i;
}
Copy the code
Some friends may think that the basic data type is stored on the stack. But is actually stored in the heap, because this is belongs to the information of objects, each object has a different instance variables, and the instance variables are stored in the heap, whether it’s basic data types or reference data type stack is ava virtual machine thread private life cycle with the same thread, each method invocation is created when a stack frame is used to store the local variables, Operand stacks, dynamic links, method exits, etc. The process of each method call represents the process of a stack frame being pushed in and out of the virtual machine stack. When entering a method, how much memory the method needs to allocate in the stack is completely determined. Many Java programmers are initially misled by online tutorials that put basic data types on the stack and instances of arrays and classes on the heap. This is not accurate. In fact, the instance variable I above is stored in the Java heap. Because it’s not a static variable, it doesn’t exist independently of the class instance, which, when instantiated, is put in the heap, and of course contains its property I. If int I = 0 is defined in the method; Two objects are created in the local variable table: references I and 0. Both objects are thread-private (safe). For example, int[] is = new int[10]. Two objects are defined, one is reference, placed in a local variable table, and one is an array of length 10, placed in the heap. This array can only be accessed by is, and is is destroyed when the method ends. Determine that the array is unreachable and destroy it.