Tuple Logo
java-virtual-machine-jvm

SHARE

Java Virtual Machine (JVM)

What is the Java Virtual Machine?

The Java Virtual Machine (JVM) is a virtual environment that runs Java applications by interpreting or compiling compiled bytecode into machine code. It is the core of the Java platform and enables Java programs to run efficiently, securely, and independently of the underlying operating system.

The JVM acts as a software-based abstraction layer between compiled Java code and the host system. Instead of converting Java code directly into machine code for a specific device, the code is first compiled into bytecode. This bytecode is a universal format that any JVM can read and execute, regardless of the platform.

This design allows Java programs to run seamlessly on different operating systems, as long as a JVM is available. It’s one of the key reasons why Java is so powerful for developers building cross-platform applications.

It’s important to note that the JVM is not a programming language, but a specification. This means there are multiple implementations, such as Oracle HotSpot and OpenJ9, as long as they comply with Oracle’s official specifications.

Why is the JVM important?

The Java Virtual Machine is a crucial component of the Java ecosystem for several reasons. It’s more than just a runtime environment, it provides the foundation for portability, performance, and security in modern software development.

Platform independence

One of the JVM’s most valuable features is its ability to enable the “write once, run anywhere” approach. Java code is compiled into bytecode, which is then interpreted or compiled at runtime by the JVM. Since bytecode is platform-agnostic, any device or operating system with a compatible JVM can run the application, no code changes required.

Support for multiple programming languages

Although the JVM was originally designed for Java, it now supports a variety of other languages that also compile to bytecode. Popular examples include:

These languages can use the same library’s and tools as Java, making the JVM a powerful and flexible platform for developing modern applications.

Built-in security and sandboxing

The JVM includes several features to ensure that code is executed safely and securely:

These features make the JVM a secure choice for running untrusted or remote code, such as in web applications or sandboxed environments.

How the JVM works

The Java Virtual Machine operates through several stages and components that work together to execute Java programs efficiently and securely. It all starts with compiling source code and ends with running the instructions within the runtime environment.

Compilation: from source code to bytecode

When a Java program is written, the source code is saved in .java files. These files are compiled by the Java Compiler (javac) into .class files containing bytecode. Bytecode is not machine code, it’s a compact, standardized instruction format designed to be understood by the JVM.

Role of the class loader

The JVM includes a class loader subsystem responsible for loading compiled classes as they are needed. There are different types of class loaders:

The class loader ensures classes are loaded in the correct order and avoids loading the same class more than once.

Bytecode verification

Once a class is loaded, the JVM performs a verification step. The bytecode is checked for correctness, consistency, and safety. This prevents corrupted or malicious code from causing crashes or security breaches. The verifier checks things like:

Class file structure

A .class file contains more than just instructions. Its structure includes:

This format allows the JVM to interpret the bytecode, call methods, create objects, and execute the program as intended.

Key components of the JVM

The Java Virtual Machine consists of several subsystems that each play a specific role during the execution of a Java program. Together, they ensure efficient memory management, safe execution, and high performance.

Class loader subsystem

The class loader subsystem is responsible for dynamically loading classes as needed. This doesn't happen at the start but during program execution. The class loader determines how and where classes are found, loaded, and isolated. This enables modular applications and plugin-based architectures.

JVM memory model

The JVM manages memory in a structured way. The main memory areas are:

Execution engine

The execution engine runs the bytecode. It consists of:

Just-In-Time (JIT) compiler

The JIT compiler analyzes which methods are used most often during execution and compiles them into optimized machine code. This greatly boosts performance without requiring changes to the source code. Optimizations happen at runtime.

Garbage collection

The JVM performs automatic garbage collection to free up memory that is no longer in use. It identifies unreachable objects in the heap and removes them. Depending on the JVM implementation and settings, several algorithms are available, including:

Each has its own benefits depending on the application's needs and system resources.

Java Native Interface (JNI)

The JNI allows Java code to interact with native code written in other languages like C or C++. This is useful for performance-critical tasks or accessing system-level features not available in Java. However, using JNI introduces extra complexity and potential security risks.

Native method libraries

The JVM can access native libraries to handle lower-level operations like networking or system interaction. These libraries are linked via JNI and run outside the JVM's core runtime.

Multithreading and synchronization

The Java Virtual Machine has built-in support for multithreading, allowing multiple tasks to run simultaneously within a single application. This is essential for modern software that needs to remain responsive and take full advantage of multi-core processors.

How the JVM manages threads

Each thread in Java is managed by the JVM and is given its own stack space. When a program starts, the JVM automatically creates a main thread. Additional threads can be created using the Thread class or by implementing the Runnable interface.

The JVM works together with the underlying operating system to schedule and manage these threads, and it also handles:

Synchronization of methods and objects

In multithreaded applications, multiple threads may try to access shared resources, such as variables or objects, at the same time. To prevent race conditions and data inconsistency, the JVM provides several synchronization mechanisms:

The JVM enforces all synchronization rules according to the Java Memory Model, which defines how and when changes to variables made by one thread become visible to others.

JVM security features

One of the major strengths of the Java Virtual Machine is its built-in security. The JVM is designed to isolate potentially harmful or faulty code and protect the user and the system. This security layer makes Java well-suited for use in environments where safety and reliability are critical, such as web applications, mobile apps, and embedded systems.

Bytecode verifier

When a .class file is loaded, the bytecode verifier checks whether the instructions follow JVM rules. This step prevents issues such as:

If the bytecode fails verification, the JVM halts execution immediately, preventing unsafe code from running.

ClassLoader isolation

The JVM uses multiple class loaders to load classes in isolated contexts based on their origin. This means that code from untrusted sources cannot interfere with core system classes or sensitive application components.

Each class loader maintains its own namespace. This allows the same class to be loaded multiple times in different contexts without conflict, adding both flexibility and security.

Security Manager

The optional Security Manager adds another layer of control by defining what actions code is allowed to perform. Examples include:

A policy file can be used to define permissions for each code source, allowing developers to create secure sandboxed environments. This is especially useful for running plug-ins or downloaded code safely.

JVM and other programming languages

Although the Java Virtual Machine (JVM) was originally built for Java, the platform today supports a wide range of other programming languages. These languages are either designed for the JVM or have been adapted to compile into Java bytecode, allowing them to benefit from the same runtime features, like memory management, garbage collection, and built-in security.

Support for multiple languages

Languages that compile to bytecode and run on the JVM are known as JVM languages. Popular examples include:

These languages allow developers to take advantage of modern programming concepts while still leveraging the stability and maturity of the JVM ecosystem.

The difference between JVM languages and JVM implementations

It's important to distinguish between JVM languages and JVM implementations:

All JVM implementations follow the official specification but may differ in performance, memory usage, and optimization features. For instance, GraalVM supports polyglot programming (mixing multiple languages) and ahead-of-time compilation.

This flexibility makes the JVM valuable not only for Java developers but also for anyone building modern applications within a stable and extensible runtime environment.

JVM configurable behavior and optimizations

The Java Virtual Machine is more than just a runtime environment. It offers a wide range of settings and optimizations that developers and system administrators can use to improve performance, manage memory usage, and tailor behavior to specific use cases.

Command-line options

You can influence the JVM’s behavior at startup using various command-line options. Common examples include:

These options help fine-tune JVM performance based on the application’s needs.

Different garbage collectors

The JVM supports several garbage collection algorithms, each suited for different scenarios:

Garbage CollectorKey featureBest suited for
Serial GCSimple, single-threadedSmall apps or limited-memory environments
Parallel GCMulti-threaded, throughput-orientedMemory-intensive applications
G1 GCRegion-based, low pause timesGeneral-purpose production use
ZGCUltra-low latency, scalableReal-time or large-scale systems

Choosing the right GC algorithm directly impacts application performance and responsiveness.

Client vs. server JVM

The JVM comes in two main modes, optimized for different use cases:

The JVM typically selects the appropriate mode automatically based on the environment, but it can also be set manually using the -client or -server flags.

JVM in embedded and real-time applications

The flexibility of the Java Virtual Machine makes it suitable not only for desktop and server environments but also for embedded systems and real-time applications. Special JVM variants and configurations have been developed to meet the constraints and requirements of these use cases.

Use in embedded systems

Embedded systems, such as routers, printers, industrial devices, or IoT hardware, often have limited memory and processing power. Even so, the JVM can be used in these environments thanks to lightweight implementations like:

These versions are optimized for low memory and power consumption while retaining key advantages of Java, such as portability and security. Typically, they offer a subset of the full Java SE capabilities.

Benefits of using the JVM in embedded systems:

Real-time JVM variants

In scenarios where strict timing constraints must be met, like in medical devices, defense systems, or industrial control, standard JVMs may fall short due to unpredictable garbage collection pauses or thread scheduling delays.

To address this, real-time JVMs have been developed, including:

These JVMs provide:

While less commonly used than standard JVMs, these implementations demonstrate how versatile the Java Virtual Machine is, even beyond traditional software environments.

Availability and implementations

The Java Virtual Machine is available globally on almost every operating system and platform. Since it’s an open specification, multiple implementations exist. While they all follow the same fundamental rules, they differ in performance, licensing, purpose, and additional features.

Popular JVM implementations

There are several widely used implementations of the JVM. Here's a brief overview of the most common ones:

ImplementationDescription
HotSpot (Oracle/OpenJDK)The standard and most commonly used JVM. Supports both client and server modes and includes the HotSpot JIT compiler. It comes bundled with most Java distributions.
GraalVMAn advanced JVM with support for multiple languages (Java, JavaScript, Python, Ruby) and ahead-of-time (AOT) compilation for extremely fast startup times. Ideal for microservices.
Eclipse OpenJ9A lightweight, performance-optimized JVM developed by IBM. Offers faster memory management and shorter startup times compared to HotSpot.
Zulu / Amazon Corretto / Liberica JDKOpenJDK builds with commercial support. Fully compatible with the standard JVM specification.

All of these implementations follow the official Java Virtual Machine Specification, which means they can execute the same bytecode. The choice depends on performance needs, licensing preferences, and any extra features like monitoring tools or cloud support.

Platform availability

The JVM is available on:

This allows developers to run the same application on laptops, servers, smartphones, and IoT devices, without changing the code.

The role of the JVM in modern software

The Java Virtual Machine has been a fundamental part of software development for decades. What started as a solution for running Java applications across platforms has grown into a powerful ecosystem that supports multiple programming languages, modern architectures, and a wide range of use cases.

The strength of the JVM lies in the combination of:

Whether you're building an Android app, a cloud-based service, or an IoT solution, the JVM remains a reliable and powerful engine for modern software. With growing support for more languages and tools like GraalVM, the Java Virtual Machine continues to prove its relevance in today’s fast-evolving tech landscape.

Frequently Asked Questions
What does JVM mean?

JVM stands for Java Virtual Machine. It’s a virtual environment that executes Java bytecode regardless of the underlying operating system.


Is the JVM stack-based?

Yes, the JVM is stack-based. Each thread has its own stack that stores method calls, variables, and return values during execution.


How does the Java Virtual Machine work?

The JVM loads compiled bytecode, verifies it for safety, and executes it using an interpreter or JIT compiler. It also manages memory, threads, and objects automatically.


What is the role of the Java Virtual Machine?

The JVM runs Java applications by interpreting or compiling bytecode into machine code. It also handles memory management, security, and cross-platform compatibility.


Articles you might enjoy

Piqued your interest?

We'd love to tell you more.

Contact us
Tuple Logo
Veenendaal (HQ)
De Smalle Zijde 3-05, 3903 LL Veenendaal
info@tuple.nl‭+31 318 24 01 64‬
Quick Links
Customer Stories