suppose the following scenario: a host with 2 GiB runs a few guests using KVM. Each guest does usually not need much memory, they are given 256 MiB each and run services that mostly twiddle their thumbs. However, occasionally the guests need more memory. Right now, each guest has little RAM but its own swap space. I noticed that a small portion of swap is used. I never had problems with that konfiguration, but just out of curiosity:

What is the optimal swap allocation strategy?

  1. Assign each guest its own swap space from their respective disks, and assign the guests only little memory from the host. (This is what I am doing now.)
  2. Assign the host a larger amount of swap space and none to the guests, and assign more memory to the guests.

Would memory ballooning help to improve memory performance?


"Occasional guests need more memory" sounds like a good application of overcommitting memory. The idea is you assign each guest a large amount of memory (more than you can actually give out) because they're generally not using it. Then you do the math to ensure you have enough swap space that the guests can actually swap out to disk in the worst-case-scenario where they all actually do use all that memory.

The swap space goes on the host machine, and it needs to obey

host swap space = sum of all guest memory + recommended host swap space

in order for it to be safe.

So if you have 10 guests and 2 GiB of RAM, you could experiment with something like

  • 512 MiB RAM per guest (512 * 10 = 5120 MiB total)
  • 2GiB swap on the host

Meaning your host swap space should be at least 512 * 10 + 2048 = 7168 MiB to handle this safely, assuming you can dedicate 2GiB of swap to the host (for that little host memory, this is recommended).

Always test these kinds of setups first to make sure your machine can handle them. Benchmarking them is even better, and will allow you to experiment with different loadouts and choose the one that works best.

  • The formula above seems overkill to me (I'm no expert). If I have a dedicated 32GB host with 20 x 2GB guests, then I'd expect to need 8GB host swap to handle the overcommit, and then a bit more for the host operating system (which presumably won't be making large allocations for itself as it's just managing its guests). ie I'm suggesting a formula like host swap space = sum of all guest memory - host RAM size + appropriate host swap space. In your example above I'd expect ~4GB swap to be sufficient. Am I missing something?! – Andy Feb 4 '15 at 11:44
  • 1
    My recommendation is conservative since I'm just giving a starting point for benchmarking. Red Hat suggests (0.5 * RAM) + (overcommit ratio * RAM) = Recommended swap size, where overcommit ratio = total guest RAM / host RAM. In my example this works out to 0.5 * 2048 + (5120 / 2048) * 2048 = 6144 MiB host swap, which is the same answer as my formula if you assume a recommended host swap ratio of 1.0 (vs. the 0.5 they used). – ndt Feb 11 '15 at 9:28
  • 3
    Won't the guests think they have more physical RAM than they really have, and then do stupid things like caching files in "RAM" only to have that be stored in host swap? – James Johnston Apr 25 '16 at 13:49
  • Red Hat's formula is from a time when RAM size was typically much smaller than the size of the local disk(s). Today, it's not unusual to have hosts with 512 GB RAM but only a 300 GB disk. (The storage for the VMs comes from a storage network such as ceph.) – Joachim Wagner Sep 18 '18 at 10:11

@ndt has a good procedure. My scenario is different, so I adopt a different method.

My server has many guests, all of them run a single application, usually web/mail/file server. The data inside is very important, so backups and snapshots are made very often. So the smaller and simpler the disk image is better. So I actually don't create a swap partition, but a swap file. Since I have no guests with high processing or even memory needs, they have a small disk storage and small memory. The swap file goes inside of rootfs.

This setup saves a lot of space and simplify maintenance. As I said, it's different from others situations.

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.