Fundamentals of Virtualization · Lesson 8
Managing Memory for a Virtual Machine
Map guest memory to host RAM, avoid paging, and use ballooning, overcommitment, page sharing, and compression responsibly.
Lesson purpose
Memory is the working area where operating systems, applications, and data are kept for fast access. In many virtual environments, memory becomes the limiting resource before CPU does. The hypervisor maps guest memory to physical RAM, accounts for its own overhead, and may reclaim or optimize pages when workloads compete. Students should leave able to configure memory conservatively, recognize paging and swapping as performance warnings, and explain the tradeoffs behind memory overcommitment.
Learning objectives
By the end of the lesson, students should be able to:
- Explain memory as workspace, cache, and a fast form of storage.
- Describe pages, paging, swap space, guest-to-host mapping, and hypervisor overhead.
- Configure VM memory and identify hot-add and reboot requirements.
- Explain ballooning, memory overcommitment, page sharing, copy-on-write, and compression.
- Distinguish allocated memory from actively used or physically resident memory.
- Use measurements and reserve capacity to decide whether memory optimization is safe.
1. What memory does
RAM is a fast workspace for the operating system, program instructions, and data being processed. Frequently used routines stay in memory so the CPU can access them quickly. Video, games, streaming, databases, and ordinary applications use memory as a buffer or scratchpad as well as a place to hold active code and data.
The guest OS in a VM uses memory the same way a physical OS does. The difference is that the guest sees the amount configured for the VM, while the hypervisor controls how that view maps to the host’s physical RAM. A VM configured with 1.5 GB does not automatically gain access to every gigabyte installed in the host.
2. Pages, paging, and swap
Operating systems divide memory into blocks called pages. A common page size is 4 KiB, although the architecture and operating system can support other sizes. When active memory is full, less recently used pages can be written to disk so that RAM can hold newer work. This movement between memory and disk is paging. The disk area reserved for those pages is commonly called swap space or a page file.
Paging is expensive because disk I/O is much slower than RAM. SSDs improve the storage path, but they do not make swapping equivalent to keeping working data in memory. Sustained paging or hypervisor swapping is a sign that the configuration, workload, or host capacity needs attention.
Keep these measurements distinct:
- Configured or allocated memory: what the VM is told it can use.
- Consumed or actively used memory: what the guest and hypervisor are currently using.
- Resident memory: what is backed by physical RAM at a given moment.
- Reserved memory: capacity protected for a VM or platform function.
- Overhead: host memory used for hypervisor processes and mapping structures.
The guest’s memory display and the host’s memory display answer different questions. Compare them when diagnosing performance.
3. Configure VM memory
At the VM level, the primary memory setting is the amount assigned. Hypervisor interfaces may show minimum, recommended, and maximum values based on the guest OS and host capacity. Use those suggestions as guardrails, then size from application requirements and observed demand.
Adding memory may be possible while the VM runs when the hypervisor and guest support hot-add. Some Windows Server releases and Linux distributions can recognize hot-added memory, although Linux may require the new memory to be brought online. Removing memory generally requires a reboot, and some Type 2 platforms require a reboot even when the guest technically supports hot-add.
A safe change process is:
- Confirm the workload’s memory requirement and current pressure.
- Check host reserve and neighboring VMs.
- Change the VM setting within supported limits.
- Complete the required guest action, such as online memory or a reboot.
- Verify the guest and host values and monitor the application.
Do not set every VM to the host’s maximum. A large allocation can waste capacity when the guest does not use it and can reduce the number of workloads the host can support.
4. Account for hypervisor overhead
The hypervisor needs memory for its own code and operations. Every VM also creates mapping tables that connect guest memory addresses to physical addresses. Modern platforms use much less overhead than earlier systems, but overhead still matters when a host is densely consolidated.
For a planning illustration, the book uses a 16 GB host, two VMs allocated 4 GB and 2 GB, and an overhead estimate of about 1 GB. That leaves roughly 9 GB for additional guests after the first allocations and overhead. The values are intentionally simple. Actual overhead depends on the hypervisor, guest configuration, device model, memory size, page size, and active features. Use current vendor calculators or measurements for production sizing.
Leave reserve for growth, bursts, host services, maintenance, and recovery. A host that runs at its theoretical maximum has no room to absorb a spike or a failed peer.
5. Ballooning and memory reclamation
Allocated memory is not always permanently reserved for one VM. When the host experiences contention, the hypervisor may reclaim memory from a guest. Ballooning uses a guest-aware balloon driver. The hypervisor tells the driver to inflate, and the guest OS identifies less recently used pages and flushes them to its page file or swap area. The hypervisor then reclaims the physical pages for another VM.
Ballooning works with the guest’s knowledge and page-selection logic, but it can still cause paging and performance impact. It usually activates during contention, not as a free performance boost. Monitor the guest and host for ballooning, swapping, latency, and application response.
6. Memory overcommitment
Memory overcommitment allocates more virtual memory to VMs than the host physically contains, relying on the fact that guests rarely use every allocated byte at the same time. Ballooning, page sharing, compression, and controlled swapping help the hypervisor manage that difference.
Example: a host with 16 GB of physical memory might run VMs with an aggregate allocation of 24 GB if their active working sets are smaller and contention remains controlled. The allocation ratio is not a promise that 24 GB of simultaneous active working data can run at full speed on 16 GB. Overcommitment is safe only when workload behavior, reserve policy, alerts, and recovery plans support it.
Conservative environments may use ratios around 1.5:1 or 2:1, while specialized, well-understood workloads can differ widely. Do not copy a ratio from another host without measuring the working sets and service-level impact.
7. Page sharing and copy-on-write
Many VMs run the same guest OS and applications, so some memory pages contain identical read-only code. Page sharing stores one physical copy and maps multiple guests to it. This is similar to storage deduplication. It can also find identical pages within one VM.
If a guest tries to modify a shared page, the hypervisor creates a private copy for that VM. This is copy-on-write. The technique preserves isolation while saving memory for pages that remain identical. It is most useful when many VMs have similar operating systems or application images, such as virtual desktop infrastructure or large pools of identical web servers.
The textbook reports savings ranging from about 10 percent to more than 40 percent in suitable environments. Treat that as an example range, not a guarantee. Current platforms may limit or change transparent page sharing behavior because of security, workload, or performance considerations.
8. Compression and swapping
Memory compression stores compressed pages in a reserved physical-memory cache. Compression and decompression generally cost less than writing and reading pages from disk, so compression can delay swapping during short periods of pressure.
Hypervisor swapping is a last-resort action. If ballooning and other reclamation methods do not free enough RAM, the hypervisor can write a VM’s memory pages to disk. The VM may continue running, but latency can become severe because every page fault can require storage I/O. Alerts should distinguish normal allocation from active reclamation and swapping.
The slide deck lists overcommitment, ballooning, transparent page sharing, and compression as optimization techniques. Availability differs by hypervisor and release, so compare current documentation before designing around one vendor’s feature set. Older course examples show VMware vSphere, Hyper-V, and Xen with different capabilities; treat that table as a comparison of the cited releases, not a current product matrix.
Classroom application
Have students build a memory-sizing worksheet for a host and three VM roles. Record:
- Guest allocation, active use, resident use, and application working-set evidence.
- Hypervisor overhead and a reserve for growth or recovery.
- Whether hot-add is supported and what guest action follows it.
- The signal that would indicate ballooning, compression, or swapping.
- Whether overcommitment is appropriate for the workload and what limit or alert should stop further allocation.
Use a simple capacity sketch: start with a 128 GB host, subtract platform overhead and reserve, then compare the result with the VMs’ allocated and measured active memory. Ask students to explain why the answer changes when identical guests share pages or when the workload begins using its full allocation.
Checks for understanding:
- Why is a guest’s configured RAM not the same as its host’s physical consumption?
- Why is paging slower than keeping a page in RAM?
- What does ballooning ask the guest OS to do?
- Why does copy-on-write preserve isolation?
- What evidence would make you stop increasing the overcommit ratio?
Common misconceptions
- “A VM allocated 4 GB permanently owns 4 GB of host RAM.” Allocation and physical residency can differ.
- “Swap makes disk as fast as memory.” It preserves operation at a significant performance cost.
- “Overcommitment creates memory.” It depends on inactive or shared pages and can fail under simultaneous demand.
- “Ballooning is always beneficial.” It is a contention response that can cause guest paging.
- “Page sharing means guests share writable data.” Shared pages become private through copy-on-write before a guest modifies them.
Lesson summary
Memory is the VM resource most likely to limit consolidation. The hypervisor maps guest pages to physical RAM, reserves overhead, and may reclaim or optimize memory when workloads compete. Right-size allocations, preserve reserve capacity, and treat paging and swapping as warnings. Ballooning, overcommitment, page sharing, copy-on-write, and compression can increase effective utilization, but each depends on workload behavior and platform support. Good memory management combines current vendor guidance with host metrics, guest metrics, and application response evidence.
Course Notes