zRAM is a Linux kernel module that allows the creation of Swap devices on memory. But the catch is that it does not just create them there; it also compresses them so that the system can store more data in memory, which allows accessing this data much faster than a traditional swap on SSD or HDD devices.

Compression allows storing more data in memory. So instead of having 4 GB of RAM only for example, with zRAM you could utilize around 8-12 GB of effective system memory.

In turn, this leads to faster system performance in general. The compression and decompression time is generally negligible for any relatively modern CPU (younger than 7-8 years old). So it feels as if your system suddenly has more memory to utilize.

This is very much different from traditional Swap devices on disk. Disk-based devices like Swap are much slower than RAM (regardless if it was HDD or SSD or NVMe M2), so even if you use a 10GB or 50GB swap device on your system, it would still be equally slow. Because you are not foxing the low-memory issue, you are just sending it to somewhere else.

zRAM on the other hand is especially helpful on systems with low memory such as 2GB, 4GB or 8GB of RAM. Such systems will find out that they can breathe more by using a compressed zRAM Swap device.

You should know that there are many data compression algorithms available for zRAM such as zstd, zlib, brotli, lz4 and others, and each one has its different performance points (some are good in writing data, some are good at decompressing data… etc). We will see how to use these algorithms later on.

Enabling zRAM on Linux

In this article, we will see how we can enable zRAM on various Linux distributions.

Check if zRAM is Already Enabled

Many Linux distributions already have zRAM enabled, so you wouldn’t want to do the effort again and try to enable it.

To check whether zRAM is enabled or not on your system, run the following command:

cat /proc/swaps

If you see an output that contains “/dev/zram0” or similar then you have zRAM already enabled on your system.

Pasted image 0

Otherwise, keep reading to learn how you can enable it.

Turn Off your Swap Disk/File

Users are advised to create a Swap disk or Swap file when they first set up their Linux installation in order to support it when memory usage becomes high. However, Swap on SSD or HDD disks is quite slow compared to RAM.

We don’t want our system to use the traditional Swap disk or Swap file, instead, we want it to use the zRAM Swap device we will create later on, so we have to turn off the former in order to avoid conflict.

First, locate your Swap in your /etc/fstab file:

sudo nano /etc/fstab

And then, comment the line containing the swap device by inserting a # symbol at the beginning of the line like the following:

Pasted image 0

To save the file and exit, just press Ctrl + X, hit Y key, and press Enter.

Now, also turn off your currently-running Swap file by writing:

sudo swapoff /swapfile

Note: If you are using a mountpoint other than /swapfile for your Swap, then just replace it in the previous command.

Enabling zRAM on Debian/Ubuntu/Linux Mint

You can easily enable zRAM Swap device disk by installing the zram-config package. It is available in all Debian-based distributions.

To install it, run the following command from the terminal:

sudo apt install zram-config

And enable the systemd service responsible for zRAM:

systemctl enable zram-config
systemctl start zram-config

Then reboot your system.

This will create a zRAM Swap device in half the size of your real RAM. So for example, if you have 8GB of RAM, your zRAM Swap device size will be 4GB.

 You should now see that you have zRAM enabled:

Pasted image 0

You can also see it in GNOME System Monitor application (it appears as a Swap but it is actually the zRAM device):

Pasted image 0

You can optionally customize the zRAM configuration options if you would like. All changes must happen on the /usr/bin/init-zram-swapping file:

sudo nano /usr/bin/init-zram-swapping
Pasted image 0

Remove / 2 if you would like to use all your memory as a zRAM Swap device (Not recommended unless you have only 8GB or less of RAM).

You can also change the default compression algorithm for zRAM that is being used.

You can find out the zRAM compression algorithms that your system currently supports with the following command, as well as the default one (which will be included in brackets):

cat /sys/block/zram0/comp_algorithm

The default algorithm used on Ubuntu and zram-config tool is zstd, but you can change it to another one if you like by adding the following command under the line starting with mem=$:

echo lz4 > /sys/block/zram0/comp_algorithm

You can replace lz4 with any algorithm you would like. It should look like the following picture:

Pasted image 0

Save the file and exit, and then reboot your system.

Enabling zRAM on Fedora

zRAM Swap device is enabled by default on Fedora.

This is done using the zram-generator tool, which is installed by default on Fedora. You can view its default settings and tweak them according to your needs from the following file:

sudo nano /usr/lib/systemd/zram-generator.conf
Pasted image 0

The file is mostly empty as you can see. The only default set is related to size: Either 8GB or your memory size if it was equal to less than that.

Just like we explained previously, you can learn the available compression algorithms on your system with the following command, and the default one will be contained inside brackets:

cat /sys/block/zram0/comp_algorithm

You can change the compression algorithm used in zRAM by appending the following line to the /usr/lib/systemd/zram-generator.conf file:

compression-algorithm = lz4

Save it, exit and reboot.

Enabling zRAM on openSUSE

On openSUSE, you need the systemd-zram-service package installed to enable zRAM Swap device. You can install it with the following command:

sudo zypper in systemd-zram-service

And enable it with the following command:

sudo zramswapon

You may want to read this interesting blog post on further changes that you should consider applying on openSUSE.

That’s it.

zRAM on Pop!_OS

zRAM is already enabled by default on Pop!_OS as well.

The team behind the distribution actually made the effort of adjusting the size, algorithm and swapiness settings so that they are optimal by default. There isn’t anything for you to do.

This is done using the pop-default-settings-zram package, which has its default settings stored in the following file:

sudo nano /etc/default/pop-zram

You can edit that file if you wish to change anything.

zRAM Swap Device on Other Linux Distributions

Gentoo die-hard users can refer to the Gentoo wiki on how to enable zRAM. Gentoo does not use systemd by default, which is why further adjustment is necessary to enable zRAM.

Gentoo-wannabes Arch Linux users can refer to their own wiki as well on how to enable it.

For other distributions, you may want to check the zram-generator utility. It is part of the systemd ecosystem, so it should work flawlessly on any distribution that uses systemd as well.

Changing Swapiness Level on Linux

Since the data in zRAM is going to be compressed, then we want to be sure that our newly-created zRAM Swap device is going to be filled before the empty area in RAM.

Swapiness is a system setting on Linux distributions that tells the system when to start filling the Swap device (in our case, the zRAM Swap device). A swapiness of 0 means that it will start filling only when memory usage is around %100, while a swapiness of 100 means it will instantly be filled.

We actually desire more than that; we want data to be filled always on the zRAM disk before any data is written to the uncompressed RAM area. So, we should use a swapiness value of 150 (it doesn’t mean 150% of RAM, it means encouraging the kernel to swap more often).

To do that (on any Linux distribution), append the following line to the /etc/sysctl.conf file:

vm.swappiness = 150

And then restart your system.

Conclusion

We have seen so far how to enable zRAM Swap devices on various Linux distributions, as well as some settings that we can adjust to get better performance.

zRAM is an amazing technology. Many owners of old computers report that it helped them achieve better system performance and stability once it gets enabled, so you should definitely give it a try if you haven’t enabled it already.

If you have a large amount of RAM on your system (32 GB or more), then perhaps you don’t need to use zRAM, and it may affect your performance negatively (because of the compression and decompression time), but other than that, it should provide you with better performance because swapping (which your system will do often on smaller amounts of RAM) to RAM is faster than swapping to disk.

Subscribe
Notify of
guest

13 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Newsletter

Enter your email address to subscribe to our newsletter. We only send you an email when we have a couple of new posts or some important updates to share.

Recent Comments

Open Source Directory

Join the Force!

For the price of one cup of coffee per month:

  • Support the FOSS Post to produce more content.
  • Get a special account on our website.
  • Remove all the ads you are seeing (including this one!).
  • Get an OPML file containing +70 RSS feeds for various FOSS-related websites and blogs, so that you can import it into your favorite RSS reader and stay updated about the FOSS world!