qemu-kvm reclamation of memory from low-use guests

Is there a way to transparently reclaim, or prevent usage of, memory from a qemu-kvm guest?

Scenario A:

A guest uses 8GB of RAM during peak hours, it returns back to 4GB of usage at night, but the guest is still claiming 8GB of RAM from the host.

Scenario B:

A guest uses 2GB of RAM, but has 6GB of RAM in page cache. The guest is claiming 8GB of RAM from the host, but up to 6GB of it could be RAM that could be have more importance in another guest system.


My first thoughts on ballooning was that the guest would not "see" the ballooning effect, but when sending "balloon 2048", the memory inside the guest is actually reduced to ~2GB.


Solution 1:

You can turn on automatic ballooning in a libvirt guest by editing the XML:

<memballoon model='virtio'>

The virtual memory balloon device takes a parameter autodeflate, which defaults to off. Set it to on to enable automatic ballooning.

<memballoon model='virtio' autodeflate='on'>

Note that there is no GUI option in virt-manager to enable or disable this setting.

If you're calling qemu manually (please save your sanity and don't) then add ,automatic=true to the balloon device, e.g. -device virtio-balloon,automatic=true.

Autoballooning requires qemu/kvm 1.3.1 or later.

Note that this probably won't solve all your memory contention problems; it's best to simply have enough RAM to run all your virtual machines.