Java code you write is a quick run up?

If you are a Java developer, you must specify the Java code, there are many different operating modes. For example, you can run the development tools (IDEA, Eclipse, etc.), you can double click to execute the jar file to run, you can run the command line, or even (such as various OJ) running on the page. Of course, these are inseparable from the implementation JRE (Java Runtime Environment).

The JRE includes the necessary components to run Java programs, including the JVM (Java Virtual Machine) and Java core libraries and so on. Java programmers often come into contact with the JDK (Java Development Kit) also contains the JRE, and also comes with a series of development and diagnostic tools.

This article mainly discuss with everyone for the following two questions:

  1. Why JVM?
  2. JVM is how to run Java code it?

Why JVM?

Java is a very important feature and platform independence, and using the JVM is the key to achieving this characteristic. Java as a high-level programming language, grammar complex, high level of abstraction. Therefore, such a complex program run directly on the hardware, not reality. So before running Java programs, we need to convert them.

A Java-oriented design characteristics of a virtual machine, and converted into a sequence of instructions that can be identified by a virtual machine (Java program by a compiler as Java bytecode opcode instruction (opcode) is fixed to one byte, it is also called Java bytecode).

JVM software is generally provided at all existing platforms (such as Windows, Linux), so you can make once a program is converted into Java byte code, then the virtual machine can be implemented on different platforms in operation (write once, run anywhere).

Another advantage is the JVM with hosting environment (Managed Runtime), instead of the hosting environment can be tedious and error-prone process some of the code portion, including the automatic memory management and garbage collection (GC).

In addition, the managed environment also provides features such as array bounds, dynamic type of motion detection, security permissions, and so on, so we write the code from those unrelated business logic.

JVM is how to run Java code it?

JVM specifically how to run Java bytecode it? Here we take a look at:

From the point of view JVM, Java code executes first need to load it compiled from the class file into the JVM. After loading the Java class is stored in the method area (Method Area) in. Actual operation, JVM will execute the method code area.

JVM will be divided in a heap and stack memory to store runtime data, broken down into Java JVM will stack method for Java stack method, native methods for the local method (written in C ++ native method) of the stack, and each store PC register thread execution location.

In operation, whenever a Java method call enters, the JVM stack frame is generated in the Java stack of the current thread in a method for storing local variables and the operand byte code. The size of the stack frame is computed in advance, and does not require the JVM stack frame memory continuously distributed in space.

When you exit the method currently executing, regardless of returns is normal or abnormal return, JVM will eject the current stack frame for the current thread, and it discarded.

From a hardware perspective, Java byte code can not be executed directly. Thus, JVM needs to translate the byte codes into machine code.

HotSpot in which the translation process in two forms: the first is interpreted (Interpreter), i.e., one by one translate the byte codes into machine code and execute; second is time compiler (Just-In-Time compilation, JIT ), all bytecode is about a method comprising compiled into machine code before execution.

The former has the advantage of having to wait compiler, while the latter has the advantage of actually run faster. HotSpot default mixed mode, combines the advantages of both interpretation and execution time compilation. It will first interpreted bytecode, and which after repeated hot code to the method as a unit-time compilation.

Java code to perform the entire process is as follows:

  1. Javac .java source files using the compiled bytecode (.class file name suffix)
  2. After the bytecode JIT environment variable is determined, whether hot codes (multiple loop or method call)
  3. Hot JIT-compiled code is executable machine code
  4. Non-hotspot Code all interpreted using an interpreter bytecode

Which, during operation will be in-time compilation of hot There are two types of code:

  1. The method is called multiple times
  2. The loop is executed twice

As compiled object for the first class, the compiler will be the overall process, which is the standard way JIT compiler. For the second type of starting body of the loop, but still to the compiler to compile the overall process as an object, as occurs during execution of the method, referred to as the stack replacement.

HotSpot uses a variety of techniques to improve starting performance and peak performance, real-time compiler just mentioned is one of the most important technology.

Time compilation based on the assumption of eight law in line with the program, which is twenty percent of the code occupies eighty percent of computing resources.

For less frequently used code that occupies most of, we do not need time-consuming to compile it into machine code, but rather take the form of interpretive execution of operation; the other hand, occupy only a small portion of the hot spots of code, we can compile it into machine code, in order to achieve the desired speed.

In order to meet the needs of different user scenarios, HotSpot plurality of built-in-time compiler: C1, C2. The reason why the introduction of a plurality of in-time compiler that is used to trade-off between efficiency of the generated code and compile time.

  • C1 (Client compiler) is required for the performance of the client to start the GUI program to optimize use of a relatively simple means, and therefore short compilation time.
  • C2 (Server compiler) oriented server-side program is required for peak performance, optimizing means employed is relatively complex, long compilation time, but higher efficiency of the generated code.

Starting with Java 7, HotSpot default stratified compiled by: hot first compiled method C1, and then hot hot process will be further compiled C2.

In order not to interfere with the normal operation of applications, instant HotSpot compiler is compiled on the additional thread made. HotSpot compiler sets the number of threads depending on the number of CPU, and a 1: 2 ratio configuration C1 and C2 to the compiler.

In the case of sufficient computing resources, and instant interpreted, compiled bytecode can be conducted concurrently. Enabled machine code will call the method after the next compilation, to replace the original interpreted.

A piece of code which determines whether the code is hot, is not it needs to trigger in-time compilation, this behavior is called Hot Spot Detection (Hot Spot Detection), the detection algorithm in two ways:

  1. Top of the stack for each thread to check virtual machine cycle, if certain methods often appear in the top of the stack, this method is hot Methods: Based on a sampling of Hot Spot Detection (Sample Based Hot Spot Detection). The advantage is simple, efficient and very easy to get called relational approach. The disadvantage is difficult to confirm reduce method, vulnerable to thread blocks or other external causes disturbed.
  2. Establishing a counter for each method (and even block), the execution count exceeds a threshold value are considered hot Method: Based on the focus detection counter (Counter Based Hot Spot Detection). The advantage is rigorous and accurate statistical results. The disadvantage is that to achieve trouble, can not get a direct call to the relational approach.

HotSpot using a second - counter-based detection of hot spots, and there are two types of counters: method calls counter (Invocation Counter) and the back side of the counter (Back Edge Counter).

to sum up

This article describes why JVM and the JVM is how to run Java code.

Why JVM:

  1. It provides portability. Once compiled, executed everywhere.
  2. Providing code hosting environment, instead of part of the processing portion tedious and error-prone.

JVM runtime memory area is divided into five parts, namely, a method area, stack, PC register, Java native method stacks and stack methods. Java class files compiled from the program, you need to load into the method area in order to work in the JVM.

In order to improve operational efficiency, HotSpot virtual machine is a mix of policy enforcement, it will explain the execution of Java byte code, which will then focus the code executed repeatedly to the method as a unit in-time compilation, direct translation into machine code It runs on top of the underlying hardware.

HotSpot loaded with a plurality of different time compiler to make trade-offs between efficiency and generated code compile time.

Focus detection algorithm is determined based on the sampling code comprising two kinds and based on a counter, the HotSpot based hot spot detection counter, the counter and the counter is divided into method calls back to the counter edge.

Guess you like

Origin www.cnblogs.com/wupeixuan/p/11790091.html