A brief analysis of the data area during execution of the JVM and a deep understanding of the stack and heap

First look at the JVM structure diagram: For
Insert picture description here
an overview of the class loader, please see this blog: JVM like loader

Of course, before explaining the heap and stack, let's first understand what other areas of the runtime data area are used for:

Native method stack

The local method stack serves the local method. The language, usage, and data structure of the method in the local method stack are not mandatory in the virtual machine specification, so the specific virtual machine can freely implement it.
Here is an example. I Thread thread = new Thread(); thread.start();
believe you should be familiar with this code. Create a thread and call its start method. Next, let's click into the source code of the start method!
Insert picture description here
The bottom layer is to call the start0 method, and then we click into the start0 method to see how to write.
Insert picture description here
We can see that this method is modified by the native method, which is not actually a method in the Java language.
The following figure helps you understand the role of the local method stack:
Insert picture description here
so we can simply understand that the local method stack stores native modified methods (methods that call the interface), just like the start0 method in the above example.

PC register

Insert picture description here
Just understand!

Method area

Insert picture description here
Just understand!

Stack

First look at the figure below: What
Insert picture description here
is stored in the stack is 8 basic type variables + object reference variables + instance methods are allocated in the stack memory of the function.
Next, let's understand what a stack frame is:

Stack frame : It can be simply understood as a method in java. Putting it on the stack is called a stack frame. So what is written in the stack frame?
Insert picture description here
If you don’t understand the above text, you can look at the following picture:
Insert picture description here
the add method is called in the main method, and the instance method mentioned before is placed on the stack, so the mian is placed first, followed by the add method, and the local variables of the add method, etc., All are stored in the stack frame.
With the above description, it is not difficult for us to understand the operating principle of the stack, as shown in the figure below: After
Insert picture description here
Insert picture description here
understanding the above brief analysis of the stack, let's take a look at this error (Error):, java.lang.StackOverflowErrorStack overflow, now we can understand if one If the function calls itself recursively without terminating, it will keep adding itself to the stack until the java stack is full!

heap

Heap : For most applications, the java heap is the largest piece of memory managed by the java virtual machine. The Java heap is a memory area shared by all threads and is created when the virtual machine starts. The sole purpose of this memory area is to store object instances, and almost all object instances allocate memory here.
Next , let’s look at a brief analysis of the heap:
Insert picture description here
physically and logically new students go to + retirement area.
Note that after JDK1.8, the permanent area becomes a metaspace!

The following figure is a brief analysis of the newborn area :
Insert picture description here
Insert picture description here
the concept of permanent area:
Insert picture description here
the difference between permanent generation and meta space:
Insert picture description here
Next, I will talk about the tuning of the next heap parameters (ie JVM tuning):
You can view the heap parameters through the following code:
Insert picture description here
code show as below:

long maxMemory = Runtime.getRuntime().maxMemory();//返回Java虚拟机试图使用的最大内存量。
           Long totalMemory = Runtime. getRuntime().totalMemory();//返回Java虚拟机中的内存总量。
           System.out.println("MAX_MEMORY ="+maxMemory +"(字节)、"+(maxMemory/(double)1024/1024) + "MB");
           System.out.println("TOTAL_ MEMORY = "+totalMemory +"(字节)"+(totalMemory/(double)1024/1024) + "MB");

How to set the parameters through the idea and view the details? The print result of
Edit->Edit Configurations
Insert picture description here
combined with the above code to view the heap parameters is as follows:
Insert picture description here
Next, let's look at the error that the heap is bursting, if we use the above method to allocate a small piece of memory to the JVM heap.
Insert picture description here
An error will be reported:: java.lang.OutOfMemoryErrorThrown when the Java virtual machine cannot allocate an object due to insufficient memory, and the garbage collector no longer has available memory.

The relationship between stack, heap, and method area

First look at the figure below. The class metadata represents the template of the class in the method (the structure letter of the class). Then
Insert picture description here
look at the figure below and combine with the figure above to understand: The
Insert picture description here
new p1 and p2 are stored in the stack. This is a reference type. The new Person objects are stored in the heap, and these objects are obtained from the method area of ​​the structure information of the class.

Guess you like

Origin blog.csdn.net/Pzzzz_wwy/article/details/106609188