I. What Is a Virtual Machine?
A virtual machine (VM) is an abstract execution environment built in software. It provides programs with a relatively independent execution model, so that programs do not have to depend directly on the low-level details of specific physical hardware or an operating system.
The core idea of a virtual machine is: build a virtual execution environment on top of a real computer, with software simulating or managing the computing resources and execution rules required by a program.
For example:
- Java Virtual Machine (JVM): executes Java bytecode and other compatible bytecode.
- .NET CLR: provides an execution environment for managed code.
- Python virtual machine: executes Python bytecode.
- WebAssembly Runtime: executes WebAssembly modules.
- QEMU: can emulate processors of different architectures and complete computer systems.
Different virtual machines operate at different levels of abstraction. Some provide only an execution environment for program instructions, while others emulate complete computer hardware.
1. Basic Structure of a Virtual Machine
A typical process-level virtual machine usually includes the following parts:
| Component | Main responsibility |
|---|---|
| Instruction set or bytecode | Defines the operations a program can perform |
| Execution engine | Interprets or compiles and executes instructions |
| Memory management | Manages objects, the heap, the stack, and memory reclamation |
| Class or module loader | Loads program modules and dependencies |
| Runtime library | Provides basic capabilities such as strings, collections, and files |
| Security and isolation mechanisms | Restricts access scope and checks execution behavior |
| Native interface | Interacts with the operating system and native code |
Not all virtual machines have exactly the same components. For example, the JVM has class loading and garbage collection mechanisms, while hardware virtual machines usually focus more on emulating or virtualizing virtual CPUs, memory, and devices.
2. How a Virtual Machine Works
Taking a Java program as an example, the source code is first compiled into bytecode, which is then loaded and executed by the JVM.
flowchart TD
A["Java source code"] --> B["Java compiler"]
B --> C["Bytecode file"]
C --> D["JVM class loader"]
D --> E["Bytecode verification"]
E --> F["Execution engine"]
F --> G["Interpreted execution"]
F --> H["JIT compilation"]
G --> I["Program execution"]
H --> I
style A fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style B fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style D fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style E fill:#FFF5DE,stroke:#E8D29A,color:#334155
style F fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style G fill:#E8EDF2,stroke:#B8C3CE,color:#334155
style H fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style I fill:#E5F4EC,stroke:#A3CBB5,color:#334155
linkStyle default stroke:#A3B8AC,stroke-width:1.5px
Note that the JVM is not merely a bytecode interpreter. Modern JVMs usually combine interpreted execution, just-in-time (JIT) compilation, runtime optimization, and other techniques to balance startup speed and long-term runtime performance.
II. Main Types of Virtual Machines
A virtual machine is not a single technology. Based on the level of abstraction and execution target, virtual machines can be divided into types such as system virtual machines and process virtual machines.
1. System Virtual Machine
A system virtual machine emulates or virtualizes a complete computer, providing an execution environment for an operating system.
For example:
- VMware: provides a complete virtualization platform.
- VirtualBox: supports creating and running virtual computers.
- Hyper-V: virtualization technology provided by Microsoft.
- KVM: hardware virtualization capability provided by the Linux kernel.
- QEMU: supports hardware emulation and can also be combined with KVM and other technologies for acceleration.
A system virtual machine usually needs to emulate or virtualize:
- CPU and instruction execution environment.
- Memory address space.
- Disks and storage devices.
- Network devices.
- Interrupts and other hardware resources.
Its basic structure is as follows:
flowchart TB
A["Physical Hardware"] --> B["Host Operating System or Hypervisor"]
B --> C["Virtual Machine Management Layer"]
C --> D["Virtual Machine A"]
C --> E["Virtual Machine B"]
C --> F["Virtual Machine C"]
D --> G["Guest Operating System A"]
E --> H["Guest Operating System B"]
F --> I["Guest Operating System C"]
G --> J["Application"]
H --> K["Application"]
I --> L["Application"]
style A fill:#E8EDF2,stroke:#B8C3CE,color:#334155
style B fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style C fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style D fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style E fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style F fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style G fill:#FFF5DE,stroke:#E8D29A,color:#334155
style H fill:#FFF5DE,stroke:#E8D29A,color:#334155
style I fill:#FFF5DE,stroke:#E8D29A,color:#334155
style J fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style K fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style L fill:#E5F4EC,stroke:#A3CBB5,color:#334155
linkStyle default stroke:#A3B8AC,stroke-width:1.5px
The main value of system virtual machines is isolating different operating systems and workloads, enabling one physical server to host multiple independent system environments.
2. Process Virtual Machine
A process virtual machine provides a dedicated execution environment for a program or process.
It does not need to emulate a complete computer; instead, it defines a set of program instructions, runtime interfaces, and resource management mechanisms.
Common examples:
| Virtual Machine | Execution Target | Typical Use |
|---|---|---|
| JVM | Java bytecode | Java applications |
| CLR | .NET Intermediate Language and related managed code | C#, F#, etc. |
| CPython VM | Python bytecode | Python programs |
| WebAssembly Runtime | WebAssembly bytecode | Browser and server-side execution |
Process virtual machines usually rely on the host operating system to provide basic capabilities such as processes, files, networking, and threads.
3. Differences Between System Virtual Machines and Process Virtual Machines
| Comparison Dimension | System Virtual Machine | Process Virtual Machine |
|---|---|---|
| Virtualization Object | Complete Computer | Program Execution Environment |
| Execution Target | Operating System | Application |
| Abstraction Level | Hardware Layer | Program Execution Layer |
| Operating System | Usually runs an independent guest operating system | Usually relies on the host operating system |
| Resource Management | Virtual CPU, memory, devices, etc. | Program memory, threads, objects, etc. |
| Typical Representatives | KVM, VMware, VirtualBox | JVM, CLR, CPython |
The two solve different problems:
- System virtual machines focus on the virtualization and isolation of computer resources.
- Process virtual machines focus on program execution, portability, and runtime management.
III. What Is a Runtime?
A runtime is the environment and supporting mechanisms that a program depends on during execution.
In a broad sense, a runtime refers not only to a software component but also to the runtime libraries, memory management, thread scheduling, exception handling, dynamic linking, and system interfaces required for a program to run.
For example:
- The Java Runtime Environment (JRE) provides the JVM and runtime libraries needed to run Java applications.
- The .NET Runtime provides capabilities such as managed code execution, garbage collection, and the type system.
- The Python Runtime provides an interpreter, object model, and standard runtime library.
- The Node.js Runtime provides a JavaScript execution environment and server-side APIs for files, networking, and more.
The main problem a runtime solves is: after a program is loaded, how it obtains the resources and services needed for execution.
1. Main Components of a Runtime
| Component | Role |
|---|---|
| Execution engine | Executes bytecode or machine instructions |
| Memory manager | Allocates, manages and reclaims memory |
| Garbage collector | Automatically reclaims objects that are no longer used |
| Thread management | Creates, schedules and manages threads |
| Exception handling | Catches, propagates and handles runtime exceptions |
| Runtime library | Provides commonly used data structures and functionality |
| Dynamic linker | Loads and resolves runtime dependencies |
| Native interface | Calls into the operating system and native code |
| Security mechanisms | Performs permission checks and controls access to resources |
These components are not necessarily all implemented by a single standalone runtime. Some capabilities may be provided jointly by the operating system, the virtual machine, the standard library or external components.
2. How a runtime works
A typical managed program run can be abstracted as follows:
flowchart TD
A["Start the program"] --> B["Initialize the runtime"]
B --> C["Load the program and dependencies"]
C --> D["Initialize the execution environment"]
D --> E["Execute program code"]
E --> F["Runtime services"]
F --> G["Memory management"]
F --> H["Thread scheduling"]
F --> I["Exception handling"]
F --> J["System calls"]
G --> K["Continued execution"]
H --> K
I --> K
J --> K
K --> L{"Has the program ended?"}
L -->|"No"| E
L -->|"Yes"| M["Release resources and exit"]
style A fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style B fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style C fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style D fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style E fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style F fill:#FFF5DE,stroke:#E8D29A,color:#334155
style G fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style H fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style I fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style J fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style K fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style L fill:#FFF5DE,stroke:#E8D29A,color:#334155
style M fill:#E8EDF2,stroke:#B8C3CE,color:#334155
linkStyle default stroke:#A3B8AC,stroke-width:1.5px
This is a conceptual model, and it does not mean that the runtimes of all languages initialize and execute in exactly the same order.
IV. The relationship between virtual machines and runtimes
Virtual machines and runtimes often appear together, but they are not the same concept.
A virtual machine emphasizes the abstract execution environment in which a program runs, while a runtime emphasizes the supporting capabilities required during program execution.
In many language implementations, the virtual machine is a core part of the runtime, but the scope of the runtime is usually larger.
For example, the JVM is responsible for executing Java bytecode, and it also includes mechanisms such as class loading, memory management, garbage collection, threading and runtime optimization. The JRE, in turn, provides components such as the runtime libraries needed to run Java applications, in addition to the JVM.
1. Core differences between the two
| Comparison dimension | Virtual machine (VM) | Runtime |
|---|---|---|
| Core positioning | Abstract execution environment | Support environment for program execution |
| Main focus | Instruction execution and environment abstraction | Resources and services during execution |
| Core capabilities | Instruction interpretation, compilation or emulation | Memory, threads, exceptions, libraries, etc. |
| Must it exist | Depends on the execution model | Program execution requires some kind of runtime environment |
| Typical examples | JVM, CLR, WebAssembly VM | Java Runtime, .NET Runtime, Node.js Runtime |
2. How virtual machines and runtimes contain one another
In a typical managed language environment, the relationship between the two can be understood as follows:
flowchart TB
A["Program Runtime Environment"]
A --> B["Virtual Machine or Execution Engine"]
A --> C["Memory Management"]
A --> D["Thread and Task Management"]
A --> E["Runtime Library"]
A --> F["Exception and Type Support"]
A --> G["Native System Interface"]
B --> H["Bytecode Execution"]
B --> I["JIT Compilation and Optimization"]
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style D fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style E fill:#FFF5DE,stroke:#E8D29A,color:#334155
style F fill:#FFF5DE,stroke:#E8D29A,color:#334155
style G fill:#E8EDF2,stroke:#B8C3CE,color:#334155
style H fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style I fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
linkStyle default stroke:#A3B8AC,stroke-width:1.5px
This is a common conceptual relationship, not a strict, unified architecture specification. Different languages and platforms may split the execution engine, runtime library, and resource management into different components.
5. Virtual Machines and Runtimes in Mainstream Languages
Different programming languages use very different execution models. Some languages rely on virtual machines, some mainly rely on native machine code and the operating system runtime environment, and others use hybrid execution models.
1. Java: JVM and Java Runtime
Java is a typical case of a process virtual machine and a runtime working together.
- Loading and verifying Java bytecode.
- Executing bytecode.
- Managing runtime memory.
- Performing garbage collection.
- Supporting threads and synchronization mechanisms.
- Performing JIT compilation and runtime optimization.
The Java runtime environment also includes the standard class libraries and other necessary components.
Several concepts need to be distinguished:
| Name | Meaning |
|---|---|
| JDK | Java Development Kit, including development tools and the components required for running applications |
| JRE | Traditional concept of the Java application runtime environment, including the JVM and runtime libraries |
| JVM | The virtual machine specification for executing Java bytecode and its specific implementations |
Modern Java distributions usually use the JDK as the primary delivery form; the JRE is no longer always provided as a separate installation package.
2. .NET: CLR and .NET Runtime
The .NET platform mainly relies on the CLR (Common Language Runtime) to execute managed code.
Languages such as C# are usually first compiled to CIL (Common Intermediate Language) and then executed by the CLR.
The CLR's main responsibilities include:
- Managed code execution.
- Type safety checking.
- Garbage collection.
- Exception handling.
- Thread support.
- JIT compilation.
- Interoperation with native code.
Typical execution process:
flowchart TD
A["C# Source Code"] --> B["C# Compiler"]
B --> C["Assembly and CIL"]
C --> D["CLR Loads Assembly"]
D --> E["Validation and Runtime Preparation"]
E --> F["JIT Compilation"]
F --> G["Native Machine Code"]
G --> H["CPU Execution"]
style A fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style B fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style D fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style E fill:#FFF5DE,stroke:#E8D29A,color:#334155
style F fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style G fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style H fill:#E8EDF2,stroke:#B8C3CE,color:#334155
linkStyle default stroke:#A3B8AC,stroke-width:1.5px
Note that .NET also supports compilation approaches such as Native AOT. Not all .NET programs must perform JIT compilation at runtime.
3. Python: Interpreter and Runtime
Taking CPython as an example, Python source code is usually compiled to bytecode and then executed by the CPython interpreter.
The CPython runtime mainly provides:
- The Python object model.
- The bytecode execution engine.
- Reference counting and cyclic garbage collection.
- The module import mechanism.
- Exception handling.
- Built-in types and the standard library.
- Interfaces for interacting with C extensions.
A typical execution process:
flowchart TD
A["Python source code"] --> B["Lexical and syntactic analysis"]
B --> C["Compile to bytecode"]
C --> D["CPython bytecode interpreter"]
D --> E["Python runtime"]
E --> F["Object and memory management"]
E --> G["Modules and standard library"]
E --> H["Operating system interface"]
F --> I["Program execution"]
G --> I
H --> I
style A fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style B fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style D fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style E fill:#FFF5DE,stroke:#E8D29A,color:#334155
style F fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style G fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style H fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style I fill:#E5F4EC,stroke:#A3CBB5,color:#334155
linkStyle default stroke:#A3B8AC,stroke-width:1.5px
The description here is mainly about CPython. The Python language specification does not mandate that all implementations use the same bytecode format and execution approach. Implementations such as PyPy can also use JIT technology.
4. JavaScript: Engine and Host Runtime
The JavaScript execution environment consists of both the JavaScript engine and the host environment.
Common JavaScript engines include:
- V8: used in environments such as Chrome and Node.js.
- SpiderMonkey: used in Firefox.
- JavaScriptCore: used in environments such as Safari.
The JavaScript engine is responsible for:
- Parsing JavaScript code.
- Generating an internal representation or bytecode.
- Executing code.
- Performing JIT compilation and optimization.
- Managing JavaScript object memory.
The host runtime, meanwhile, provides capabilities beyond the engine.
For example:
| Environment | Capabilities provided by the host |
|---|---|
| Browser | DOM, BOM, events, network APIs |
| Node.js | File system, networking, processes, modules, etc. |
| Deno | Files, networking, permission control, etc. |
| Bun | Server-side APIs, module loading, toolchain, etc. |
Take Node.js as an example:
flowchart TB
A["Node.js Application"]
A --> B["JavaScript Engine V8"]
A --> C["Node.js Runtime"]
B --> D["Code Parsing and Execution"]
B --> E["JIT Compilation and Optimization"]
C --> F["Event Loop"]
C --> G["File and Network APIs"]
C --> H["Module System"]
C --> I["Native Extensions"]
D --> J["Operating System"]
E --> J
F --> J
G --> J
H --> J
I --> J
style A fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style B fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style D fill:#FFF5DE,stroke:#E8D29A,color:#334155
style E fill:#FFF5DE,stroke:#E8D29A,color:#334155
style F fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style G fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style H fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style I fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style J fill:#E8EDF2,stroke:#B8C3CE,color:#334155
linkStyle default stroke:#A3B8AC,stroke-width:1.5px
An important distinction: V8 is the JavaScript engine, while Node.js is a runtime environment that includes V8 and other system capabilities.
JavaScript in the browser also uses an engine, but the DOM, timers, and browser network APIs belong to the browser host environment and are not all capabilities provided by the ECMAScript language itself.
5. Go, Rust, and C/C++: Native Execution Environments
Go, Rust, and C/C++ typically use a native machine code execution model, but this does not mean that a runtime is completely absent.
| Language | Typical Execution Method | Runtime Characteristics |
|---|---|---|
| Go | Compiled to native machine code | Built-in scheduler, garbage collector, and runtime support |
| Rust | Compiled to native machine code | Typically has no traditional managed runtime; some standard libraries and target platforms still require runtime support |
| C | Compiled to native machine code | Depends on startup code, the C runtime library, and operating system interfaces |
| C++ | Compiled to native machine code | Depends on mechanisms such as the C++ runtime library, exception handling, and runtime type information |