The zram kernel module (previously called compcache ) provides a compressed block device in RAM. If you use it as swap device, the RAM can hold much more information but uses more CPU. Still, it is much quicker than swapping to a hard drive. If a system often falls back to swap, this could improve responsiveness. Using zram is also a good way to reduce disk read/write cycles due to swap on SSDs.
From my personal experience, it is one of the best things I have discovered using linux, the difference between zram and conventional swap is gigantic, you also have multiple options (algorithms), here I will publish the table
For people with little amount of ram, I recommend zstd, this is the one that compresses the most.
Pros:
It is much faster than the conventional swap, Although it uses cpu and therefore has infinitely higher performance than swap, cpu usage is minimal (depends on the algorithm), It is easy to install, configure and remove,
Cons:
It only comes by default in two distros (fedora, opensuse), You can’t use hibernate, in very old cpu’s it get bad experiencel (depends on the algorithm),It can be a bit complicated to understand how it works at first
Note: It is recommended to disable swap if you already have it installed on your system and to remove it from the entries when you turn it on, although zram can work with swap if you configure the priorities correctly, it is not recommended at all.
Ok now let’s go with the installation:
sudo pacman -Syu zram-generator
sudo nano /usr/lib/systemd/zram-generator.conf
And paste this in the conf file
[zram0]
zram-fraction = 1.0
max-zram-size= 8192
compression-algorithm=zstd
swap-priority=60
sudo systemctl daemon-reload
sudo systemctl start /dev/zram0
Check htop and it should be there
Explanation of the parameters:
zram-fraction = If the value is 1.0 it means that the zram will be equal to the amount of physical ram of the pc, example. If the pc has 3.72gb of ram, the zram will be exactly that value.
max-zram-size = Just as its name says, this parameter limits the maximum amount of zram on the system, as recommended 8gb should be the maximum.
compression-algorithm = In this parameter you choose the algorithm that is responsible for compressing and decompressing
swap-priority = This parameter is the value at which zram will start to work, in 99% of Linux distros the default value is 60.
Important: It is not recommended to use more zram than the available ram (for obvious reasons)