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.
In turn, this leads to faster system performance in general. The compression and decompression time is generally negligible. So it feels as if your system suddenly has more memory to utilize (just a metaphor, not really).
zRAM 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
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:
If you see an output that contains “/dev/zram0” or similar then you have zRAM already enabled on your system.
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
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:
To save the file and exit, just press
Ctrl + X, hit
Y key, and press
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 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:
You can also see it in GNOME System Monitor application (it appears as a Swap but it is actually the zRAM device):
You can optionally customize the zRAM configuration options if you would like. All changes must happen on the
sudo nano /usr/bin/init-zram-swapping
/ 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):
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
echo lz4 > /sys/block/zram0/comp_algorithm
You can replace
lz4 with any algorithm you would like. It should look like the following picture:
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
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:
You can change the compression algorithm used in zRAM by appending the following line to the
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:
You may want to read this interesting blog post on further changes that you should consider applying on openSUSE.
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
vm.swappiness = 150
And then restart your system.
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.
Hanny is a computer science & engineering graduate with a master degree, and an open source software developer. He has created a lot of open source programs over the years, and maintains separate online platforms for promoting open source in his local communities.
Hanny is the founder of FOSS Post.
“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).”
Ummm… You should never attempt to use _all_ of your RAM as zram. As you mentioned in the article, zram doesn’t simply compress what goes into ram. It allocates a compressed swap area in ram (it preallocates a chunk of RAM which is exposed as a block device; then that block device gets formatted with mkswap). You still need a portion of RAM that’s available for use as working memory.
I noticed you said to turn off zswap. Both zswap and zram serve the same purpose. A comparison between the two is probably warranted, but I’d wager that most systems with zswap enabled will perform the same with zram.
If this is disabling traditional disk-based swap file usage, would not this be a potential significant positive contribution to SSD life expectancy by reducing the read/writes to the SSD? It seems like that’s something that would have been mentioned in the article.
also I am glad this article came out, as I’m surprised how zswap seemingly has to be installed with the linux kernel (depending on the OS, e.g. Azure Debian or PrawnOS) instead of running as a systemd service.
Running debian 10 on my desktop. apt doesn’t find the ‘zram-config’ package, only zram-tools and udisks2-zram .
yeah, me too. i installed zram-tools and was able to get to a 256MB allocation (which seems to be out of the box) but can’t figure out how to increase the size of the zram.
Ummmm… isn’t the whole idea of swap to be a place to put data from ram that you’re not currently using, to make room for some new stuff that needs to go in ram, and you use swap because you don’t have enough ram. So allocate ram to pretend that it’s swap to take up RAM space so now you have to use swap more often???
The key point is compression. If you have 16 GB of RAM and allocate just 4 GB of your RAM to zRAM and use a compression algorithm like zstd (which has a 3:1 ratio mainly), then you can place 12 GB of compressed data in this 4 GB of RAM space, leading to a total of 28 GB of effectively used RAM. There is a CPU time lost to compression and de-compression, but mostly it is negligible so it gives a better performance than using a Swap device on SSD or HDD devices, which are very much slower than RAM.