zRAM is a kernel module which creates swapspace in compressed RAM. The speed benefits of having swapspace in RAM far outweigh the minor cost of compression in CPU cycles.
zRAM is controlled by systemd, there’s no need for an fstab entry. It’s also installed by default on RHEL 7 and 8 and their clones. All you need to do is enable it. There are several steps to this process.
First, create a new file with the command:
sudo nano /etc/modules-load.d/zram.conf
In that file, add the word:
zram
Write out and close the file.
Next, create a new file with the command:
sudo nano /etc/modprobe.d/zram.conf
In that file, paste the line:
options zram num_devices=1
Write out and close the file.
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}="2G",TAG+="systemd"
Write out and close the file.
The existing swap file or partition must be disabled for zRAM to function. Edit /etc/fstab with the command:
sudo nano /etc/fstab
In that file, comment out the line for the swap partition or image by adding a leading # character.
Write out and close the file.
To run zRAM at startup, create a systemd unit file with the command:
sudo nano /etc/systemd/system/zram.service
Paste the following contents into the file:
[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
Write out and close the file.
Enable the new unit with the command:
sudo systemctl enable zram
Reboot.
To see if zRAM is working issue the command:
cat /proc/swaps
You can see details on utilization and compression using the command:
zramctl
That’s it! Have a beer, and consider reclaiming the disk space utilized by your old swap file or partition.