Swap on host and containers

Morphushka

Well-Known Member
Jun 25, 2019
49
7
48
35
Syberia
Hello!

1) From documentation:
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).
Q: As I understand, host swap space is reclaiming to containers depends on their swap setting. Is it right ? If not, where is containers swap come from ?

2) Here is my host and some containers mem/swap info. Sirius - is the host. Host has 8Gb swap. Behavior I can't clear understand: if containers memory equal or higher host swap size, then container swap is 8Gb, even If I set different value in containers setting. For example, for zabbix I have 512Mb swap setted.
Q: Does container shows host swap total size anyway and just not use more then in settings ?

3) xfiles container has 0 swap size in settings, but free command shows that container use 795Mb.
Q: Why is it still use swap?

Code:
root@sirius:~# free -h
              total        used        free      shared  buff/cache   available
Mem:           62Gi        13Gi       859Mi         9Gi        49Gi        38Gi
Swap:         8.0Gi       5.6Gi       2.4Gi
root@sirius:~# pct enter 102
root@zabbix:~# free -h
              total        used        free      shared  buff/cache   available
Mem:            10G        1.6G         68M        6.8G        8.4G        8.4G
Swap:          8.0G        182M        7.8G
root@zabbix:~# exit
exit
root@sirius:~# pct enter 104
root@xfiles:~# free -h
              total        used        free      shared  buff/cache   available
Mem:           4.0G        1.1G        795M         92M        2.2G        2.9G
Swap:          4.0G        795M        3.2G
root@xfiles:~# exit
exit
root@sirius:~# pct enter 105
root@oscam:~# free -h
             total       used       free     shared    buffers     cached
Mem:          8.0G        84M       7.9G        14M         0B        48M
-/+ buffers/cache:        35M       8.0G
Swap:         8.0G       4.4M       8.0G
root@oscam:~# exit
exit
root@sirius:~#
 
Hello!

1) From documentation:

Q: As I understand, host swap space is reclaiming to containers depends on their swap setting. Is it right ? If not, where is containers swap come from ?
Yes it comes from the host and the available swap space is shared between host and containers.
2) Here is my host and some containers mem/swap info. Sirius - is the host. Host has 8Gb swap. Behavior I can't clear understand: if containers memory equal or higher host swap size, then container swap is 8Gb, even If I set different value in containers setting. For example, for zabbix I have 512Mb swap setted.
Q: Does container shows host swap total size anyway and just not use more then in settings ?
The container can use up to "memory.memsw.limit_in_bytes"=(memory limit + swap limit) of memory and swap space. The limit is shared: e.g. when the limits for swap and memory are set to 1G each, the container could actually use 1.5G of swap space. But then it can only use 500M of memory before it hits the limit. Here's an example of it.
3) xfiles container has 0 swap size in settings, but free command shows that container use 795Mb.
Q: Why is it still use swap?
As stated above, there is only a common limit for memory+swap.
Currently the only way to completely disable swapping in containers in PVE is to turn of swapping on the host. This might change in the future with cgroups v2 though.
Code:
root@sirius:~# free -h
              total        used        free      shared  buff/cache   available
Mem:           62Gi        13Gi       859Mi         9Gi        49Gi        38Gi
Swap:         8.0Gi       5.6Gi       2.4Gi
root@sirius:~# pct enter 102
root@zabbix:~# free -h
              total        used        free      shared  buff/cache   available
Mem:            10G        1.6G         68M        6.8G        8.4G        8.4G
Swap:          8.0G        182M        7.8G
root@zabbix:~# exit
exit
root@sirius:~# pct enter 104
root@xfiles:~# free -h
              total        used        free      shared  buff/cache   available
Mem:           4.0G        1.1G        795M         92M        2.2G        2.9G
Swap:          4.0G        795M        3.2G
root@xfiles:~# exit
exit
root@sirius:~# pct enter 105
root@oscam:~# free -h
             total       used       free     shared    buffers     cached
Mem:          8.0G        84M       7.9G        14M         0B        48M
-/+ buffers/cache:        35M       8.0G
Swap:         8.0G       4.4M       8.0G
root@oscam:~# exit
exit
root@sirius:~#
 

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!