Home  >  Article  >  Java  >  How does java code run

How does java code run

(*-*)浩
(*-*)浩Original
2019-11-13 09:24:281878browse

How does java code run

#How does the Java virtual machine run Java bytecode?

From the perspective of the virtual machine, executing java code first requires loading the compiled class file into the java virtual machine. The loaded java class will be stored in the method area. When actually running, the virtual machine executes the code in the method area. (Recommended learning: java course)

How does java code run

During the running process, whenever a java method is called, the java virtual opportunity Generate a stack frame in the java method stack of the current thread to store local variables and bytecode operands. The size of this stack frame is calculated in advance, and the Java virtual machine does not require stack frames to be continuously distributed in the memory space.

How does java code run

In hotspot, the above translation process has two forms: the first is interpretation and execution, that is, translating bytecode into machine code one by one and executing it; The second is just-in-time compilation (JIT), which compiles all the bytecode contained in a method into machine code before executing it.

The advantage of the former is that there is no need to wait for compilation, while the advantage of the latter is that it actually runs faster. HotSpot adopts hybrid mode by default, which combines the advantages of interpreted execution and just-in-time compilation. He will first interpret and execute the bytecode, and then compile the hotspot codes that are repeatedly executed in method units on the fly.

How efficient is the Java virtual machine?

Just-in-time compilation is based on the assumption that the program complies with the 80/20 rule, that is, 20% of the code occupies 80% of the computing resources.

For the uncommon code that occupies the majority, we do not need to spend time compiling it into machine code, but run it through interpretation and execution; on the other hand, for the hot code that occupies only a small part, We can compile it into machine code to achieve the desired running speed.

Theoretically speaking, the execution efficiency of a just-in-time compiled java program may exceed that of c. This is because compared with static compilation, just-in-time compilation has the runtime information of the program and can make corresponding optimizations based on this information.

For example: for a virtual method call, although there are many target methods, it may only call one of them during actual operation. This information can be exploited by the just-in-time compiler to avoid the overhead of virtual method calls.

The above is the detailed content of How does java code run. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:what is java thread poolNext article:what is java thread pool