zRAM and LXC working

Hi there! I have some questions about work of zram and LXC containters.
I have:

  • ubuntu 20.04, lxc 4.0.2, zram-tools

  • /dev/sdb with LVM where located test container

# swapon --show
/dev/zram0 partition 512M 1.9M  100
/dev/zram1 partition 512M 1.8M  100
/dev/zram2 partition 512M 1.5M  100
/dev/zram3 partition 512M 1.7M  100
/dev/zram4 partition 512M 1.6M  100
/dev/zram5 partition 512M 1.9M  100
/dev/zram6 partition 512M 1.9M  100
/dev/zram7 partition 512M 1.9M  100

# free -m
              total        used        free      shared  buff/cache   available
Mem:          32087         648       30636           2         803       31043
Swap:          4095          14        4081

# lxc-create -t download -n test1 -B lvm --vgname vg1 --fssize 6G --fstype ext4 -- -d centos -r 7 -a amd64
# lxc-start test1
# lxc-cgroup -n test1 memory.limit_in_bytes 1G
# lxc-attach test1
# yum install -y numpy
# cat <<EOF > memtest.py
import numpy
result = [numpy.random.bytes(1024*1024) for x in xrange(1900)]
print len(result)
# chmod +x memtest.py
# while true; do ./memtest.py ; done

And what i see? I see IO usage of sdb when container is swapping. Screens below.

Why is this happening? How to debug it? How to avoid it?

To prevent swap use from a container, you need to enable the cgroup swap controller with swapaccount=1 on the kernel command line.

With that done, you’d then want to set both limit_in_bytes and memsw.limit_in_bytes to the same value (memsw.limit_in_bytes is the amount of RAM+SWAP).

This won’t fully prevent swapping though but will properly restrain container memory.
To further limit the change of swapping, you can set memory.swappiness in the cgroup such that it is extremely unlikely to swap.


Hi, @stgraber ! Glad to see you again!
There is very strange behavior with lvm… when i create container with loop device lxc-create -t download -n test2 -B loop --fssize 6G --fstype ext4 -- -d centos -r 6 -a amd64 - the problem is disappear. Swap is using but there is no IO usage at all.