Article From:https://www.cnblogs.com/chanshuyi/p/jvm_serial_06_jvm_memory_model.html

Looking at this, I believe you have a clear idea of how a Java source file becomes a bytecode file and what the bytecode file means. Then let the Java virtual machine run the bytecode file to get the final result we want. In the process, J.The AVA virtual machine loads the bytecode file, stores it in the memory space of the Java virtual machine, then performs a series of initialization actions, and finally runs the program to get the result.

So how is bytecode data stored in Java virtual machine memory? How does the Java Virtual Machine allocate memory for class instances or member variables? To answer these questions, we first need to understand the memory structure of the Java virtual machine.

In fact, the memory structure of the Java virtual machine is not an official term. The term “runtime data area” is used in the Java Virtual Machine Specification. But many times this term is not very vivid, coupled with accumulated habits, we are used to the term virtual machine memory structure.。

According to the Java Virtual Machine Specification, the memory structure of the Java Virtual Machine can be divided into public and private parts. Public refers to parts shared by all threads, Java heap, method area, constant pool. Private refers to the private data for each thread, including:Register, Java Virtual Machine Stack, Local Method Stack.

public part: Java heap, method area, constant pool

In Java virtual machine, thread sharing includes Java heap, method area and constant pool.

Java The heap refers to an area that is partitioned from the JVM and is dedicated to memory allocation for Java instance objects, where almost all instance objects are allocated. Almost because of special circumstances, sometimes small objects are allocated directly on the stack.We call this phenomenon “allocation on the stack”. This is not an in-depth introduction. There are chapters to follow.

Method area refers to the area that stores Java class bytecode data. It stores the structure information of each class, such as run-time constant pool, field and method data, construction method, etc. You can see that the constant pool is actually stored in the method area, but the Java Virtual Machine Specification will store the constant pool and the method area.The method area is placed on the same level, which we know.

Method areas have different forms in different versions of virtual machines. For example, in version 1.7 of the HotSpot virtual machine, method areas are called Permanent Space, while in JDK 1.8 they are called MetaSpace.。

After describing the general functions of these parts, let’s go into the Java heap.

Java The heap can also be divided into two regions: the younger generation and the older generation. The younger generation can be further divided into Eden, From Survivor 0 and To Survivor 1 regions. As shown in the figure below.

When there are objects to be allocated, an object is always allocated in the Eden area of the younger generation first. When there is not enough memory in the Eden area, the Java virtual opportunity starts garbage collection. At this point, the memory of unreferenced objects in the Eden area will be reclaimed, and some lifetime will be saved.Longer objects will enter the old age. In the JVM, there is a parameter named – XX: Max Tenuring Threshold, which is used to set the number of GC times needed for promotion to the older generation, i.e. the number of GC times passed by the younger generation’s object.Later, it will enter the old age at the next GC.

Let’s think about this: Why does the Java heap have such a zoning?

According to our experience, objects in virtual machines must have both long-lived objects and short-lived objects, which is a common normal distribution law. If we mix them together, then because there are many objects with short survival time, it will inevitably lead to more frequent garbage collection. And garbageAll memory has to be scanned for recycling, but there are some objects that survive for a long time. Scanning them is a waste of time. Therefore, in order to improve the efficiency of garbage collection, zoning is taken for granted.

Another question worth considering is: Why is the default virtual machine configuration, Eden: from: to = 8:1:1?

In fact, this is the result of IBM based on a large number of statistics. According to IBM’s statistics on objects’survival time, they found that 80% of objects had very short survival time. So they set the Eden area to 80% of the younger generation, which reduces the waste of memory space.Improve memory space utilization.

Summary

Java The memory structure of a virtual machine is a must for learning a virtual machine. Among them, the memory model of the Java heap is the most important, because online problems often occur in the Java heap. Therefore, mastering the partition of Java heap and the adjustment of common parameters is the key.

In addition to the six major parts mentioned above, there are also direct memory, stack frames and other data structures in Java. However, because the use of direct memory and stack frames is relatively small, so there is no introduction here, in order to avoid confusion for beginners.

Learn here, a Java file is loaded into memory, and Java class information is stored in our method area. If an object is created, the object data is stored in the Java heap. If you call a method, you will use PC registers, Java virtualAnalog stack, local method stack and so on. So how does the JVM determine the loading order of so many Java classes and how does it control their loading? In the next section, we will talk about the class loading mechanism of JVM.

Leave a Reply

Your email address will not be published. Required fields are marked *