Let's fine tune your zRAM AKA Free RAM in openSUSE with Zstandard AKA zstd
Tags: linux, opensource, productivity, beginners
Published: 2022-09-05 03:26:45

> **UPDATE:** As of Tumbleweed's snapshot 20220914, **[my request](https://build.opensuse.org/request/show/1000767)** has been merged to the **[upstream repository](https://build.opensuse.org/package/show/openSUSE:Factory/systemd-zram-service)**. Therefore, people on Tumbleweed can get the ultimate zRAM with the normal `sudo zypper install systemd-zram-service && sudo zramswapon`. However, people on Leap will have to install the new zRAM package from the **[upstream repository](https://build.opensuse.org/package/show/openSUSE:Factory/systemd-zram-service)**.

> **UPDATE2:** After enabled zRAM, it's recommended to change `vm.page-cluster` to `0` and `vm.swappiness` to `180` to make the most out of your zRAM. You can permanently change both values by opening `/etc/sysctl.conf` file, then putting `vm.page-cluster=0` and `vm.swappiness=180` onto different lines.

Hi readers, the last time we met, I told you **[why zRAM should be enabled by default](https://dev.to/archerallstars/linux-opensuse-tumbleweed-review-in-2022-the-best-distro-for-both-newbies-and-pros-c6i#:~:text=zRAM%20AKA%20free%20RAM%2C%20and%20BTRFS%20transparent%20compression%20AKA%20free%20disk%20space%2C%20should%20be%20enabled%20by%20default)**. And fortunately, enable zRAM on openSUSE is extremely easy. All you need to do is open the terminal, and enter this command:

```
sudo zypper install systemd-zram-service && sudo zramswapon
```

Then, open **YaST Services Manager**, and **[make zramswap service to start on boot](https://dev.to/archerallstars/linux-opensuse-tumbleweed-review-in-2022-the-best-distro-for-both-newbies-and-pros-c6i#:~:text=You%20will%20have%20to%20make%20zramswap%20service%20start%20on%20boot%2C%20by%20opening%20YaST%20Services%20Manager%2C%20and%20set%20the%20start%20mode%20of%20zramswap%20service%20to%20On%20Boot%2C%20as%20shown%20in%20the%20screenshot%20below%3A)**. Now, you should have a zRAM enabled system.

Well, what's the problem with this setup?

---

## Too many zRAM devices are created

![Urban Building](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9l9phc5wlpkjpfj39k43.jpg)

After you enabled zRAM with the above command, if you check your zRAM status with `sudo zramctl`, you will notice that there are a lot of zRAM devices created, from `/dev/zram0`, `/dev/zram1`, `/dev/zram2`, and so on, depends on your CPU cores. This is because, in the past (before kernel 3.15), there's no **multi stream functionality** exist yet. Therefore, this was the only way to parallelize the task at that time.

However, **[this setup is deprecated](https://bugzilla.suse.com/show_bug.cgi?id=1185928)** and not considered a good practice anymore. Because, **[since kernel 4.7](https://github.com/Nefelim4ag/systemd-swap/issues/176#issuecomment-687765141)**, zRAM will always allocate multiple compression streams - one per online CPU - thus allowing several concurrent compression operations. Moreover, by having a lot of zRAM devices created and mounted at boot time can slow down the boot considerably, **[as reported here](https://github.com/Nefelim4ag/systemd-swap/issues/176)**.  Therefore, you should only have one zRAM device at a time, but have multiple compression streams instead. For instance, if you're using **[AMD Ryzen Threadripper 3990X](https://www.amd.com/en/products/cpu/amd-ryzen-threadripper-3990x)**, you should have 128 compression streams on your `/dev/zram0`, not 128 zRAM devices to bombard your `lsblk` 😂

If you check at **[systemd-zram-service's package repository](https://build.opensuse.org/package/show/filesystems/systemd-zram-service)**, you will see that the script corresponding to create zRAM devices was written over 10 years ago!

---

## The compression algorithm is outdated

![Old Computers](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w0cesx0axpvzh8l5apw0.jpg)

The default zRAM compression algorithm is `lzo-rle`, which has high compressed swap allocations, but has low compression ratio, thus prone to OOM. While `zstd`, which is an open source compression algorithm **[backed by Facebook](https://github.com/facebook/zstd)**, may has a lower compressed swap allocations rate, but it has a much higher compression ratio. Therefore, at the end of the day, not only `zstd` is the most efficient compression algorithm to use, but it will also finish the compression task faster than `lzo-rle` due to its compression ratio advantage. You can see more about zRAM's compression algorithm benchmarks **[here](https://www.reddit.com/r/Fedora/comments/mzun99/new_zram_tuning_benchmarks/)**.

I recommend everyone who uses zRAM to switch over to `zstd`.

---

## Let's go faster and better, fix the issues to get our hands on the ultimate zRAM! 

![The Man Playing Tennis](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8nxc8dqxo5341t4uuhyt.jpg)

You have to `nautilus admin:///` and search for `zramswapon` script to make our change. This is the current `systemd-zram-service`'s `zramswapon` script:

```
#!/bin/bash

# get the number of CPUs
num_cpus=$(grep -c processor /proc/cpuinfo)
# if something goes wrong, assume we have 1
[ "$num_cpus" != 0 ] || num_cpus=1

# set decremented number of CPUs
decr_num_cpus=$((num_cpus - 1))

# get the amount of memory in the machine
mem_total_kb=$(grep MemTotal /proc/meminfo | grep -E --only-matching '[[:digit:]]+')
mem_total=$((mem_total_kb * 1024))

# load dependency modules
#modprobe zram zram_num_devices=$num_cpus
# Determine module parm
mod_parm=$(modinfo zram |grep parm |tr -s " " |cut -f2 -d ":")
modprobe zram $mod_parm=$num_cpus

# initialize the devices
for i in $(seq 0 $decr_num_cpus); do
echo $((mem_total / num_cpus)) > /sys/block/zram$i/disksize
done

# Creating swap filesystems
for i in $(seq 0 $decr_num_cpus); do
mkswap /dev/zram$i
done

# Switch the swaps on
for i in $(seq 0 $decr_num_cpus); do
swapon -p 100 /dev/zram$i
done
```

It tries to get the number of CPU cores first before initializing zRAM devices. We don't want that since we only need one zRAM device. Therefore, here's my `zramswapon` script, nice and simple:

```
#!/bin/bash

# get the amount of memory in the machine
mem_total_kb=$(grep MemTotal /proc/meminfo | grep -E --only-matching '[[:digit:]]+')
mem_total=$((mem_total_kb * 1024))

# load the dependency module
modprobe zram

# initialize the device with zstd compression algorithm
echo $mem_total > /sys/block/zram0/disksize

# Creating the swap filesystem
mkswap /dev/zram0

# Switch the swaps on
swapon -p 100 /dev/zram0
```

Lastly, we want to use `zstd` as our compression algorithm. However, once the device is initialised, there is no way to change compression algorithm. See more about this **[here](https://docs.kernel.org/admin-guide/blockdev/zram.html#:~:text=once%20the%20device%20is%20initialised%20there%20is%20no%20way%20to%20change%20compression%20algorithm)**. Therefore, we will also have to choose the compression algorithm when we initializing zRAM device:

```
#!/bin/bash

# get the amount of memory in the machine
mem_total_kb=$(grep MemTotal /proc/meminfo | grep -E --only-matching '[[:digit:]]+')
mem_total=$((mem_total_kb * 1024))

# load the dependency module
modprobe zram

# initialize the device with zstd compression algorithm
echo zstd > /sys/block/zram0/comp_algorithm;
echo $mem_total > /sys/block/zram0/disksize

# Creating the swap filesystem
mkswap /dev/zram0

# Switch the swaps on
swapon -p 100 /dev/zram0
```

With our new `zramswapon` script, we will have a single zRAM device (`/dev/zram0`) equal to the size of our physical RAM with `zstd` compression algorithm. You will either have to reboot your PC to see this change since the script only run on boot time, or you can just call `sudo zramswapoff` and `sudo zramswapon` again to see the effect with `sudo zramctl`.

However, I recommend you to fork **[the official systemd-zram-service repository](https://build.opensuse.org/package/show/filesystems/systemd-zram-service)** on OBS (Open Build Service), so your script won't get overwritten when you update the package, especially, if you're on Tumbleweed that a new snapshot usually overwrites system files. You can see how to use OBS **[here](https://openbuildservice.org/help/manuals/obs-user-guide/art.obs.bg.html)**.

Well, you can also use **[systemd-zram-service package on my repository](https://build.opensuse.org/package/show/home:archerallstars/systemd-zram-service)** by:

```
sudo zypper remove systemd-zram-service && sudo zramswapoff
```

Then, go to my repository's **[download page](https://software.opensuse.org//download.html?project=home%3Aarcherallstars&package=systemd-zram-service)** > right-click at the download button corresponding to your OS > save link as... > double-click the file to install in YaST. And don't forget to **[enable zramswap service to start on boot](https://dev.to/archerallstars/linux-opensuse-tumbleweed-review-in-2022-the-best-distro-for-both-newbies-and-pros-c6i#:~:text=You%20will%20have%20to%20make%20zramswap%20service%20start%20on%20boot%2C%20by%20opening%20YaST%20Services%20Manager%2C%20and%20set%20the%20start%20mode%20of%20zramswap%20service%20to%20On%20Boot%2C%20as%20shown%20in%20the%20screenshot%20below%3A)**.

I have created **[a request](https://build.opensuse.org/request/show/1000767)** to make this change upstream, so you won't have to add my repository. Let's hope it will be accepted soon.

---

I think this's it for today. I hope this is a good read. If you have any comment, please let me know down below. See you later, bye 💨

---

**Cover** photo by **[Ralfs Blumbergs](https://unsplash.com/@rblumbergs?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)** on **[Unsplash](https://unsplash.com/s/photos/drift-car?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)**

**Urban Building** photo by **[Meriç Dağlı](https://unsplash.com/@meric?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)** on **[Unsplash](https://unsplash.com/s/photos/urban?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)**

**Old Computers** photo by **[Alex Motoc](https://unsplash.com/@alexmotoc?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)** on **[Unsplash](https://unsplash.com/s/photos/old-computer?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)**

**The Man Playing Tennis** photo by **[Moises Alex](https://unsplash.com/ja/@arnok?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)** on **[Unsplash](https://unsplash.com/@archerallstars/likes?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)**