What is the JVM (Java Virtual Machine)?
- What is the JVM (Java Virtual Machine)?
- What is a JVM implementation?
- How does the Java Virtual Machine work?
- What are the key components of Java Virtual Machine architecture?
- What is JIT in Java Virtual Machine?
- What is the difference between JVM, JRE, and JDK?
- How can AWS support your Java programming language requirements?
What is the JVM (Java Virtual Machine)?
Java Virtual Machine (JVM) is the software component that runs the Java code developers write so the Java application works. Prior to Java’s launch, programs would only run on specific target platforms. Developers also had to manage the program's memory usage, which was very time-consuming. With its JVM, Java introduced the concept of “write once, run anywhere” to programming languages. Java code runs in the JVM, independent of underlying platform dependencies. The JVM, along with the Java Runtime Environment (JRE) and the Java Development Kit (JDK), are the three essential components required for Java programming.
What is a JVM implementation?
There are three aspects to the JVM—specification, implementation, and instance.
Specification
The JVM specification is a document that outlines the rules, behavior, and functionality of the Java Virtual Machine. It serves as the official reference and provides a standardized framework while still allowing development flexibility. Different groups can create their own JVM as long as they follow the specification.
Implementation
JVM implementation refers to the software implementation of the specification. There are multiple JVM implementations available, provided by different vendors. For example, OpenJDK's HotSpot is an open-source JVM implementation. It serves as the base for many other JVM implementations and is widely used by developers, organizations, and the Java community. It is one of the most thoroughly tried-and-tested codebases in the world.
Instance
When you run Java on your machine, this means a specific instance of the JVM runs on your machine. When developers use the term JVM, they refer to the software running on their machine that is running the Java code they write.
How does the Java Virtual Machine work?
In Java, real-world entities are represented as Java objects. Objects have attributes and behaviors. So, for example, if you are creating a booking system in Java, you could have the customer object with attributes like name and address and functions like create_customer. You define a customer class that specifies the blueprint or template of the customer object.
While classes and objects are fundamentals of Java programming, they are still abstract representations. Class customer is just a line of text. JVM processes the text written in the Java program and converts the abstract concepts into physical representations in device memory. For example, for the code text - customer object A, it creates a physical memory component for object A.
While the associated complexities are high, we briefly overview the process below.
- Developer writes the Java code.
- Developer compiles the Java code.
- The JRE converts the code to a low-level representation called Java bytecode.
- The JVM runs the bytecode, allocating and deallocating memory on the underlying platform as required.
What are the key components of Java Virtual Machine architecture?
The main components of JVM architecture are given below. Each component is critical and supports the JVM instance to run the Java program.
Class Loader
The Class Loader is responsible for loading Java class files into memory. It locates, loads, and verifies the bytecode of classes and interfaces as referenced by the program. It also maintains the runtime integrity of classes and ensures they adhere to the JVM's security and memory management rules.
Runtime data areas
The JVM divides memory into various runtime data areas, such as the method area, heap, stack, and PC registers.
- The Method Area stores class-level structures such as method bytecode, field information, and constant pool data.
- The Heap is a runtime data area where JVM dynamically allocates memory space for objects. All Java threads share the Heap.
- Program Counter (PC) registers contain the address of the currently running bytecode instruction.
Execution Engine
The Execution Engine is responsible for running the Java bytecode. It includes the Just-In-Time (JIT) compiler, which dynamically compiles frequently run bytecode into native machine code for better performance.
Garbage collector
The garbage collector manages memory allocation and reclamation for data objects. It automatically identifies and frees the memory occupied by objects the program no longer references.
Java Native Interface
The Java Native Interface allows Java programs to interact with native code and libraries written in languages like C and C++. It enables Java applications to call native methods and vice versa, allowing seamless integration of Java with existing platform-specific functionality.
Native method libraries
Native method libraries contain implementations of native methods referenced by Java applications. These libraries provide the bridge between Java code and platform-specific functionality. Native methods are typically used for low-level operations or system-dependent tasks that you cannot implement directly in Java.
What is JIT in Java Virtual Machine?
The JVM interprets the bytecode line by line, translating each instruction into machine code at runtime. This interpretation process allows for platform independence but can be slower than native machine code execution. The JVM employs the Just-In-Time (JIT) compilation technique to improve performance. JIT compilation provides a significant performance boost for Java applications. It allows the JVM to optimize for the specific runtime environment and hardware.
How JIT works
Instead of interpreting the entire bytecode, the JVM identifies frequently run portions of code (such as loops) or frequently called methods, known as hotspots. The JIT compiler then selectively compiles these hotspots into native machine code, which the CPU can run directly. This compiled code is stored in a code cache and reused for subsequent invocations. The JIT compilation process involves several stages.
Interpretation
The JVM starts by interpreting the bytecode, executing it line by line. This allows for rapid startup and makes the JVM platform-independent.
Profiling
As the bytecode is being executed, the JVM collects information about the code's behavior, such as the frequency of method invocations and the types of objects being used. This profiling information helps identify hotspots.
JIT compilation
Based on the profiling data, the JIT compiler identifies hotspots and selectively compiles them into native machine code. The compilation process includes optimizations like inlining frequently called methods, eliminating unnecessary checks, and optimizing arithmetic operations.
Deoptimization
Since the JVM compiles code based on profiling data, a change in the program's behavior results in code deoptimization and a fallback to interpretation. This ensures correctness but can impact performance temporarily.
Recompilation
The JVM continuously monitors the program's running and recompiles hotspots as necessary. If a previously unoptimized portion of code becomes a hotspot, the JVM compiles it to improve performance.
What is the difference between JVM, JRE, and JDK?
JVM, JRE, and JDK are all essential components of the Java platform, but they serve different purposes. JVM is the runtime engine that executes Java bytecode. JRE and JDK are software packages that include the JVM along with additional tools and libraries.
JVM vs. JRE
Java Runtime Environment (JRE) is a package that includes the JVM, Java class libraries, and other components required for running Java applications. With JRE installed, you can run Java applications on your system without the need for compiling or modifying code.
JVM vs. JDK
Java Development Kit (JDK) is a package that includes the JRE along with other development tools necessary for Java application development. The JDK provides compilers, debuggers, documentation, and other utilities that developers use to write, compile, and debug Java code. It is essential for software development and enables programmers to create and build Java applications.
How can AWS support your Java programming language requirements?
AWS SDK for Java simplifies the use of AWS Services by providing a set of consistent and familiar libraries for Java developers. It supports higher-level abstractions for simplified development. The SDK code examples contain Java code examples and real-world use cases for AWS services to help accelerate the development of your applications. In addition, the Java API reference guide describes the API operations for the latest version of the AWS SDK for Java. The reference guide provides sample requests, responses, and errors for the supported web services protocols.
Get started with AWS SDK for Java with the developer guide or visit the AWS Java Developer Center.
Browse all cloud computing concepts
Browse all cloud computing concepts content here:
Did you find what you were looking for today?
Let us know so we can improve the quality of the content on our pages