An Article to Understand the Memory Distribution of Jvm Runtime

Memory Distribution of JVM Runtime Introduction:

An article to understand the memory distribution of JVM runtime

Memory Distribution of JVM Runtime.A busy year is coming to an end. Due to several reasons, I decided to start writing some basic series, mainly including Java basics, Android basics, design patterns and algorithms, etc. I haven't thought of a good name for this series yet.
There are many implementations of virtual machines, such as HotSpot , Android Dalvik, ART, etc. The specific implementations of different virtual machines are different, but they all conform to the rules in the Java virtual machine specification.

JVM runtime memory distribution from 1+2

Create a new Test class and define a static method sum, the code is as follows:
public class Test {
public static void main( String[] args ) {
System.out.println ( sum( ));
public static int sum( ) {
int a = 1;
int b = 2;
return a + b;

Run the program, the print result is 3. So what is the process of running the Test file?

Memory Distribution of JVM Runtime.JVM memory distribution

First, the file is compiled by the editor to generate the Test.class file. When running the Test class, Test.class is loaded into the JVM memory through the ClassLoader , as shown in Figure 1.

Figure 1 execution flow
The JVM runtime memory is mainly divided into five parts: program counter, virtual machine stack , local method stack , heap, and method area, as shown in Figure 2.

The method area and heap are shared among threads, and the virtual machine stack , local method stack , and program counter are private to threads. Let's look at the respective roles of these areas in turn.
program counter
The program counter is used to keep track of where the current thread is executing. The CPU can allocate execution time among multiple threads. When a thread is suspended, the program counter is used to record the position where the code has been executed. When the thread resumes execution, it continues to execute from the recorded position. Common exception handling, branch operations, etc. are all done through the program counter.
Inside each thread is a program counter that is created as threads are created and destroyed as threads are destroyed. The counter records the address of the virtual machine bytecode instruction being executed. If the native method is currently being executed, the counter value is empty.
virtual machine stack
The virtual machine stack is used to describe the memory model of Java method execution. We all know that the JVM is executed by a stack -based interpreter. The stack here refers to the virtual machine stack , or more precisely , the operations in the virtual machine stack stack frame. number stack .
stack frame for each method in the execution mode, and the stack frame contains the local variable table, operand stack , dynamic link and return address. The stack frame distribution in the thread is shown in Figure 3.

local variable table
The local variable table is the storage space for variable values. The parameters passed by the calling method and the variables created inside the method will be saved in the local variable table. After the java file is compiled, the size of the local variable table has been determined, and it will be written in the max_locals attribute in the Code attribute table .
Taking the code for adding the above two numbers as an example, the bytecode code for viewing the Test file is as follows:
public static int sum( );
descriptor: ()I
stack=2, locals=2, args_size =0
0: iconst_1
1: istore_0
2: iconst_2
3: istore_1
4: iload_0
5: iload_1
6: iadd
7: ireturn
LineNumberTable :
line 16: 0
line 17: 2
line 18: 4

It can be seen from the bytecode file that the value of the locals attribute is 2, indicating that the size of the local variable table is 2, which are used to store variable a and variable b respectively. args_size indicates the number of parameters, where the parameter is 0, and stack indicates the maximum value of the operand stack . First, let’s look at what the operand stack is.
operand stack
operand stack . In the bytecode code table, stack=2 indicates that the maximum depth of the operand stack is 2. When the method is executed, bytecode instructions will be pushed or popped. Taking the above bytecode operation as an example, let's take a look at the operands . Changes to the stack and local variable table.
First, let's take a look at the meaning of each command value:
iconst : push the constant into the top of the operand stack . Similar to this, there is the bipush instruction. When the value of int is -1~5, the iconst instruction is used, and the value of -128~127 is the bipush instruction.
istore : Pop the top element of the operand stack into the index position of the local variable table, istore_n means to put the top element of the stack in the position marked n in the local variable table.
iload : iload_n means to push the value with subscript n in the local variable table onto the top of the stack
iadd : Add the top two elements of the operand stack and push the result to the top of the stack
Take the 1+2 bytecode method as an example
0: iconst_1
1: istore_0
2: iconst_2
3: istore_1
4: iload_0
5: iload_1
6: iadd
7: ireturn

Figure 4 below shows the word local variable table and operand stack when the sum mode is just started .

Figure 4 Local variable table and operand stack initial state
After executing 0: iconst_1, as shown in Figure 5.

Figure 5
After executing 1: istore_0, as shown in Figure 6.

do the same
2: iconst_2
3: istore_1
4: iload_0
5: iload_1
6: iadd
The sequential changes are shown in Figure 7.

element 3 in the operand stack , thus the operation of 1+2=3 is completed , and the local variable table and operand stack will be destroyed after the method is executed.
We often encounter StackOverflowError exceptions. This is because each time we call a method, a stack frame will be created in the virtual machine stack . When an exception is encountered and the method cannot exit, the stack frame will not be destroyed . The exception that caused the StackOverflowError .
dynamic link
Memory Distribution of JVM Runtime.Dynamic linking is to support dynamic linking during method invocation. If a method calls another method, it needs to convert the symbolic reference of the method to the application of the memory address, and the symbolic reference is stored in the method area.
return address
Memory Distribution of JVM Runtime.The return address can restore the current method to the execution state of the upper-level method, so that it can return to the location where the method was called and continue execution after the method exits.
There are only two ways to exit the method: normal exit and abnormal exit. The program counter can be used as the return address during normal exit, and the return address needs to be determined through the exception handler table during abnormal exit.
native method stack
native method stack is basically the same as the virtual machine stack , and is mainly used to manage native methods, such as using JNI in Android. The native method stack is not introduced separately here.
method area
The method area is mainly used to store loaded classes, static variables, constants and other information. The method area is only the area specified in the JVM specification, and different JVM manufacturers implement it differently. This is something to be aware of.

The heap is the largest piece of memory managed by the JVM. The heap is used to store instances of objects and is also the main area managed by the GC.

Memory Distribution of JVM Runtime.According to the time of storage objects, it can be divided into the new generation and the old generation. The new generation is divided into Eden area and Survivor area. Different storage areas store objects with different life cycles, so that each area can use different garbage collection algorithms. This increases the garbage collection rate. The partitioning of the heap is shown in Figure 8.

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us