【Java】What is Java Virtual Machine (JVM)

This article is for learning reference only!
Related tutorial (article) address:
https://zhuanlan.zhihu.com/p/34426768
https://github.com/doocs/jvm

The Java Virtual Machine (JVM) is a key component of the Java programming language, providing a platform-independent environment for executing Java code across most major hardware, operating system, and software architectures. In this article, we'll discuss what the JVM is, how it works, and the main features it offers developers.

What are JVMs?

Java Interview Questions JVM Concepts

The Java Virtual Machine is a virtual "machine" that provides a runtime environment for Java applications and programs. What it does is simple: interpret and execute Java bytecode , which is a low-level representation of a piece of Java code in compiled form. When applications written in Java are compiled, they produce bytecode that can be executed (or run) by any JVM implementation, regardless of the underlying architecture, hardware, or operating system.

Java's JVM is platform independent, meaning that Java programs can be written once and run on any JVM implementation - a principle known as WORA or Write Once, Run Anywhere . This concept of WORA is implemented through an abstraction layer between Java code and the underlying operating system and hardware. At runtime, the JVM interprets the generated bytecode and converts it into native machine code, taking into account the characteristics of the underlying system on which it will run.

How does the Java virtual machine work?

Answer the question "How does the JVM work?" A developer must first understand the steps the JVM follows in order to interpret Java code and then execute it. These steps include:

  • load bytecode
  • verify bytecode
  • Prepare memory resources
  • Interpret Java bytecode
  • just-in-time compilation
  • garbage collection

Load Java bytecode

The first step that occurs in the JVM process involves loading Java bytecode into the JVM. This task is performed by the class loader, whose responsibility is to find any necessary bytecode files and load them into system memory.

confirm

After the bytecode is loaded into memory, the JVM needs to verify its correctness by checking the Java bytecode for violations of the Java Language Specification, including illegal access to private fields or private methods.

Prepare bytecode

Once the bytecode is verified, the Java Virtual Machine prepares the memory and resources needed to execute the program. This preparation includes allocating memory for any required objects and initializing static variables.

Interpret Java bytecode

Next, the JVM must interpret the bytecode and execute each instruction sequentially. As each instruction is executed, the JVM maintains a stack of values ​​for use by any subsequent set of instructions.

just-in-time compilation

Once the code is interpreted, the JVM can use just-in-time (JIT) compilation to improve performance. During JIT compilation, the JVM compiles frequently executed bytecode into native machine language, which executes more efficiently than interpreted bytecode. We will discuss JIT in more detail in the following sections.

garbage collection

The Java Virtual Machine manages memory resources (allocation and deallocation) by performing automatic garbage collection as the application executes . Garbage collection frees memory resources that are no longer used by the program or the CPU, allowing the JVM to reclaim this memory and use it for other purposes.

What are the characteristics of JVM?

The Java Virtual Machine has several key features that make it such a powerful and valuable platform, including platform independence , memory management , security , dynamic loading , JIT compilation , and multithreading :

  • Platform independence: The JVM includes a platform-independent runtime environment that allows Java applications to be executed on most hardware and software architectures.
  • Memory management: JVM automatically manages memory through its built-in garbage collector, which frees memory when the program is complete, freeing developers from having to think about memory allocation and deallocation.
  • Security: The JVM enforces security policies to prevent malicious code from gaining access to system resources.
  • **Dynamic loading:** Using the Java virtual machine, classes can be dynamically loaded at runtime, allowing developers to extend the functionality of their Java programs without recompiling or redeploying.
  • Just-in-time compilation: JIT compilation improves application performance by compiling frequently executed bytecode into native machine language.
  • Multithreading: JVM supports multithreading, which allows a Java program to run multiple threads of execution concurrently.

What is JIT?

As discussed, JIT is a compilation mechanism that enables the JVM to compile frequently executed bytecode into native machine language at runtime. During the execution of a Java application, the JVM interprets the bytecode and executes one instruction at a time. This process can be slow and suboptimal, and this is where JIT comes into play.

Just-in-time compilation improves performance by dynamically compiling frequently executed bytecode, changing it into native machine code, which is much more efficient than its interpreted counterpart. This native code is stored in memory and reused when the same code is executed, further improving application performance.

How does the JIT work?

As mentioned above, when executing a Java program, the JVM interprets the bytecode and executes it sequentially, one instruction at a time. During this process, if the JVM detects that a certain piece of code is being executed frequently, it starts compiling that frequently used code into native machine code.

A JIT compiler can optimize the performance of compiled code by using several techniques. For example, loop unrolling can be employed to remove the overhead of loop control structures. Inline methods can be used to remove the overhead associated with method calls.

What are the benefits of JIT?

The JIT provides several benefits to Java developers, including improved performance, faster startup times, adaptive optimization, platform independence, and shared compiled code:

  • **Improve Performance:** Improve performance by dynamically compiling frequently executed bytecode into native machine code.

  • **Faster startup time:** Since the JVM only compiles frequently executed code, the startup time of Java applications is much faster.

  • **Adaptive Optimization:** The JIT compiler adjusts code optimization strategies based on a specific program's execution profile, which can help improve performance.

  • Platform independence: JIT compilation allows Java programs to run on many different hardware and software systems without recompilation.

  • **Code Sharing:** Since compiled code is stored in memory and reused on subsequent executions, the JIT allows multiple instances of the same program to share compiled code. This action helps reduce memory usage and resources.

END

In this article, we learned about the Java Virtual Machine (JVM), how it works, its features, and its benefits. This includes providing a platform-independent runtime environment for Java programs to run, regardless of the system on which they are deployed. The JVM interprets Java bytecode and converts it to native machine code at runtime.

We also discussed JIT compilation, which lets the JVM improve the performance of Java applications by dynamically compiling frequently executed bytecode into native machine code. The technology improves performance, reduces startup time, employs adaptive optimization, provides platform independence, and allows code sharing.

Guess you like

Origin blog.csdn.net/m0_47015897/article/details/131409959