How is SWAP handled by LXC?

Ovidiu

Renowned Member
Apr 27, 2014
324
12
83
When creating a new LXC, should we allocate swap? How does Proxmox treat that? If I give a machine "GB RAM + 2GB SWAP does the machine actually have access to 4GB RAM or is its swap somehow passed through to a real swap file on the Proxmox root fs?
 
  • Like
Reactions: MichelGalle
4G RAM + 2G swap means when it goes over 4G RAM usage it'll start swapping, and if it exceeds the swap space it'll get killed.
The container doesn't have its own swap file anywhere, so in order for the swapping to work you need to have an actual swap active on your host.
 
so if I have 2 containers with 2GB swap each, then do I add that amount to the swap of my host? so the host has to have its own swap + 4GB extra?
 
Unless you want to over-commit. Which IMO is a reasonable choice when it comes to swap.
But it really depends on your needs.
As a rule of thumb I'd say: if you leave enough RAM unassigned that your host won't need swapping then over-committing swap for CTs is a perfectly fine choice (unless the involved CTs need to be HA). Otherwise if you might end up requiring swapping on your host, it's probably best to have enough swap that things don't get killed randomly.
 
ah, I think I understand: so if I have enough uncommitted RAM on the host it will be used for the swap requested by the CTs :)

Thanks for clarifying.
 
No, you misunderstood. RAM is never used as swap. I mean that - assuming your CTs use up all their assigned RAM - if your host then still has enough RAM to function (because you assigned less than you physically have), then most of the time you don't need to worry about CTs filling up the swap space either.

In any case: With regards to utilization and limits, RAM and Swap are 2 independent things. Each shared between host and guest. Each limited to the assigned value. Whether or not you actually have enough that they can all use everything at the same time is a different matter, and it is what you use to decide how much swap you want/need.
If you assign swap to a container but don't actually have any swap partitions activated, then they will not have any actual swap space and will be out-of-memory killed when they reach their RAM limit.
 
OK, we're getting somewhere. I understood RAM is never used for swap but I'm still confused about the SWAP. Can we use an example?

Host: 8GB RAM, 2GB SWAP
Guest1: 2GB RAM, 2GB SWAP
Guest2: 2GB RAM, 2GB SWAP

So the host has plenty of free RAM and now lets say the guests simultaneously reach their RAM limits and need more so they will try and use swap but their total amount of swap is 2GB+2GB=4GB while the host only has 2GB available.

What happens in this case? Would this still work out if I gave the Host 4GB swap?
 
Processes in the guest should get out-of-memory-killed in this case if the swap is full. They could of course each go up to 1G swap usage, or one uses 2G the other 0G before they get killed.
 
If your swap resides on a disk which is also in use by the host a lot, then yes.
 
I think how swap is handled by LXC is stupid. You swap on slow harddrive even when you have plenty of fast RAM space available on the guest. This eats up precious IO (and basically kills database performance for all containers). OpenVZ does it the opposite way. They have vSwap, which is rate limited swap in RAM, which can be used by guests. Once the RAM gets full, this vSwap gets swaped on harddisk. So it does not screw up the host IO unless necessary.
 
I think how swap is handled by LXC is stupid. You swap on slow harddrive even when you have plenty of fast RAM space available on the guest. This eats up precious IO (and basically kills database performance for all containers). OpenVZ does it the opposite way. They have vSwap, which is rate limited swap in RAM, which can be used by guests. Once the RAM gets full, this vSwap gets swaped on harddisk. So it does not screw up the host IO unless necessary.

Best to use zRAM which does exactly the same as your described way of vSwap.
 
But it does not rate limit the swap, so if container swaps lot, it can eat all your CPUs.

Okay, yes, that's right ... but normally you have much more CPU power than disk power. It'll offload your disk bottleneck to a probably CPU bottleneck.
 
As a confirmational FYI, from the pct man page :


Memory
Container memory is controlled using the cgroup memory controller.

memory: Limit overall memory usage. This corresponds to the
memory.limit_in_bytes cgroup setting.

swap: Allows the container to use additional swap memory
from the host swap space.
This corresponds to the
memory.memsw.limit_in_bytes cgroup setting, which is
set to the sum of both value (memory + swap).
 

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!