If you’re finding your Linux system performance not quite up to par, enable zRAM for a more efficient swap system.

linuxadminhero.jpg

Image: Jack Wallen

If you’ve dealt with Linux long enough, you are fully aware of swap. If you’re relatively new to Linux, this is what you need to understand about a swap partition: The swap partition is a dedicated partition to be used when a system runs out of RAM. When this happens, inactive pages are moved out of RAM and into the swap partition.

The problem with swap is that it typically resides on drives that are slower than the RAM installed on a system. Of course, this is Linux, so there’s always a way around this. Said way is the zRAM module. What this does is dedicate a portion of RAM to serve as the swap space.

But isn’t the purpose of swap for when there’s insufficient RAM? That being the case, how does this chicken/egg scenario work?

Simple: Data stored in a zRAM partition is compressed, so more data can be stored in RAM. And although a small percentage of CPU time is used for this compression, the performance trade off is often worth it. 

If this sounds like a module you’d like to use, how do you make it so? I’m going to show you.

SEE: Flash storage: A guide for IT pros (TechRepublic Premium)

What you’ll need

As using zRAM is available on all Linux distributions, the only things you’ll need are:

With those bits in hand, let’s make this happen. I’m going to demonstrate on Ubuntu Server 18.04. If you’re using a different distribution, you’ll only need to modify the systemd unit file to fit your distro of choice.

How to enable zRAM

The zRAM module is controlled by systemd, so there’s no need for an fstab entry. And since everything is already installed out of the box, we only need to create a few files and modify one. 

Open a terminal window and create a new file with the command:

sudo nano /etc/modules-load.d/zram.conf

In that file, add the word:

zram

Save and close the file. 

Next, create a second new file with the command:

sudo nano /etc/modprobe.d/zram.conf

In that file, paste the line:

options zram num_devices=1

Save and close the file.

Next, we need to configure the size of the zRAM partition. Create a new file with the command:

sudo nano /etc/udev/rules.d/99-zram.rules

In that file, paste the following (modifying the disksize attribute to fit your needs):

KERNEL=="zram0", ATTR{disksize}="512M",TAG+="systemd"

Save and close the file.

How to disable traditional swap

In order for zRAM to function, you’re going to need to disable the traditional swap. This is handled within the fstab file. Open that file with the command:

sudo nano /etc/fstab

In that file, comment out (add a leading # character) the line starting with /swap.img.

Save and close the file.

How to create a systemd unit file

In order for zRAM to run, we need to create a systemd unit file. Create this file with the command:

sudo nano /etc/systemd/system/zram.service

In that file, paste the following contents:

[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 new unit with the command:

sudo systemctl enable zram

Reboot the machine.

How to find out if zRAM is working

Once the system reboots, log back in. From a terminal window, issue the command:

cat /proc/swaps

You should now see that /dev/zram0 is handling your swap (Figure A).

Figure A

zRAM is now handling the swap on this Ubuntu Server.

” data-credit rel=”noopener noreferrer nofollow”>zramtest.jpg

zramtest.jpg

zRAM is now handling the swap on this Ubuntu Server.

Congratulations, zRAM is now working. You should see a performance boost once applications and/or services start using of swap on the system.

Also see

Source