Consider 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 configuration, 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?

3 Answers 3


"Occasionally 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)
  • 2 GiB 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 2 GiB 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
    Commented Feb 4, 2015 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
    Commented Feb 11, 2015 at 9:28
  • 6
    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? Commented Apr 25, 2016 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.) Commented Sep 18, 2018 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.


The accepted answer is wrong:

You should also enable swap space on the VM, so it is aware that RAM is being swapped and avoid using it for disk cache and IO buffers.

If you only enable swap on the host, the VM will believe it has available real RAM and will use it, while it will be swapped on the host creating unnecessary bottlenecks and freezes. This is the biggest mistake you could do.

You should also use a different virtual drive as a swap disk, since you don't want to snapshot/backup it together with your other data drives.

Since swap space can be volatile, you could also use cache=unsafe or use zram/zswap for better performance.

Also remember that swapping is good, since it frees inactive RAM that can later be reused for disk cache and IO buffers.

  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented May 17, 2022 at 16:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .