zram Configuration Guide
zram creates a compressed swap space in RAM. It is a good replacement of swap partition or swap file.
Preparation
Check the existing swap spaces
cat /proc/swaps
We will want to disable the existing swap spaces.
Disable zswap
Check if zswap is enabled:
cat /sys/module/zswap/parameters/enabled
If N
is returned, then zswap is not enabled.
To disable zswap permanently on kernels where it is enabled by default, assuming that GRUB is the boot loader, edit /etc/default/grub
:
sudo nano /etc/default/grub
You will find the GRUB_CMDLINE_LINUX_DEFAULT
line, which may look similar to this:
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
Append zswap.enabled=0
to the options, save and exit, then re-generate the grub-cfg
file with:
sudo grub-mkconfig -o /boot/grub/grub.cfg
Disable swap partition
Edit /etc/fstab
and remove the entry similar to:
UUID=<device_UUID> none swap defaults 0 0
Disable swap file
Locate the swap file as described at the beginning of this section. Suppose that the swap file is located at /swapfile
, turn it off:
sudo swapoff /swapfile
Remove the swap file, be extra careful for the file name:
sudo rm -f /swapfile
Edit /etc/fstab
and remove the entry similar to:
/swapfile none swap sw 0 0
Disable swap file on Raspberry Pi OS
Turn off the swap file:
sudo dphys-swapfile swapoff
Remove the swap file:
sudo dphys-swapfile uninstall
Disable the relevant systemd service:
sudo systemctl disable dphys-swapfile.service
Reboot
Reboot after any changes.
Configuring zram
Using zram-tools
Install:
sudo apt install zram-tools
Configure:
sudo nano /etc/default/zramswap
With the following configuration:
# Use the zstd compression algorithm
ALGO=zstd
# Scale zram to full memory size
PERCENT=100
Save and exit, then restart the zram service:
sudo systemctl restart zramswap
Using zram-generator
Install:
# For Arch Linux
sudo pacman -S zram-generator
# For Ubuntu
sudo apt install systemd-zram-generator
Configure:
sudo nano /etc/systemd/zram-generator.conf
With the following configuration (ref: https://man.archlinux.org/man/zram-generator.conf.5):
[zram0]
# Scale zram to full memory size
zram-size = ram
# Use the zstd compression algorithm
compression-algorithm = zstd
Reload the systemd daemon
sudo systemctl daemon-reload
Start the zram service:
sudo systemctl start systemd-zram-setup@zram0.service
Note on choosing the compression algorithm
speed: lz4 > zstd > lzo
compression: zstd > lzo > lz4
Note on choosing the zram size
Ref: https://fedoraproject.org/wiki/Changes/Scale_ZRAM_to_full_memory_size
The size of zram is the maximum uncompressed data size within the zram, hence it can be set to be equal to or even greater than the system’s physical memory size, as long as the compressed size does not exceed the physical memory capacity.
My own rationale for scaling the zram to the full physical memory size is that it is both not too aggressive, as this should not overflow the memory since the compressed zram should be smaller than the physical memory, while also not too conservative, as more data can be stored in the zram than the default values. However, this needs to stand the test of time.
For a more conservative configuration, set PERCENT=50
for zram-tools
, or zram-size = min(ram / 2, 4096)
for zram-generator.
Tuning zram
Configure swap behavior
Ref: https://wiki.archlinux.org/title/Zram#Optimizing_swap_on_zram
Create the following file:
sudo nano /etc/sysctl.d/99-vm-zram-parameters.conf
With the following content:
vm.swappiness = 180
vm.watermark_boost_factor = 0
vm.watermark_scale_factor = 125
vm.page-cluster = 0
Reboot
Reboot after any changes.
References
https://wiki.archlinux.org/title/Zram
https://wiki.archlinux.org/title/Zswap#Toggling_zswap
https://wiki.archlinux.org/title/Swap
https://forums.raspberrypi.com/viewtopic.php?t=244130
https://github.com/garyexplains/examples/blob/master/how_to_enable_ZRAM_Raspberry_Pi.md