1

I would like a certain Linux guest running in QEMU to release memory before the host is under memory pressure. How can I manually trigger the virtio balloon driver on a guest so that it releases as much memory as possible?

  • 1
    First, the size of the balloon is controlled by the host, not the client; as far as I can see there is no way to increase the balloon from the clients side. Second, what is "as much memory as possible"? The host may reclaim memory from the client even if the client has to clear its buffers and even start swapping. – ridgy Dec 28 '17 at 17:24
1

Something like this seems to work: first reduce the amount of memory available to the guest, then (optionally) restore it to the original setting.

virsh --connect qemu:///system qemu-monitor-command --domain NAME --hmp 'balloon 512'
# Wait for the balloon driver to release some memory
sleep 2
virsh --connect qemu:///system qemu-monitor-command --domain NAME --hmp 'balloon 4096'

Or, as a proper script:

#!/bin/bash

set -eu

domain=$1
# How many MB to temporarily reduce the guest memory to; don't set this too
# low or the OOM killer will start killing.
reduced_memory=$2

hmp() {
    virsh --connect qemu:///system qemu-monitor-command --domain "$domain" --hmp "$1"
}

original_memory=$(hmp "info balloon" | grep = | cut -d '=' -f 2)
hmp "balloon $reduced_memory" > /dev/null
# Wait for the balloon driver to release some memory before setting it back
sleep 2
hmp "balloon $original_memory" > /dev/null

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.