Grimoire-
Command
.es

GNU+Linux command memo

ZRAM compressed RAM swap

Ajouter de la RAM virtuelle à sa machine, avec une mémoire d’échange compressée.

The linux kernel comes with a zram module which provides the zramctl command. It allows to setup a virtual memory storage which content is stored compressed. This allows to store more things in RAM, at the cost of a slower access for the compressed part.

This is a better solution for a previous problem evoqued here : No swap but no freeze.

$ sudo vi /etc/rc.local
modprobe zram (1)
zramctl /dev/zram0 --algorithm zstd --size "$(grep MemTotal /proc/meminfo | tr -dc '0-9')KiB" (2)
mkswap -U clear /dev/zram0 (3)
swapon --priority 100 /dev/zram0 (4)
1 This line allows to load the zram module in the running kernel
2 This invoques the zramctl command to create the virtual drive. It specifies wich compression algorithm to use, here it’s Z-standard (an algorithme known to be very fast on decompression), and the size of the virtual drive, here it’s the size of the existing physical RAM, but it can be more or less depending on specific needs).
3 This line format the virtual drive to allow using it as a swap drive.
4 This last line tells the system to actually use the zram drive as a swap one.

Then you can increase the swappiness of your system.

$ sudo vi /etc/sysctl.d/99-vm-zram-parameters.conf
# https://wiki.archlinux.org/title/Zram
vm.swappiness = 180
vm.watermark_boost_factor = 0
vm.watermark_scale_factor = 125
vm.page-cluster = 0