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 are the knowledge points of Java compilation and interpretation execution

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the Java compilation implementation and interpretation of the implementation of the knowledge points of what the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, with a certain reference value, I believe that you will read this Java compilation and interpretation of the implementation of the knowledge points of what articles will have a harvest, let's take a look at it.

I. Preface

Programming languages are divided into low-level languages and high-level languages. Machine language and assembly language are low-level languages, while C, C++, java and python are high-level languages.

Machine language is the lowest language, which can be executed directly. The source code we write is human language, the computer can only recognize some specific binary instructions, and the source code must be converted into binary instructions before the program is really run. The assembly language is translated into machine instructions by the assembler and executed. An assembly instruction corresponds to a machine instruction.

Programs programmed in high-level languages can be executed in three ways:

1. One is compilation and execution, in which the source program is first translated into machine instructions through the compiler (responsible for translating the source program into target machine instructions), and then executed through compilation-> link-> target executable file; that is, all source code is converted into binary instructions in advance, that is, an executable program is generated. For example, languages such as CPowerCraft + are compiled and executed.

two。 One is to interpret the execution, is to use the interpreter to interpret a sentence of our code into binary code that can be recognized by the machine, which can be thought of as explaining a sentence and executing a sentence. During this process, no intermediate files are generated. For example, the script mode is a series of commands, which are translated into machine-recognizable instructions by the interpreter of the system when executed. For example, the shell script is executed by the shell program, and the js is interpreted and executed by the browser.

3. The last one is a combination of compilation and interpretation, so let's talk about Java.

2. Understand several compilers of Java

Front-end compiler: converts .java files into .class files. Including Javac of Sun and incremental editor (ECJ) of Eclipse JDT

Back-end runtime just-in-time compiler (JIT compiler, Just In Time Compiler): converts bytecode to machine code. Including C1 and C2 compilers of HotSpot VM

Static advance compiler (AOT compiler, Ahead Of Time Compiler): compiles * .java into native machine code. Including GNU Compiler for the Java (GCJ), Excelsior JET

Third, Java adopts a mixed mode of interpretation and compilation.

During the compilation period, by compiling the source code to .class, combined with the cross-platform abstraction of JVM, we shielded the difference between the underlying computer operating system and hardware, and realized "compile at once, run everywhere". At run time, the current mainstream JVM is mixed mode (- Xmixed), that is, interpreting and compiling runs are used together.

Java was initially positioned as a "interpretive execution" language, but now mainstream virtual machines include a just-in-time compiler JIT.

The program goes through the stages from source code to running: java program-- (compiling javac)-- > bytecode file .class-- > class loading subsystem as reflection class Class--- > runtime data area-- > (interpreting execution + JIT compiler compilation)-- > operating system (Win,Linux,Mac JVM).

.class files are files that can be run everywhere. The Java bytecode is then converted to the target machine code, which is executed by JVM, the second compilation of Java.

Java uses a mixed mode of interpretation and compilation: the interpreter interpreter in the JVM-based execution engine coexists with even the compiler JIT

The execution engine obtains that javac compiles the source code into a bytecode file class.

It is then converted to the final machine code at run time through the interpreter interpreter. (interpretive)

In addition, the JVM platform supports a technology called instant compilation. The purpose of just-in-time compilation is to prevent the function from being interpreted and executed, but to compile the whole function into machine code, which can greatly improve the execution efficiency (direct compilation).

JIT converts the bytecode into the final machine code

Take the HotSpot virtual machine provided by Oracle JDK as an example, in the HotSpot virtual machine, two compilation modes are provided: interpretive execution and just-in-time compilation (JIT,Just-In-Time).

Interpretation execution translates bytecode into runnable machine code one by one, while just-in-time compilation translates bytecode into machine code on a method-by-method basis ("compilation execution" mentioned above). The advantage of the former is that it does not have to wait, while the latter is more efficient in actual operation.

The significance of just-in-time compilation is that it is one of the important means to improve program performance. According to the "28 Law" (that is, 20% of the code accounts for 80% of the system resources), for most of the less commonly used code, we do not need to take time to compile it into machine code, but to interpret and run it one by one. For some hot code that occupies only a small part (which can be regarded as important code that executes repeatedly), it can be translated into machine code that conforms to the machine to execute efficiently and improve the efficiency of the program, which is the real-time compilation at run time.

In order to meet different scenarios, the HotSpot virtual machine has built-in several just-in-time compilers: C1 Magi C2 and Graal. Graal is an experimental just-in-time compiler officially introduced by Java10, which will not be described here (actually, I don't know much about it, awkward). Take a look at C1 and C2 first. I believe we have been in contact with each other more or less.

C1: Client compiler, for client-side GUI programs that require startup performance, the optimization method is relatively simple, so the compilation time is short.

C2: that is, the Server compiler, for server programs with peak performance requirements, uses complex optimization methods, so the compilation time is long, but the performance is better in the process of running.

Starting from Java7, the HotSpot virtual machine adopts hierarchical compilation by default: the hot methods are first compiled by C1 compiler, and then the hotspots in hot methods are further compiled by C2, and the better compilation optimization is calculated according to the previous run. In order not to interfere with the normal operation of the program, JIT compiles in extra threads, and HotSpot allocates C1 and C2 threads at 1:2 according to the resources of the actual CPU. In the case of sufficient computer resources, the bytecode interpretation run and compilation run time can be carried out at the same time, and the machine code after JIT compilation execution will be started the next time the method is called, replacing the original interpretation execution (meaning that the more efficient machine code has been translated, naturally replacing the original relatively inefficient execution method).

Above, we can see that in Java, just-in-time compilation (compile execution) plays an important role in Java performance optimization, so it is time to say that Java is a co-existence of interpretive execution and compilation execution, at least for the most part.

IV. Comparison between compilation and interpretation

1. The compilation of a program is a waste of time, and it has to be recompiled when it is migrated to other platforms, but the executable files generated after compilation run fast.

two。 Interpretive programs can be executed across platforms and can be run in time without compiling all the code, but the final running speed is not as fast as compiled programs because it is interpreted and executed one by one.

3. Memory usage: compiled execution requires the generation of compiled machine code files, while interpretation execution is interpreted sentence by sentence, so interpretation execution takes up less memory.

Disadvantages of using the interpreter alone

Abandons the performance advantages that JIT may bring. If the code is not compiled by JIT, it needs to be parsed repeatedly when it is run again.

Disadvantages of using the JIT compiler alone

All the code needs to be compiled into native machine code. To take more time, JVM startup will be much slower

Increase the length of executable code (bytecode is much smaller than JIT-compiled machine code), which results in page scheduling, which slows down the program.

Some optimization methods of JIT compilers, such as branch prediction, often cannot be optimized effectively without profiling.

Therefore, HotSpot adopts the practice of lazy assessment (Lazy Evaluation). According to the 2008 law, only a small part of the code (hot code) consumes most of the system resources, and this is the part that JIT needs to compile. JVM collects information based on each execution of the code and makes some optimizations accordingly, so the more times it is executed, the faster it will be.

JDK 9 introduces a new compilation mode, AOT (Ahead of Time Compilation), which compiles bytecodes directly into machine code, thus avoiding the overhead of JIT warm-up and so on. JDK supports hierarchical compilation and AOT collaborative use.

Note: JIT is method-level and caches compiled bytecode in CodeCache without being repeatedly interpreted.

This is the end of the article on "what are the knowledge points of Java compilation and interpretation execution?" Thank you for your reading! I believe you all have a certain understanding of "what are the knowledge points of Java compilation and interpretation execution". If you still want to learn more knowledge, 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

Development

Wechat

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

12
Report