A hypervisor has 256 GB of RAM and 38 VMs whose <memory> totals 380 GB on paper. On a Tuesday morning, RAM utilization on the host hits 92 percent — even though most guests are idle, sitting on memory they grabbed during a quarterly batch job last weekend and never released. The operator looks at memory ballooning to claw back the idle gigabytes without rebooting the guests. The first attempt either does nothing or sends a database VM into OOM. This article is what the balloon driver actually does, when it works, when it does not, and how to wire it without surprising the guest kernel.
How to verify
A guest with the virtio-balloon driver loaded shows it in lsmod and exposes a /sys/devices/virtio*/virtio_balloon0/ tree:
# Inside the guest
lsmod | grep balloon
# virtio_balloon 24576 0
ls /sys/devices/virtio*/virtio_balloon0/ 2>/dev/null
# (the driver registers a virtio device on the guest's virtio bus)
On the host, the balloon device shows up in the domain XML and the live values are visible through virsh:
# Host, libvirt
virsh dommemstat my-guest --period 5
# actual 16777216 # total guest memory in KiB
# rss 14502912
# available 16777216
# unused 9512432 # the number the balloon could potentially reclaim
# usable 8201024
virsh setmem my-guest 8G --live
# Inflates the balloon to release ~8G back to the host
If dommemstat returns only the most basic lines and unused and available are missing, the guest does not have the balloon driver loaded — without the driver the balloon device exists on the bus but nothing inflates it. If setmem returns successfully but the host RSS for the qemu process doesn’t drop, the balloon inflated but the kernel hasn’t released the pages yet (or the guest pinned them).
What’s happening
Memory ballooning is a paravirtual trick. The host wants memory back from an over-provisioned guest; it can’t simply take it because the guest’s kernel doesn’t know which pages are safe to lose. Instead, the host asks the balloon driver inside the guest to allocate a chunk of guest memory (the “balloon”), pin it as unavailable to the rest of the guest, and report back which physical pages those are. The host then knows those guest-physical pages are not being used by any guest workload and can deallocate the corresponding host-physical pages — handing the RAM back to the host pool.
To return memory to the guest, the host deflates the balloon: the driver releases the pinned pages, and the guest sees its available memory grow again. The host has to back those pages with real RAM before the guest tries to touch them, otherwise the guest faults and the qemu process is killed.
The crucial mechanism: ballooning lets the host adjust the effective memory a guest has access to, between zero and the configured <memory> ceiling, without the guest needing to be rebooted. The configured <memory> is the max; the balloon target (or <currentMemory> in libvirt) is the actual.
The catch: ballooning only reclaims memory the guest’s kernel considers free or cleanly evictable — page cache, slab caches that can be dropped, unallocated heap. It cannot reclaim memory the guest has actively committed (anonymous pages a process holds, an in-flight allocation, kernel data structures). If you tell the balloon to inflate beyond what the guest can safely give up, the balloon driver itself ends up triggering the guest’s OOM killer, killing one of the guest’s user processes. The kernel logs virtio_balloon: out of memory or the OOM killer picks the largest process — typically your database.
The procedure
-
Confirm the balloon device is in the domain XML. On modern libvirt this is added by default for QEMU/KVM guests. If a guest was created with an older toolchain, the device may be missing:
<devices> <memballoon model='virtio'> <stats period='10'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/> </memballoon> </devices>The
<stats period='10'/>enablesvirsh dommemstatreporting at 10-second granularity. Without it, the host can balloon blind. -
Confirm the guest driver is loaded. On Linux guests, virtio-balloon ships in the kernel (
modprobe virtio_balloonif not autoloaded). On Windows guests, install the balloon driver fromvirtio-win.isoand theBlnsvrservice — without the service, the driver is loaded but reports nothing. -
Set
<currentMemory>below<memory>to start the balloon inflated. The guest boots with<currentMemory>worth of RAM; the balloon holds the difference:<memory unit='KiB'>16777216</memory> <!-- 16 GiB ceiling --> <currentMemory unit='KiB'>8388608</currentMemory> <!-- 8 GiB start -->virsh setmem my-guest 12G --livelater inflates or deflates to land at 12 GiB live. -
Inflate the balloon gradually. Going from 16 GiB to 8 GiB in one call asks the guest to evict 8 GiB of pages instantly. The kernel may not have 8 GiB of clean pages available. Walk it down in 1-2 GiB steps with 30-60 seconds between steps:
virsh setmem my-guest 14G --live sleep 60 virsh setmem my-guest 12G --live sleep 60 virsh setmem my-guest 10G --liveWatch
dommemstatunusedbetween steps. Ifunusedis below 1 GiB, stop — the guest does not have evictable memory and further inflation will pressure it. -
Never balloon a database guest into its working set. PostgreSQL, MySQL, MongoDB, Elasticsearch — all keep a buffer pool in anonymous memory the OS cannot evict. If you balloon below
shared_buffers + work_mem * connections + OS overhead, the balloon driver itself triggers OOM. For DB guests, either disable the balloon (<memballoon model='none'/>) or set a balloon floor enforced in your automation —currentMemorynever goes below the documented working-set size. -
For automatic ballooning, use the auto-balloon daemon. Libvirt itself doesn’t auto-adjust; tools like
vmnetd(oVirt’s MOM), Proxmox’s auto-balloon, or your own script pollingdommemstatare how memory pressure on the host drives balloon decisions. The pattern: when host free RAM<20%, walk down the largest idle guests’ balloons proportionally to theirunusedvalue.
Common pitfalls
-
Ballooning a Windows guest with no balloon service installed. The driver is loaded, the device exists,
setmemsucceeds, nothing changes. The Blnsvr Windows service is what actually inflates the balloon — installing the driver alone is not enough on Windows. RunBlnsvr.exe -ifrom the virtio-win ISO. -
Treating the balloon as a memory reservation tool. It is not. The balloon takes back memory from the guest at the host’s request; it does not guarantee the host can give it back later. If the host is over-committed and the guest tries to grow, the deflation may fail and the guest’s allocations stall or fault.
-
Ballooning under hugepages. If the guest is backed by 1 GiB hugepages, the balloon cannot reclaim partial-page chunks — it can only release whole hugepages. In practice, balloon + hugepages reclaims memory only in 1 GiB units, far less responsive than 4 KiB pages.
-
Forgetting that
<currentMemory>resets on reboot. A livesetmemthat takes a guest from 16 GiB to 8 GiB does not persist. After reboot, the guest comes back at its<currentMemory>value from the XML. If you want the change persistent, update the XML withvirsh setmem --config --live. -
No alerting on guest OOM caused by balloon pressure. The balloon driver writes to the guest’s kernel log when it kills processes; the host has no visibility unless you ship guest logs or alert on the host’s
oom_killevents for the qemu process. Wire the alert.
Operational notes
Balloon-driven overcommit lets a 256 GiB host comfortably run 380 GiB of nominal guest memory when guests are idle most of the time. It does not let you overcommit on workloads that all peak at once; that requires either swap on the host (slow), KSM page-sharing (helps for identical pages but doesn’t reclaim arbitrary working sets), or buying more RAM. Ballooning is the cheap win for an estate where ten guests are quiet for every one that is busy.
We operate KVM-based VM estates with balloon, KSM, and hugepage policies tuned per workload — and a database-guest exception list to keep the OOM killer off the boring path. See Managed Cloud Operations.