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

What is the JVM architecture?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces how the JVM architecture is, which is very detailed and has a certain reference value. Friends who are interested must finish it!

Let's start with a JVM architecture diagram:

1) Runtime data area: the compiled bytecode file (class file) is loaded by class loader (class loading subsystem) and handed over to the execution engine for execution. The data generated during the execution of the engine is stored in a memory area. This area of memory is the runtime area.

2) Program counter: used to record the location of the executing bytecode instruction of the current thread. Because the multithreading of the virtual machine is implemented by switching threads and allocating cpu execution time, the execution locations of different threads need to be recorded, so the program counter is thread private.

3) Virtual machine stack: virtual machine stack is the memory structure of java method execution. Virtual machine creates a "stack frame" when each java method executes, which is used to store local variable table, Operand stack, dynamic link, method exit and other information. When the method is executed, the stack frame is removed from the virtual machine stack. The local variable table contains basic data types and object references.

In the java virtual machine specification, two exception states are specified for this area: if the depth of the stack requested by the thread is greater than the depth allowed by the virtual machine, a StackOverFlowError exception (stack overflow) will be thrown if the virtual machine stack can be dynamically expanded (most java virtual machines can now be dynamically extended, but the java virtual machine specification also allows a fixed-length java virtual machine stack) If sufficient memory space cannot be applied for when expanding, an OutOfmMemoryError exception will be thrown (not enough memory)

4) Local method stack: the execution of java method has virtual machine stack, and the execution of local method should have local method stack.

5) method area: used to store class information, constants, static variables, compiled code and other data that have been loaded by the virtual machine. Thread sharing (just look at the stored data)

The java virtual machine specification has very loose restrictions on the normal zone, and you can choose not to implement garbage collection except that you don't need contiguous memory and can choose to be fixed or scalable like the java heap. Garbage collection is relatively rare in this area, but it is not as permanent as the name of the permanent generation when the data enters the method area.

The important goal of memory recovery in this area is to reclaim constant pools and unload types. Generally speaking, the 'results' of memory recovery in this area are not satisfactory. In particular, the unloading conditions of the type are very stringent, but this part of the recycling is indeed necessary. Several serious bug that have appeared in sun's bug list are memory spills caused by unfinished recycling of this area by an earlier version of the HotSpot virtual machine.

6) java heap (java Heap): the main function of heap is to store object instances created during program running, because there may be a large number of object instances to be stored, so it is also the largest block in virtual machine memory management. And because of the limited hardware conditions, it is necessary to constantly collect "useless" instance objects to make room for newly generated instance objects; therefore, java garbage collection is mainly for heap collection (and constant pool in the method area), java heap is often called GC heap (Garbage Collected Heap).

7) Class loading mechanism (Class Loader): the class loading subsystem loads the binary stream of a class into memory according to the fully qualified name of a class. A meta-information object (method area) describing the structure of Class will be formed in JVM. Through this meta-information object, we can know the structure information of Class, such as constructors, properties and methods. Java allows users to call the function of Class object indirectly through this Class-related meta-information object.

good! After saying so many keywords, let's give an example to explain these keywords:

a. Figure 1 is the HelloWorld.java we wrote, through IDE or command: javac HelloWorld compiled to generate hexadecimal HelloWorld.class (bytecode file, see figure 3), want to read hexadecimal byte can refer to: a text to let you understand java bytecode; but the general IDE will automatically translate into figure 2 instructions; or through the command: javap-verbose HelloWorld for translation.

(figure 1) HelloWorld.java

(figure 2) HelloWorld.class

(figure 3) hexadecimal bytecode:

b. Then, when we run the class file through IDE or the command: java HelloWorld, the bytecode file (class file) is loaded through the class loading mechanism and delivered to the execution engine for execution The class loading mechanism loads the information of the HelloWrold class, static variables (not added in the example), constants (not added in the example, constants will be loaded into the constant pool in the method area, which is different from static variables) into the method area. Next, if you need to create objects of this class, you need to find class-related information in the method area through the parameters followed by new.

c. After the class is loaded, the virtual machine checks the entry of the program, and the execution entry of the program in the virtual machine is the main function, such as HelloWorld.class, where the execution engine finds the main function to start executing instructions and generates a "frame stack" to the top of the virtual machine stack. We can see (figure 2) the command under the main method: 0 new java.lang.StringBuilder [16] means to create a String object, and the created String object instance allocates memory storage in the java Heap (the creation process of Java objects in JVM can be seen in this article: how Java objects are created (learn about JVM memory structure through object creation)) And record the instruction position "0" to the program counter of the current thread. 3 dup then pushes the reference of the object into the virtual machine stack and records the instruction position "3" to the program counter of the current thread; 4 ldc [18] loads the string constant Hello from the string constant pool (starting from jdk1.7, the string constant pool is moved to the java heap), and updates the instruction position to the program counter. If there is an instruction of a local method during execution, it will enter and exit the stack in the local method stack. Note here, please see the location of the main function instruction 16: 16 new java.lang.StringBuilder [31] A StringBuilder object has been created here. This type of string concatenation has been optimized since jdk5, which is supplemented by Google.

d. During the execution of instructions by the execution engine, the local library interface is called as needed to execute local library methods, such as new instructions, output screens, etc.

These are all the contents of the article "what is the JVM Architecture?" Thank you for reading! Hope to share the content to help you, more related knowledge, 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