KVM and Swap space

kvmswapvirtualization

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?

Best Answer

"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.

Related Question