What Is a Virtual Machine? A Simple Explanation

What Is a Virtual Machine?
A Virtual Machine, often shortened to VM, is a software-based environment that behaves like a computer or like an environment where programs can run. Which one it is depends on the type of VM.
With a system-oriented VM, you basically get a complete virtual computer. It can run its own operating system, store data, and use a network connection. The real physical computer is called the host. The virtual computers running on it are called guests.
A process-oriented VM is an execution environment for program code. The Java Virtual Machine, or JVM, is a well-known example.
Within crypto, the Ethereum Virtual Machine, often shortened to EVM, works in a similar way. It is not a virtual computer with its own operating system, but an environment that runs smart contracts according to fixed rules.
So you can think of a VM as a software layer that determines how a virtual computer or program code is executed.
Key Takeaways
- A Virtual Machine is a software-based computer environment or execution environment for code.
- A system-oriented VM mimics a complete computer.
- A process-oriented VM runs program code within a fixed runtime.
- The EVM is a blockchain VM for smart contracts, not a virtual PC.
- VMs can help separate environments from each other and run code according to fixed rules.
How Does a Virtual Machine Work?
How a VM works depends on the type.
With a system-oriented VM, there is usually a hypervisor between the physical hardware and the virtual computers. A hypervisor is software that divides resources like processing power, memory, storage, and network capacity from the host across different virtual machines.
Imagine one physical server running three virtual machines. Each VM can then act like it has its own computer, while in reality they share the hardware of the same host. The hypervisor controls which resources each VM can use and keeps the different environments as separate as possible.
The available capacity still depends on the physical computer. If the host has too little memory, processing power, or storage, the virtual machines can be affected too.
A process-oriented VM works differently. Program code is executed inside a fixed software environment. Java code, for example, is converted into instructions that a JVM can run. That means the same code can run on different operating systems and types of hardware, as long as a suitable JVM is available.
Blockchain VMs execute program code according to fixed rules that are the same for the participating nodes. That means the same code with the same input should always produce the same result.
What Types of Virtual Machines Are There?
Virtual Machines are usually divided into two main types: system-oriented and process-oriented VMs.
What Is a System-Oriented Virtual Machine?
A system-oriented Virtual Machine is a virtual computer that can run its own operating system and applications.
The VM gets virtual versions of hardware like a processor, memory, storage, and a network connection. A hypervisor then manages how the different virtual machines use the host's real hardware.
Hyper-V and KVM are examples of technologies that can be used to create these kinds of virtual machines.
One advantage is that multiple operating systems can run on one physical server. That lets you separate different applications or work environments without needing a separate physical computer for each one.
That separation can also help with security. If one virtual machine crashes or gets attacked, that does not automatically mean the other virtual machines are affected too. A secure setup of the host and hypervisor is still important.
What Is a Process-Oriented Virtual Machine?
A process-oriented Virtual Machine is an execution environment for program code, not a full virtual computer with its own operating system.
The Java Virtual Machine (JVM) is a well-known example. A Java program is converted into an intermediate format that the JVM can understand. A suitable JVM can then run that code on different operating systems and types of hardware.
The JVM determines which instructions are available and how Java code is supposed to behave while it runs. The exact technical execution can differ from one JVM to another.
Blockchain VMs are more like this type of VM than like a full virtual computer, because they are mainly meant to run program code according to fixed rules.
What Role Does a Virtual Machine Play in Blockchain?
Within a programmable blockchain, a Virtual Machine determines how smart contracts and other program code are executed.
The VM defines which instructions are possible and how those instructions may change the state of the blockchain. That allows different nodes to process and verify the same transaction according to the same rules.
Ethereum uses the Ethereum Virtual Machine (EVM) for this. Ethereum does not just track transactions, but also a current state that includes accounts, balances, and smart contract data.
When a smart contract is deployed on Ethereum, the compiled program code is stored as bytecode. If someone later calls the contract, the EVM executes that bytecode.
The Virtual Machine and the consensus mechanism have different jobs here. The VM determines what a transaction does technically. Consensus makes sure participants agree on which transactions and which new state are accepted by the network.
Not every blockchain has a general-purpose VM for smart contracts. Bitcoin, for example, uses Bitcoin Script to set conditions for spending bitcoin. This is intentionally more limited than a general smart contract environment like the EVM.
How Does the Ethereum Virtual Machine (EVM) Work?
An Ethereum transaction can, for example, send ETH, create a new smart contract, or call an existing smart contract.
When a smart contract is created, the compiled bytecode is stored in a contract account. If someone later calls the contract, the EVM executes this bytecode.
The EVM works like a stack machine. You can think of a stack as a pile of values. The EVM can place values on this stack and use them again to perform calculations.
The EVM stack can hold up to 1,024 items. Each item consists of 256 bits.
During execution, the EVM uses temporary memory for calculations. This memory disappears after the transaction. Smart contracts can also store data in permanent contract storage, so it remains available for later transactions.
Each EVM instruction, also called an opcode, has a certain gas cost. Gas shows how much computing work and other network resources an action uses. The gas limit of a transaction determines how much gas the execution may use at most.
In the end, you pay for the gas that is actually used. For Ethereum transactions under EIP-1559, the gas price includes a base fee and a priority fee, among other things. The base fee is burned. The priority fee goes to the validator that proposes the block where the transaction is included.
Example: Suppose you send a transaction to a smart contract to transfer tokens. The EVM executes the contract's code, checks the conditions, and updates the stored balances if everything is correct. The transaction uses gas to carry out these instructions.
What Other Blockchain Virtual Machines Are There?
Besides the EVM, there are other blockchain environments for running programs and smart contracts. These can use different programming languages, storage models, and execution rules.
Solana, for example, uses a runtime where programs are executed as sBPF bytecode. Programs are compiled into ELF files with sBPF code and then run in a sandboxed environment.
Instead of gas, Solana uses compute units, among other things, to measure how much computing work a transaction uses. The different instructions within a transaction share the same compute budget.
Sui uses the Move programming language for smart contracts. The smart contracts are published on the blockchain as Move packages and can create and modify on-chain objects.
Sui uses an object model for this. An on-chain object has, among other things, a unique ID, an owner, and a version number. Transactions can read, modify, transfer, or create new objects.
That is different from Ethereum, where accounts and contract storage play a central role. So blockchain VMs can have the same general goal, but be built very differently under the hood.
What Are Virtual Machines Used For?
System-oriented VMs are widely used when multiple operating systems or applications need to run on one physical server. This happens, for example, in cloud environments and data centers.
They are also used to separate development, testing, and production environments from each other. In addition, complete virtual environments can be saved as VM images or virtual disks and started again later.
Process-oriented VMs are mainly used to make the same program code runnable on different systems. That way, an application does not have to directly adapt to every combination of hardware and operating system.
Blockchain VMs make programmable transactions possible. They run smart contracts and can therefore, for example, change stored data, move tokens, or carry out other actions according to the rules of a contract.
What Are the Advantages and Limitations of a Virtual Machine?
Virtual Machines offer several advantages, but they also have limitations.
For system-oriented VMs, these are important advantages:
-
More efficient hardware use: Multiple virtual computers can share the resources of one physical host.
-
Separation between environments: Different operating systems and applications can run independently from each other.
-
Problems are more often contained: An error or crash in one VM does not automatically affect all other VMs on the same host.
-
Environments are easier to reuse: With VM images and virtual disks, a complete software environment can be saved and started again.
That separation is not completely foolproof. In a so-called VM escape, an attacker may try to gain access to the hypervisor or host from inside a virtual machine. A bad configuration can also weaken the separation between VMs.
Virtual Machines also use resources themselves. The hypervisor and the different virtual operating systems need processing power, memory, and storage. If too many VMs use the same physical machine, they can compete with each other for these resources.
For process-oriented VMs, portability is an important advantage. On the other hand, the code always has to work within the rules and available resources of the runtime.
For blockchain VMs, the main advantage is the predictable execution of smart contracts. At the same time, calculations and storage on a blockchain are limited. Networks set limits on how much computing work and storage a transaction or smart contract may use.
In addition, more complex calculations can lead to higher transaction costs. That means heavy computations are not always practical to run fully on a blockchain.
Conclusion
A Virtual Machine can be a complete virtual computer, an environment for running program code, or a specialized runtime for smart contracts.
System-oriented VMs make it possible to run multiple separate computer environments on the same physical hardware. Process-oriented VMs make program code easier to move between different systems.
Within blockchain, VMs like the EVM make sure smart contracts are executed according to fixed rules. That means transactions can do more than just send value.
There are limits, though. Virtual Machines still depend on available resources, and blockchain VMs also set limits on computing work, storage, and costs.