Why Java is running in a virtual machine

Source: Internet
Author: User

Why Java is running in a virtual machine

    1. Simply put, Java as a high-level programming language, syntax complex, high degree of abstraction, can not be directly translated into machine code to run on the machine, so the designer designed the virtual machine, through the compiler to convert the Java program to the virtual machine can recognize the script, that is, Java bytecode, Java bytecode is translated by the virtual machine into machine code that can be recognized by the machine. This also implements the Java cross-platform
    2. The second benefit of a virtual machine is a managed environment that can replace the redundant and error-prone parts of our code, such as automatic memory management, garbage collection. There are dynamic monitoring such as array out-of-bounds, dynamic types, security permissions, and so on.

How Java runs in a virtual machine

    1. From a virtual machine perspective, executing Java code First needs to load the class file it compiles into a virtual machine. The loaded Java class is stored in the method area, and the virtual machine executes the code in the method area when it is run

How a Java Virtual machine stores data at run time

The Java Virtual machine divides the heap and stack in memory to store run-time data. The stack is subdivided into Java method Stacks for Java methods, local method stacks for local methods, and PC registers that store the execution locations of each thread.

During the run, whenever a method is called, the Java Virtual machine generates a stack frame in the Java method stack of the current thread, storing local variables and byte-code operands. This stack frame size is calculated in advance, and Java virtual machines do not require stack frames to step in memory space. When you exit the execution method, The Java Virtual opportunity pops up the current stack frame of the current thread and discards it.

In front of the Java bytecode will be translated into machine code by virtual machine, that is how to translate it? There are two forms: the first is the interpretation of execution, that is, the translation of bytecode into machine code and execution, the advantage is not waiting to compile. The second is instant compilation (JIT), That is, all the bytecode contained in a method is compiled into a machine code and then executed, the advantage is that the actual running speed is very fast. And our popular hotspot virtual machine defaults to mixed mode, which combines the advantages of both interpretation execution and instant compilation, explaining the code of execution byte code and then executing the hot spot repeatedly. The immediate compilation is done in a method.

How efficient is the operation?

The first thing to say is that compiling is to improve performance and peak performance, it is based on the assumption that the program conforms to the 28 law, that is, 20% of the code occupies 80% of the computing resources.

How to understand? For most of the code that is not commonly used, it does not take time to compile it into machine code, but rather to run it in a way that interprets execution; On the other hand, for a small number of hot code, the compiler into machine code, improve the speed of operation.

The HotSpot includes multiple instant compilers: C1, C2, and Graal. Graal is

C1, also known as the client compiler, is oriented to the requirements of the startup performance GUI program, the use of optimization means relatively simple, so the compilation time is short.

C2, also known as the server compiler, is designed for server-side programs with peak performance requirements, which are relatively complex to use, and therefore have a longer compile time, while generating code is more efficient to execute.

Java7 start, hotspot by default to take a layered compilation: hot-Spot method is first compiled by C1, and then the hotspot in the hot method will be further C2 compiled.

Summarize

The reason to run in a virtual machine is because it provides portability. Once Java code is compiled into Java bytecode, it can be run on a Java Virtual machine implementation on a different platform. In addition, the virtual machine also provides a code-hosting environment,
Instead we deal with some tedious and error-prone transactions, such as memory management.

The Java Virtual machine divides the run-time memory area into five parts, namely the method area, heap, PC Register, Java method stack, and local method stack. The Java program compiles a class file that needs to be loaded into the method area before the Java Virtual machine
Run in.

To improve operational efficiency, the HotSpot virtual machines in the standard JDK adopt a hybrid execution strategy.

It interprets the execution of the Java bytecode and then executes the hot-spot code, which is executed on the fly, in the same way as the machine code, and runs directly above the underlying hardware.

The HotSpot loads a number of different instant compilers to make a trade-off between compile time and the execution efficiency of the generated code.

Why Java is running in a virtual machine

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.