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 does JVM mean?

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

Share

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

This article will explain in detail what JVM means. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

The Java virtual machine (JVM) is a hypothetical computer that can run Java code. As long as you port the interpreter to a specific computer according to the JVM specification, you can guarantee that any compiled Java code will run on that system.

I. compilation, download, interpretation and execution of Java source files

The development cycle of Java applications consists of compiling, downloading, interpreting, and executing. The Java compiler translates the Java source program into JVM executable code-bytecode. This compilation process is somewhat different from that of C / C++. When the C compiler compiles and generates code for an object, the code is generated for running on a particular hardware platform. Therefore, during the compilation process, the compiler converts all references to symbols into specific memory offsets by looking up the table to ensure that the program runs. Instead of compiling references to variables and methods into numeric references and not determining the memory layout during program execution, the J ava compiler keeps these symbolic references in bytecode, and the interpreter creates the memory layout during operation, and then looks up the table to determine the address of a method. This effectively ensures the portability and security of J ava.

The job of running JVM bytecode is done by the interpreter. The interpretation execution process is divided into three parts: the loading of the code, the verification of the code and the execution of the code. The job of loading the code is done by the Class Loader (class loader). The class loader is responsible for loading all the code needed to run a program, including the classes inherited by the classes in the program code and the classes called by it. When a class loader loads a class, the class is placed in its own namespace. There is no other way for classes to affect other classes than to refer to classes outside their own namespace through symbols. All classes on this computer are in the same address space, and all classes imported from the outside have their own independent namespace. This makes local classes more efficient by sharing the same namespace, while ensuring that they do not interact with classes imported from the outside. When all the classes needed to run the program are loaded, the interpreter can determine the memory layout of the entire executable program. The interpreter establishes a corresponding relationship and a query table between a symbol reference and a specific address space. By determining the memory layout of the code at this stage, J ava solves the problem of crashing subclasses caused by superclass changes, and prevents code from illegally accessing addresses.

The loaded code is then checked by a bytecode verifier. The verifier can find a variety of errors, such as Operand stack overflow, illegal data type conversion and so on. After passing the verification, the code begins to execute.

There are two ways to execute Java bytecode:

1. Instant compilation: the interpreter compiles the bytecode into machine code before executing the machine code.

two。 Explain the mode of execution: the interpreter completes all operations of the Java byte code sequence by interpreting and executing a small piece of code at a time.

The second method is usually used. Because the JVM specification is flexible enough, it makes it more efficient to translate bytecode into machine code. For those applications that require high running speed, the interpreter can compile Java bytecode into machine code in real time, thus ensuring the portability and high performance of Java code.

II. JVM specification description

The design goal of JVM is to provide a computer model based on abstract specification, providing flexibility for interpreter developers, and ensuring that Java code can run on any system that conforms to the specification. J VM gives a specific definition of some aspects of its implementation, especially the format of Java executable code, that is, bytecode (Bytecode). This specification includes the syntax and values of opcodes and operands, the numerical representation of identifiers, and Java objects in Java class files, and the storage image of constant buffer pools in JVM. These definitions provide JVM interpreter developers with the information and development environment they need. The designers of Java want to give developers the freedom to use Java at will.

JVM defines five specifications that control the interpretation, execution and implementation of Java code. They are:

JVM instruction system

JVM register

JVM stack structure

JVM fragment recovery reactor

JVM storage area

2.1JVM instruction system

The JVM instruction system is very similar to the instruction system of other computers. The Java instruction is also composed of opcodes and operands. The opcode is an 8-bit binary number, the Operand is immediately following the opcode, and its length varies according to needs. Opcodes are used to specify the nature of an instruction operation (here we describe it in the form of assembly symbols), such as I load for loading an integer from memory, anewarray for allocating space for a new array, iand for the "and" of two integers, and ret for process control to indicate a return from a call to a method. When the length is greater than 8 bits, the Operand is divided into more than two bytes. JVM uses "big endian" encoding to deal with this situation, that is, high-order bits is stored in low bytes. This is consistent with the encoding adopted by Motorola and other RISC CPU, but different from the "little endian" encoding adopted by Intel, that is, the low-order bits is stored in the low-order bytes.

The Java instruction system is designed for the implementation of the Java language, which contains instructions for calling methods and monitoring multi-precedent systems. The length of 8-bit opcodes of Java makes J VM have up to 256 instructions. At present, more than 160 opcodes have been used.

2.2JVM instruction system

All CPU contain a register group that holds the system state and the information required by the processor. If the virtual machine defines more registers, you can get more information from it without having to access the stack or memory, which helps to improve the running speed. However, if there are more registers in the virtual machine than in the actual C PU, it will take up a lot of time for the processor to simulate the registers with conventional memory, which will reduce the efficiency of the virtual machine. In response to this situation, JV M sets only the four most commonly used registers. They are:

Pc program counter

Optop Operand stack top pointer

Frame current execution environment pointer

Vars pointer to the first local variable in the current execution environment

All registers are 32 bits. Pc is used to record the execution of the program. Optop,frame and vars are used to record pointers to the Java stack area.

2.3JVM stack structure

As a computer based on stack structure, JVM stack is the main method for Java to store information. When JVM gets an Java bytecode application, it creates a stack frame for each method of a class in the code to hold the state information of the method. Each stack framework includes the following three types of information:

Local variable

Execution environment

Operand stack

Local variables are used to store local variables used in the methods of a class. The vars register points to the first local variable in the variable table.

The execution environment is used to save the information needed by the interpreter to interpret the Java bytecode. They are the last called method, the local variable pointer, and the top and bottom pointers of the Operand stack. The execution environment is a control center that executes a method. For example, if the interpreter wants to perform I add (integer addition), it must first find the current execution environment from the frame register, then find the Operand stack from the execution environment, pop up two integers from the top of the stack for addition, and finally press the result onto the top of the stack.

The Operand stack is used to store the operands required by the operation and the results of the operation.

2.4JVM fragment recovery reactor

The storage space required by an instance of the Java class is allocated on the heap. The interpreter is specifically responsible for allocating space for class instances. After allocating storage space to an instance, the interpreter begins to record the use of the memory area occupied by the instance. Once the object is used, it is recycled into the heap.

In the Java language, there is no other way to request and free memory for an object other than new statements. The work of releasing and reclaiming memory is undertaken by the Java operating system. This allows the designers of the J ava operating system to decide on their own how to recycle the fragments. In the Java interpreter and Hot Java environment developed by SUN, fragment collection is performed in the form of background threads. This not only provides good performance for the running system, but also frees programmers from the risk of controlling the use of memory.

2.5JVM storage area

JVM has two types of storage: constant buffer pools and method areas. Constant buffer pools are used to store class names, method and field names, and string constants. The method area is used to store the bytecode of the Java method. The specific implementation of these two storage areas is not clearly defined in the J VM specification. This makes the storage layout of Java applications must be determined during operation, depending on how the specific platform is implemented.

JVM is a platform-independent specification defined for Java bytecode, which is the basis of Java platform independence. There are still some limitations and deficiencies in the current JVM, which need to be further improved, but in any case, the idea of JVM is successful.

Comparative analysis: if we think of the Java original program as our C++ original program, the bytecode generated by the compilation of the Java original program is equivalent to the 80x86 machine code (binary program file) compiled by the C++ original program, the J VM virtual machine is equivalent to the 80x86 computer system, and the Java interpreter is equivalent to 80x86CPU. Machine code runs on 80x86CPU and Java bytecode runs on the Java interpreter.

The Java interpreter is equivalent to a "CPU" running Java bytecode, but the "CPU" is not implemented in hardware, but in software. The Java interpreter is actually an application under a specific platform. As long as the interpreter program under a specific platform is implemented, Java bytecode can be run on that platform through the interpreter program, which is the foundation of Java cross-platform. At present, there are not corresponding Java interpreter programs on all platforms, which is why Java can not run on all platforms. It can only run on platforms where Java interpreter programs have been implemented.

This is the end of the article on "what does JVM mean?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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