Java Virtual Machine
The Java Virtual Machine is a virtual “machine” that provides a runtime environment for Java applications and programs. Its role is simple: interpret and execute Java?bytecode, which a low-level representation of the compiled form of a piece of Java code. When applications written in Java are compiled, they produce bytecode, which 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 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 achieved courtesy of a layer of abstraction residing between the Java code and the underlying OS and hardware. At runtime, the JVM interprets the resultant bytecode and translates it into native machine code, accounting for the characteristics of the underlying system it will run on.
How Does the Java Virtual Machine Work?
To answer the question, “How does the JVM work?” developers must first understand the steps the JVM follows in order to interpret Java code and then execute it. These steps include:
Interpreting the Java bytecode
Loading Java Bytecode
The very first step that occurs in the JVM process involves loading the Java bytecode into the JVM. This task is performed by the class loader, whose responsibility is to locate any necessary bytecode files and load them into system memory.
Verification
After bytecode is loaded into memory, the JVM needs to verify its correctness, which it does by checking the Java bytecode for violations of the Java language specification, including illegal access to private fields or private methods.
领英推荐
Preparing Bytecode
Once the bytecode is verified, the Java Virtual Machine preps the memory and resources needed in order for the program to execute. This preparation includes memory allocation for any required objects and initializing static variables.
Interpretation Java Bytecode
Next, the JVM has to interpret the bytecode and sequentially execute each instruction. While each instruction is executed, the JVM maintains a stack of values to be used by any following sets of instructions.
Just-In-Time Compilation
Once the code has been interpreted, the JVM may uses Just-In-Time (JIT) compilation to improve performance. During JIT compilation, the JVM compiles frequently executed bytecode into native machine language, which is executed with more efficiency than interpreted bytecode. We discuss JIT in more detail in a section below.
Garbage Collection
As the application is executing, the Java Virtual Machine manages memory resources (allocating and deallocating) by performing automatic?garbage collection. Garbage collection frees up memory resources that are no longer being used by the program or CPU, allowing the memory to be reclaimed by the JVM and put to other uses.
What are the Features of the JVM?
The Java Virtual Machine has several key features that make it such a powerful and invaluable platform including?platform independence,?memory management,?security,?dynamic loading,?JIT compilation, and?multithreading: