Enable zRAM on Rocky Linux to eke out as much speed as possible
The Linux kernel contains a module called zRAM that uses compressed RAM instead of the traditional partition for swap. And although zRAM does consume more CPU cycles, you'll find the increased speed (thanks to the faster RAM-based swap) worth it. Besides, with today's faster CPUs (with more and more cores), you'll barely notice the cycle hit.
You might be wondering: why even bother? Given how cheap RAM is these days, you've probably never run out of RAM on your Linux system, so swap remains untouched. But when you're running more intensive workloads, it is possible a system could bottom out in the RAM department and have to rely on swap. Given traditional swap is partitioned on your local drive, it's going to be slower than RAM.
If you're concerned that you might have to compile the Linux kernel to enable the zRAM module, fret not, as most Linux distributions ship with it ready to go. The only thing you have to do is enable it. You'll be surprised at how simple the process is and the benefits to be gained by it.
Let us show you how to make this work.
What you'll need
You'll need two things to make this work: A running instance of Rocky Linux and a user with sudo privileges. That's it. Let's get busy.
Enabling zRAM
The first thing we must do is enable zRAM. To do this, create a new file that will load the zRAM module. Issue the command:
echo zram | sudo tee -a /etc/modules-load.d/zram.conf
Create a new configuration file for the module with the command:
sudo nano /etc/modprobe.d/zram.conf
In that file, add the following:
options zram num_devices=1
The above line instructs zram how many devices should be pre-created.
Save and close the file.
Next, we need to create a UDEV file to configure the size of the zRAM partition. As for the amount, most agree that 50-100% of the installed RAM on a system is sufficient. So, if you have a system with 8GB of RAM, you could use 8GB for the zRAM partition. Of course, if you have a system that runs a more demanding workload (such as databases), you could use a larger value for the zRAM partition. Create this rule with the command:
sudo nano /etc/udev/rules.d/99-zram.rules
In that file, paste the following (changing the amount of zRAM according to your needs):
KERNEL=="zram0", ATTR{disksize}="8G",TAG+="systemd"
The above line first names the zram device (zram0), defines the size (8G), and ensures it is started by systemd.
Save and close the file.
The next step is to disable traditional swap. For that, open the fstab file with:
sudo nano /etc/fstab
If you see a swap entry in that file (such as /swap/swapfile swap swap defaults 0 0), comment it out by adding a # character at the beginning of the line.
Save and close the file.
Create a systemd unit file (which starts the zram service at boot and allows it to be controlled with the systemctl command) with:
sudo nano /etc/systemd/system/zram.service
In that file, add the following:
[Unit]
Description=Swap with zram
After=multi-user.target
[Service]
Type=oneshot
RemainAfterExit=true
ExecStartPre=/sbin/mkswap /dev/zram0
ExecStart=/sbin/swapon /dev/zram0
ExecStop=/sbin/swapoff /dev/zram0
[Install]
WantedBy=multi-user.target
Save and close the file.
Enable the zRAM unit with:
sudo systemctl enable zram
You can now reboot your machine with the sudo reboot command. After logging back in, you can verify zRAM is working with:
zramctl
You should see something like this in the output:
/dev/zram0 lzo-rle 8G 4K 74B 12K 2 [SWAP]
Do note, any time you change the amount of zRAM for the machine, you'll need to reboot.
And that's all there is to it. You should see a noticeable improvement in performance, when running resource-intensive tasks. Of course, if you don't see any improvement, you can always disable zRAM by reversing this process (making sure to enable swap in fstab).