The Role of a Virtual Machine
What Is a Virtual Machine?
Virtual machine
A virtual machine (VM) is a software-based emulation of a physical computer.
It provides a layer of abstraction that allows software to run in a controlled, isolated environment.
- A virtual machine is not a physical machine.
- It's a software construct that mimics the behaviour of a physical computer.
Key Features of Virtual Machines
We need a VM for:
- Isolation
- Each VM operates independently, ensuring that issues in one VM do not affect others.
- For that property, it's widely used in cybersecurity, as potential hackers cannot access the original machine.
- Portability
- VMs can be moved between different physical machines without modification.
- VM Image allows you to duplicate and distribute the state of a VM.
- Abstraction
- VMs abstract the underlying hardware, allowing software to run on any compatible host.
- Hence, reducing the need for physical hardware lowers costs.
Think of a virtual machine as a sandbox where you can run applications without worrying about affecting the host system.
VMs can be used for:
- Software Development: VMs provide a controlled environment for testing applications on different operating systems.
- Server Consolidation: Multiple VMs can run on a single physical server, optimising resource usage.
- Disaster Recovery: VMs can be easily backed up and restored, ensuring business continuity.
- Legacy Software: VMs can run outdated applications on modern hardware.
- For instance, to run software designed for Windows XP on modern Windows 10 machines.
How Virtual Machines Work
A hypervisor is software that creates and manages virtual machines.
There are two types:
- Type 1 (Bare-Metal): Runs directly on the hardware (e.g., VMware ESXi).
- Type 2 (Hosted): Runs on top of an operating system (e.g., VirtualBox, VMware Workstation).
To note:
- Each VM runs its own operating system, known as the guest OS.
- The hypervisor provides virtualised hardware components, such as CPU, memory, and storage.
- Imagine a virtual machine as a guest staying in a hotel.
- The hotel (hypervisor) provides all the necessary amenities (virtual hardware), allowing the guest (guest OS) to function independently.
Intermediate Code
Intermediate Code
Code that is not specific to any hardware architecture.
- Due to the intermediate code properties, it can be used in VMs.
- Examples include Java bytecode and .NET Intermediate Language (IL).
The JVM allows Java programs to run on any device with a JVM installed, regardless of the underlying hardware.
You've probably heard terms like "JVM", "JRE", and "JDK", so it is good to understand their differences and roles in Java.
JVM vs JRE vs JDK
Some VMs use Just-In-Time (JIT) Compilation to convert intermediate code into machine code during execution.
Benefits of JIT:
- Portability: Code can run on any platform with a compatible VM.
- Optimisation: JIT compilers can optimise code based on the specific hardware.
JIT compilation balances the portability of interpreted code with the performance of compiled code.
- What are the main advantages of using virtual machines in software development?
- How do virtual machines contribute to cross-platform compatibility?
- Can you think of a scenario where using a virtual machine would be beneficial?
How does the abstraction provided by virtual machines influence our understanding of hardware and software interactions?