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.


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.

  • 4
    There is no automatic=true option for device virtio-balloon. libvirt's autodeflate corresponds to deflate-on-oom=on. – Piotr Jurkiewicz Apr 30 '18 at 4:53

cron this in the VM and/or VM host every night after busy hours:

sync; echo 1 > /proc/sys/vm/drop_caches

it drops the fs cache and gives it back as available memory to the system then the KVM/Hypervisor can see it as free memory and find other uses for it

also works very well with docker systems

  • 2
    This just kills your performance to no purpose. The cache was already available to the system if it actually needed it. – Michael Hampton Aug 2 '19 at 16:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.