Zram is a compressed RAM disk on Linux. Lately, it's been put to use for swap space on many distributions. In my previous article, I introduced zram and demonstrated how to use it. In this article, I cover some of the ways you can customize how your system puts zram to use.
Augmented swap
Zram swap can be augmented with standard secondary storage devices. Adding some traditional swap space can be especially useful on systems with low amounts of RAM. (Such augmentation isn't normally useful on hosts with very large amounts of RAM.)
If you do choose to augment swap with some type of storage device, hard drives do still work but are significantly slower than using SSD in either SATA or m.2 formats. However, the flash memory in SSD devices has a more limited life than HDD devices, so systems with large amounts of swap activity can significantly reduce the lifespan of an SSD.
Tuning swap
There's more to tuning swap space than simply allocating a specific amount of swap space. There are other factors that can be used to manage how swap space is used and managed by the system. Swappiness is the primary kernel parameter that can be used to manage swap performance.
I recently wrote How I troubleshoot swappiness and startup time on Linux, in which I discussed the vm.swappiness
kernel setting. The short version is that the default setting for how aggressively the Linux kernel forces swapping to begin and to function is 60. Zero (0) is the least aggressive and 100 (or 200, depending on what you read) is the most aggressive. At that level, I was experiencing delays when working with very large documents in LibreOffice despite having 64 GB of RAM in my primary workstation, much of which was unused.
The resolution to this problem is to reduce the vm.swappiness
kernel parameter to 13. This works well for my use cases, but you may need to experiment to get it right for your environment. Read the linked article for the details.
You can also find more general information about tuning the Linux kernel in my article How to tune the Linux kernel with the /proc filesystem.
Swap size recommendations
At this time, I have found no recommendations from any distribution for swap size when using zram. Based on my personal experience, I have found that having at least a little swap space can be beneficial. Even when you have large amounts of RAM, just the fact that properly tuned swap for your environment is being used and contains data can indicate that more RAM is needed.
The default zram swap size, listed in my previous article, is more than sufficient for that purpose. It has worked well so far on all of my Linux hosts.
In my opinion, the ultimate purpose of swap space is to be a small buffer — a red flag – that lets the system administrator know when more RAM is needed in a system. Of course, some very old hardware cannot support more than 4 or 8 GB of RAM. In such a case, a new motherboard is needed – one which will support enough RAM to perform the task at hand.
My recommendation is to do as I have. I set up zram swap of the default size on every one of my hosts. I removed all existing swap partitions (I don't use swap files), and my hosts have all been running perfectly with that swap setup.
Removing traditional swap partitions and files
Because I've just recommended removing all of the old swap partitions, I should also mention that the process to do so is not as straightforward as it should be. It's not hard, but it took me some research to figure it out because there's a lot of old and incorrect information out there on the Internet. This procedure works for me on Fedora 36.
-
Turn off swap for the existing swap partitions and files.
This can be done using
swapoff /dev/nameofswapdevice
but it might be easiest to just turn off all swap with theswapoff -a
command. This command also turns off any existing zram swap. -
Remove the entries for traditional swap partitions or files in the
/etc/fstab
file. I just commented these out, in case of unexpected problems. I deleted those entries later. Zram swap does not require an entry in the/etc/fstab
file. -
The default
/etc/default/grub
configuration file is simple, and you only need concern yourself with one line:GRUB_TIMEOUT=5 GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)" GRUB_DEFAULT=saved GRUB_DISABLE_SUBMENU=true GRUB_TERMINAL_OUTPUT="console" GRUB_CMDLINE_LINUX="resume=/dev/mapper/vg01-swap \ rd.lvm.lv=vg01/root rd.lvm.lv=vg01/swap \ rd.lvm.lv=vg01/usr rhgb quiet" GRUB_DISABLE_RECOVERY="true" GRUB_ENABLE_BLSCFG=true
Change the
GRUB_CMDLINE_LINUX
line to:GRUB_CMDLINE_LINUX="rd.lvm.lv=vg01/root rd.lvm.lv=vg01/usr"
Removing
resume=/dev/mapper/vg01-swap
andrd.lvm.lv=vg01/swap
prevents the kernel from looking for the swap volume. -
You must save these changes with
grub2-mkconfig
. First, make a backup of the current/boot/grub2/grub.cfg
file, and then run the following command:
# grub2-mkconfig > /boot/grub2/grub.cfg
Troubleshooting zram
It's important to follow all of these steps. Removing rhgb quiet
causes all kernel boot messages and systemd startup messages to be displayed. This can make it easier to quickly locate problems during the boot and startup phases.
When I first tried this, I removed the logical volume I'd designated as swap space, and then rebooted to test. The reboot failed and hung early in the boot process.
Fortunately, I've set the kernel so that it displays boot and startup messages rather than the graphical boot. As a result, I was able to see the error message indicating that the kernel couldn't find the swap volume.
If this happens to you because you've missed a step, then boot from a Live Fedora USB drive, and create a new swap volume. It's not necessary to do anything else. Then reboot and remove the swap entries in kernel line of /etc/defaults/grub
.
Using only zram swap
After removing swap partitions and regenerating the GRUB config, I ran swapon -a
and verified with swapon -show
and lsblk
. Rebooting the system gave me a final assurance that the system did boot properly and that the only swap is the zram swap.
Zram is a tool that's meant for creating compressed virtual swap space. The ideal swap configuration depends on your use case and the amount of physical RAM in your host computer. No matter what combination of zram, swap partitions, and swap files you use for swap, you should always experiment with your own system loads, and verify that your swap configuration works for you. However, using the default zram swap without any traditional swap partitions or files works as well for me — as well as any other swap configuration I've ever used, and better than many.
Comments are closed.