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

How to learn more about Java virtual machine memory

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

Share

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

In this issue, the editor will bring you an in-depth understanding of Java virtual machine memory. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Before discussing JVM memory region analysis, let's take a look at the specific execution process of the Java program:

The execution process of Java program: Java source code file (.Java file)-> Java Compiler (Java compiler)-> Java bytecode file (.class file)-> class loader (Class Loader)-> Runtime Data Area (runtime data)-> Execution Engine (execution engine). Today, let's analyze the Runtime Data Area (runtime data) of the execution process of the Java program, so what are the parts of the runtime data area?

Program counter (Program Counter Register)

Java virtual machine stack (VM Stack)

Local method stack (Native Method Stack)

Method area (method area)

Heap (heap)

Program counter

It is used to indicate which instruction the program executes, which is logically the same as the program counter of assembly language. According to the JVM specification, if the thread is executing a non-native method, the address of the instruction that currently needs to be executed is stored in the program counter, and if the thread is executing the native method, the value undefined in the program counter. Each thread has its own independent program counter. Why? Because in multithreading, a CPU kernel will only execute instructions in one thread, so in order to enable each thread to return to the position where the program was executed before the switch, each thread has its own independent program counter.

2. Java virtual machine stack Java virtual machine stack stores stack frames one by one. When the program executes a method, it will create a stack frame and press it into the stack. When the method is executed, the stack frame will be removed. What we mean by "stack" refers to the Java virtual machine stack, which includes: local variable table, Operand stack, dynamic connection, method return address, additional information 1. The local variable table is mainly the local variables in the storage method, including the information of the local variables in the method and the parameters of the method. For example, various basic data types (boolean, byte, char, short, int, float, long, double), object reference (reference type, which is not equivalent to the object itself, may be a reference pointer to the starting address of the object, or it may point to a handle that represents the object or other locations associated with the object) and the returnAddress type (the address that points to a bytecode instruction) Of these, 64-bit data of long and double types takes up two local variable spaces (Slot), while the rest of the data types occupy only one. The size of the local variable table can be determined by the compiler, so the size of the local variable table will not change during program execution. In the Java virtual machine specification, two exceptions are specified for this area: if the stack depth requested by the thread is greater than the depth allowed by the virtual machine, a StackOverflowError exception will be thrown; if the virtual machine stack can be dynamically expanded (most of the current Java virtual machines can be dynamically expanded, but the Java virtual machine specification also allows fixed-length virtual machine stacks), if enough memory cannot be applied for during expansion, an OutOfMemoryError exception will be thrown. The Operand stack virtual machine takes the Operand stack as its working area, and all the calculations in the program are completed with the help of the Operand stack, from which most instructions pop up the data, perform the operation, and then press the result back into the Operand stack. Each stack frame of a dynamic connection contains a reference to the method to which the stack frame belongs in the runtime constant pool (to the runtime constant pool: constants in the class may be needed during method execution). This reference is held to support the dynamic connection method return address during method invocation when a method is executed, it is necessary to return to the place where it was called. Therefore, a method return address must be saved in the stack frame. The additional information virtual machine specification allows a specific virtual machine implementation to add some information not described in the specification to the stack frame, such as highly relevant information, which depends entirely on the specific virtual machine implementation. In the actual development, the dynamic connection is generally considered, and the return address of the method and other additional information are all classified into the same category, which is called stack frame information. Local method stack,

The role of the local method stack (Native Method Stack) is very similar to that of the virtual machine stack, except that the virtual machine stack performs Java methods (that is, bytecode) services for the virtual machines, while the local method stacks serve the Native methods used by the virtual machines. Like the virtual machine stack, the local method stack area throws StackOverflowError and OutOfMemoryError exceptions.

4. Heap

In C language, programmers can request and free space on the heap through the malloc function and the free function. So what's it like in Java? The heap in Java is used to store objects themselves and arrays (of course, array references are stored in the Java stack), where almost all object instances allocate memory. In Java, programmers basically don't have to worry about space release, and Java's garbage collection mechanism handles it automatically. In addition, the heap is shared by all threads, and there is only one heap in JVM.

5. The method area (Method Area), like the Java heap, is a memory area shared by each thread. It is used to store class information, constants, static variables, and compiled code that has been loaded by the virtual machine. The runtime constant pool (Runtime Constant Pool) is part of the method area. In addition to the version, field, method, interface and other description information of the class in the Class file, another piece of information is constant pool (Constant Pool Table), which is used to store all kinds of literals and symbol references generated during compilation. This part of the content will be stored in the runtime pool of the method area after the class is loaded. In the JVM specification, there is no mandatory requirement that the method zone implement garbage collection. Many people are used to calling the method zone "permanent generation" because the HotSpot virtual machine implements the method area with a permanent generation, so that JVM's garbage collector can manage this area as if it were a heap area, so there is no need to design a garbage collection mechanism specifically for this part. Since JDK7, however, the Hotspot virtual machine has removed the runtime pool from the permanent generation. Note: the native method refers to the method written in a language other than Java. This is what the editor shares with you about the memory of the Java virtual machine. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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

Internet Technology

Wechat

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

12
Report