Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Understanding the implementation method of memory allocation of JVM on HotSpot virtual machine from the perspective of computer composition

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is mainly about "understanding the implementation of memory allocation of JVM on HotSpot virtual machine from the perspective of computer composition". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "from the perspective of computer composition to understand the implementation of JVM memory allocation on the HotSpot virtual machine"!

As can be seen from the above figure, in the JDK8 version, the JVM memory model in a narrow sense is divided into program counters, local method stacks, virtual machine stacks and heaps, of which the first three are thread private and the last heap is thread shared (so GC occurs mainly on the heap). In a broad sense, the JVM memory model also contains part of the local memory, which can be divided into meta-space and direct memory.

1. Program counter Program Counter Regiter

Thread private

No OutOfMemoryError exceptions are defined

The program counter, referred to as PC Register for short, is essentially different from the register of CPU. The program counter of JVM is not independent hardware, but only a small piece of memory space, which stores the offset of the JVM instruction being executed by the current Java thread to the start of the method. The program counter has a value only if the current java thread executes the java method, and when the local method is executed, the value is: undefined.

From the point of view of the composition of the computer, what is stored in the program counter is an integer, which represents the instruction in which the currently executing JVM instruction is offset backward relative to the beginning of the method.

Why is a program counter also called a line number indicator

When decompiling class files, for the better, the decompiler will format the decompiled instructions. After formatting, one line is an instruction, and there is something that looks like a "line number" in front of each line, and this line number is stored in the program counter, so it becomes a "line number indicator". But in fact, what looks like a "line number" is essentially the offset of an instruction.

two。 Local method stack Native Method Stack

The local method stack is essentially similar to the virtual machine stack, but it is used to run Native methods. Don't make too many introductions.

3. Virtual machine stack VM Stack

Thread private

An OutOfMemoryError exception is defined

Virtual machine stack is an important part of Java virtual machine, and the operation of the method depends on it. A method call is a "stack frame" in the virtual machine stack, and the bottom of the stack is the stack frame of the main method. The stack frame is mainly composed of local variable table, Operand stack, dynamic link, return address (also called method exit) and other additional information. A method call in the java file corresponds to a stack entry and exit in the virtual machine stack.

3.1 Local variable scale Local Variable Table

The local variable table is the storage space of the local variables in the participating method of the current method. The maximum capacity of the local variable table is determined according to the max_locals attribute of the method when the class file is compiled. The basic unit of the local variable scale is the variable slot (Variable Slot, generally referred to as Slot). The Java virtual machine specification stipulates that "a variable slot takes up 32 bits of space". The 32-bit space can cover most of the basic data types of Java, and two of the rest (for example, long and double) can be used for more than one variable slot, but the program will check during compilation if you need to access two variable slots at once (for example, read a value of type double alone). It is not allowed to read only one of them in any way, and if this happens, an error will be reported during the compilation phase.

The virtual machine uses the local variable table by index positioning, but in practice, the index starts from 1. The 0 index bit of the local variable table stores the reference of the this key to the object instance to which the current method belongs, that is, a memory address in the heap space.

From the point of view of computer composition, a local variable meter is a continuous space in memory that determines the size of the byte space, which is an integral multiple of 32 or 64.

3.2Operand stack Operand Stackint i = 1 + 2

For example, in the above code, the two numbers 1 and 2 and the result 3 of their addition are stored in the Operand stack, but as the program runs, the two numbers 1 and 2 will enter the stack successively. Corresponding to the contents recorded in the program counter have changed twice (the first is the number "1" into the stack, the second is the number "2" into the stack. When the program counter continues to record the offset of the next instruction, the numbers "1" and "2" in the Operand stack go off the stack, and the resulting number "3" added by the two numbers goes into the stack.

From the point of view of computer composition, the Operand stack is a physically discontinuous but logically continuous memory space that occupies an integer multiple of 32 or 64 bytes.

3.3 dynamic connection Dynamic Linking

Dynamic linking, also known as reference, stores a reference to the method that executes the stack frame in the runtime pool to indicate which method the current stack frame belongs to.

From the point of view of computer composition, the dynamic connection part stores a memory address.

3.4 return address Return Address

The information recorded in the return address is the value of the program counter corresponding to the last method stack frame when this method was called, in order to resume the method that called the current method at the end of the current method execution (normal execution or exit when an exception is encountered).

The value in the return address is used to resume the execution of the previous method when the method ends normally (no exception has been thrown or the exception has been thrown but catch has been dropped in the current method). When the method exception ends (the exception is thrown without catch), the exception handler will use the convenient exception stack and use the information in the return address to build an "exception stack".

From the point of view of the composition of the computer, the content stored in the return address is the same as recorded in the program counter, which is an integer.

What happens when a method is called through an object instance

First of all, through the metadata reference of the object, find the metadata of the object, and find the corresponding method.

Then create the stack frame of the new virtual machine stack, copy the contents of the current program counter to the return address of the new stack frame, and write the reference of the current method in the dynamic connection of the new stack frame.

If the new method requires input parameters, and the value of the input parameter is exactly at the top of the current method stack frame Operand stack, the data at the top of the Operand stack is used as part of the local variable table of the new method stack frame (possibly copying data, may be memory address sharing, depending on the specific virtual machine implementation).

Clear the program counter and start the execution of the new method.

4. Heap Heap

Thread sharing

An OutOfMemoryError exception is defined

Object obj = new Object ()

Heap is the largest area of memory in JVM, and it is also the most concerned area of GC manager. What is stored in the heap is the object instance. Taking the above code as an example, the memory space opened by "new Object ()" is mainly in the heap.

Object instances are first assigned to the Eden area of the new generation. If there is not enough space in the Eden area to accommodate new object instances, a Minor GC will be triggered, and objects that are still in use will be transferred to Survivor. When they survive more than the number of first-class Minor GC, that is, when the age of the object reaches a certain value, it will be transferred to the old age.

From the point of view of the composition of the computer, what is stored in the heap is the information such as the objects and all their variables generated in the running of the program.

5. Metadata area Meta Space

The metadata area is a new addition in JDK8 and an implementation of the description of the "method zone" in the Java virtual machine specification. JDK7 and its previous, use "permanent generation" to implement the method area, but in practical use, it is found that there are problems in GC management and performance, and Oracle uses the metadata area to implement the method area in order to integrate Hotspot and JRockit and assemble their advantages.

From the point of view of the composition of the computer, what is stored in the metadata area is the information of the class file and the direct reference memory address information after parsing some symbol references in the class.

6. Direct memory Direct Memory

Direct memory, after the introduction of NIO by JDK1.4, the memory used by NIO-based MMap is located in direct memory.

From the point of view of the composition of the computer, the data content is stored in the direct memory.

Test whether the string constant pool is in the heap or in the metadata area

Using the code to constantly create new strings, the results are shown in the following two diagrams. The heap space is changing, but the metadata area is almost unchanged. It can be concluded that the string constant pool is in the heap.

At this point, I believe you have a deeper understanding of "understanding the implementation of JVM memory allocation on the HotSpot virtual machine from the perspective of computer composition". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report