¿What is zram and how to install it?

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

unknown

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)

6 Likes

Thanks for the great little write up! I recently did a fresh install of EndeavourOS Gnome to switch from Ext4 to Btrfs and while I was in the process of doing that, I was reading about zswap vs zram. I know Fedora uses it by default so I was curious about it, since I like the direction(s) that Fedora takes with (new) technologies. I have a modest Acer Aspire E5-576G laptop with 256GB SSD, 16GB Ram, hybrid Nvidia MX150/ Intel graphics so I was curious to see which would give me the most benefit.

In all honesty, I couldn’t really figure out if I’d actually be able to even notice a substantial difference in using either one on my system (outside of benchmark numbers), so I ended up just settling with zswap since it is the default already, so it didn’t require any additional intervention on my part during installation. I did have to create a Swap (no hibernation) of 8GB, which if I had chosen zram, I guess I could have saved that 8GB to still be used on my SSD, but perhaps I may need to have another look into the matter since I do like what Fedora sets as their defaults.

I believe this would be swappiness.

Controls the relative swap priority, a value between -1 and 32767. Higher numbers indicate higher priority.
If unset, 100 is used.
https://man.archlinux.org/man/zram-generator.conf.5

If I am not mistaken, this parameter is used when there are several swap devices present in the system to determine which is to be used first:

Swap pages are allocated from areas in priority order, highest priority first. For areas with different priorities, a higher-priority area is exhausted before using a lower-priority area. If two or more areas have the
same priority, and it is the highest priority available, pages are allocated on a round-robin basis between them.
source >> man swapon

1 Like

Please see this Guide.

Made by @flyingcakes.

All thanks to him.

3 Likes

There should be no significant differences in speed.
Both are allowed to use the same compression method.
And also the size of the zswap pool can be increased. Plus if the swappiness value is lowered.

The difference between zswap and zram is that zswap is able to evict pages to a disk based backing store, and zram is not. Gentoo wiki

If someone now starts to experiment with zram and is using zswap, it would be advisable to disable zswap. Since it is enabled by default.

Edit, disclaimer
Probably differences occur with less memory / without adjusting zswap values to match zram settings.

1 Like

How to disable zram now?
Doing

sudo systemctl stop dev/zram0 

timed out.

I had to uninstall the package and remove the systemd file.

Perhaps

sudo swapoff /dev/zram0

before

sudo systemctl stop /dev/zram0

?