Despite having 32GB of RAM on my Debian Unstable system, I was experiencing random stalls and freezes triggered by memory pressure alerts from psi-notify. Sometimes the system would recover after a few seconds, but often it required a hard reboot. The solution was to expand my existing 4GB encrypted swapfile to 16GB and add ZRAM for compressed swap in RAM.
This builds on my previous guide for setting up encrypted swap on Btrfs.
Expand Encrypted Swapfile
First, expand the existing encrypted swapfile from 4GB to 16GB:
# Turn off current swap
sudo swapoff /dev/mapper/swap
sudo systemctl stop systemd-cryptsetup@swap.service
# Remove old swap file
sudo rm /swap/swapfile
# Create new 16GB swap file
sudo btrfs filesystem mkswapfile --size 16g /swap/swapfile
# Reload and restart encrypted swap
sudo systemctl daemon-reload
sudo systemctl start systemd-cryptsetup@swap.service
sudo swapon /dev/mapper/swap
Add ZRAM for Compressed RAM Swap
Install ZRAM tools:
sudo apt update
sudo apt install -y zram-tools
Edit /etc/default/zramswap:
PERCENT=50 # 50% of RAM → ~16 GB compressed swap
ALGO=zstd # fast and efficient
PRIORITY=100 # higher than disk swap
Enable and start the service:
sudo systemctl enable --now zramswap.service
Tune Swappiness
Lower swappiness so the kernel prefers RAM over swap:
sudo sysctl vm.swappiness=15
echo 'vm.swappiness=15' | sudo tee /etc/sysctl.d/99-swappiness.conf
Verify
Check that both swap devices are active:
sudo swapon --show
cat /proc/swaps
You should see both /dev/mapper/swap (encrypted disk swap) and /dev/zram0 (compressed RAM) listed as active swap devices.
If you have physical swap space zswap is superior to zram in almost any case. There are only few special cases where zram+regular swap is adviseable.
The kernels swappiness is a parameter used to indicate to the kernel how expensive swapping is compared to filepage eviction. So the kernel will not swap and instead throw out files that are cached. That includes the code for running programs. So this will not necessarily improve performance; there is also no correct answer to the question which value for the swappiness is correct.
However: 60 is the value the kernel team determined for average usecases on hard drives, as swapping works with smaller data chunks and thus file eviction is much “cheaper”. The values for swapping on SSDs should be higher overall, like close to 100. For zswap or zram which you use, the swapping takes place inside your RAM and is therefore much faster than any SSD, so swapping is cheaper by a factor of 10 (depends on the situation) and therefore the recommended values (of kernel developers, arch maintainers, fedora devs and pop-OS maintainers) are in the range of 133 to 200, mostly somewhere around 180.
TLDR: 15 might be not the optimal setting.
However, if your zram device is too small and swapping takes place mostly in the physical swap, you waste a ton of RAM for the zram part and only use regular slow swap in the end (LRU inversion). Then you shouldn’t use a high swappiness value – but you shouldn’t combine zram+swap in the first place, so….