Virtualization
Memory Ballooning Explained: virtio-balloon, Overcommit and Your VPS RAM
By NileVPS Team · Published · 5 min read
اقرأ المقال بالعربيWhen you buy a VPS with 4 GB of RAM, you expect four gigabytes that are yours. Under the hood, though, the hypervisor has several tools for moving memory between virtual machines, and one of the oldest is memory ballooning. Used carefully, it helps a host run efficiently. Used aggressively, it lets a provider sell more RAM than physically exists, and the guest pays for it in swapping and stalls. This article explains how ballooning works in KVM, what overcommit really means, how to read memory numbers inside your server, and what signs suggest your RAM is not as dedicated as advertised.
Why hypervisors want to move memory around
A physical host has a fixed amount of RAM. Each VM is given a configured maximum, but most guests do not use all of it all the time. Much of it sits in the page cache, holding file data that could be dropped and re-read from disk. From the host's point of view, that memory is "allocated" but not doing much. If the host could reclaim idle guest memory and give it to a VM that needs it, total utilization would improve.
The difficulty is that the hypervisor cannot see inside the guest. It does not know which guest pages are free, which hold cache, and which hold a database's working set. Swapping guest memory out from the host side is blind and slow. Ballooning solves this by asking the guest kernel to cooperate.
How virtio-balloon works
The balloon is a paravirtualized device, virtio-balloon, with a driver inside the guest kernel (virtio_balloon on Linux, and a balloon service in the virtio-win drivers on Windows).
Inflating the balloon
When the host wants memory back, it sets a target size for the balloon. The guest driver then allocates pages from the guest kernel, just like any other kernel allocation, and reports their physical addresses to the host. The guest can no longer use those pages, and the host is free to unmap them and give the underlying RAM to someone else. From inside the guest, it looks as if a driver suddenly grabbed a large block of memory.
Because the guest kernel performs the allocation, it chooses what to give up. It frees cache first, then reclaims other memory, and only swaps or invokes the OOM killer if pressure is high enough. That is far smarter than host-level swapping.
Deflating the balloon
When the host lowers the target, the driver releases pages back to the guest kernel, and available memory rises again.
Free page reporting and stats
Modern implementations add two helpful features. The balloon device can expose guest memory statistics to the host (free memory, swap activity, major faults), and free page reporting lets the guest tell the host about free pages so the host can reclaim them without forcing the guest to give up memory it might soon need.
Overcommit: the honest and dishonest versions
Overcommit means the sum of RAM configured across all VMs exceeds the physical RAM on the host. Mechanisms that make this possible include ballooning, Kernel Samepage Merging (KSM, which deduplicates identical pages), and host swap.
There is a mild form of overcommit that is mostly harmless: a host that occasionally reclaims idle page cache, with enough headroom that no guest ever loses memory it actively needs. There is also an aggressive form, where a host is packed so tightly that balloons stay inflated permanently and guests constantly fight for memory. In that case, the "4 GB" plan behaves like a 2.5 GB plan with an expensive swap file.
This is why provider honesty matters. A plan that states dedicated or guaranteed RAM should mean that memory is backed by physical RAM reserved for you. Before you choose a plan on /pricing, it is fair to ask whether memory is guaranteed and how the host handles pressure.
Reading memory correctly inside your server
Many users misread memory numbers, so start with the basics:
free -mThe columns that matter:
- total: what the kernel can manage. Depending on the balloon features negotiated, inflated pages are either subtracted from total or counted as used, so a full balloon can show up in either column.
- used: memory in use by processes and the kernel.
- buff/cache: file cache, which is reclaimable.
- available: the kernel's estimate of memory you can still use without swapping. This is the number to watch, not free.
Low free with high available is healthy; Linux uses spare RAM as cache on purpose.
What this means for your VPS
Check whether a balloon device exists and how much memory it is holding:
# Is the balloon driver loaded?
lsmod | grep virtio_balloon
lspci | grep -i balloon
# Compare configured and usable memory
grep -E 'MemTotal|MemAvailable' /proc/meminfo
sudo dmesg | grep -i -E 'memory|balloon'If the configured RAM is 4096 MB but MemTotal or available drops sharply while your own processes stay the same, the balloon may be inflated. Look for signs of real pressure:
vmstat 1 10 # si/so columns > 0 means swapping
cat /proc/pressure/memory # PSI: some/full stall percentages
sar -B 1 5 # major page faults (sysstat package)Steady swap-in activity, rising major faults, and non-zero full values in /proc/pressure/memory on an otherwise idle server are warning signs. Also check CPU steal time with top (the st value), because overloaded hosts usually show both symptoms together. For memory-hungry databases, also read our guide on HugeTLB vs Transparent Huge Pages, since huge pages interact with ballooning: pages reserved as HugeTLB cannot be reclaimed by the balloon.
FAQ
Can I disable memory ballooning from inside my VPS?
You can unload the virtio_balloon module, but that is not a real solution: the host may then fall back to blind host-side swapping, which is worse. The right fix is a plan with guaranteed RAM that is not overcommitted in the first place.
Is ballooning always a sign of a bad provider?
No. A balloon device can be present simply to collect memory statistics or report free pages, and occasional reclaim of idle cache is reasonable. The problem is permanent inflation that takes memory your applications need.
Why does free -m show almost no free memory on a quiet server?
Linux fills spare RAM with file cache to speed up disk reads. That cache is released instantly when applications need memory. Watch the available column instead, along with swap activity and memory pressure.