Drop swap for zram on Linux

Zram is a tool for creating an in-RAM compressed cache, specifically for use as swap space.
5 readers like this.
How Linux got to be Linux: Test driving 1993-2003 distros

Internet Archive Book Images. Modified by Opensource.com. CC BY-SA 4.0

I spend a lot of time playing (I mean working) on my computers, and I've found a lot of interesting things. One that has most recently come to my attention is the zram0 device. I first noticed it when working on one of my Opensource.com articles several months ago. It showed up in the output from the lsblk command:

# lsblk
NAME          MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sda             8:0    0 931.5G  0 disk
├─sda1          8:1    0   600M  0 part
[...]
zram0         252:0    0     8G  0 disk [SWAP]

It's identified as swap space, which is what first piqued my curiosity, so I did some exploration. Zram was originally called "compcache," which stands for "compressed cache." It turns out that zram is a tool for creating an in-RAM compressed cache, specifically for use as swap space.

But Why?

When I began researching zram, all I found were a couple of basic articles about using zram for swap space. At first, this seemed a bit counterintuitive to me. After all, if you're running out of RAM and you swap pages into a virtual drive in RAM, what's gained?

I then found the Fedora Project wiki page that proposed the use of Swap on zram. The proposal says: "Swap is useful, except when it's slow. zram is a RAM drive that uses compression. Create a swap-on-zram during start-up. And no longer use swap partitions by default."

The rest of the page is about details, benefits, side effects, and feedback.

Zram for swap space on Linux

Using zram for swap space is intended to do the same thing as regular partition-based or file-based swap space. When memory pressure becomes too great, some of the least recently used data is moved to swap space. On average, it's compressed to about 50% of its original size, and placed in zram space in RAM. This is much faster than storing those memory pages on a hard drive and frees up the RAM it was using for other use.

Saving on swap

I tried to find revised recommendations for how much swap or zram swap to configure. This led me back to a reassessment of swap, and my previous article, What's the right amount of swap space for a modern Linux system? As far as I can tell from the most current documentation for RHEL and Fedora, the recommended amount of swap space has not changed. That documentation, however, ignores the use of zram.

However, the tables in that previous article still provide a good starting point for swap space allocation when using older releases of Linux that don't use zram or in cases where zram has been disabled.

The documents I found for the Zram feature are inconsistent in terms of how zram is allocated with respect to RAM size, and the amount of space allocated to zram swap.

Due to the lack of authoritative documentation, I performed some experiments to empirically determine the algorithm used to allocate zram swap. I used my own physical and virtual systems for this. The results are interesting and do not match any documentation I've so far found.

The default size of zram is 8 GB on all systems large enough to support that, but it's typically reduced significantly on hosts with small amounts of RAM. On one virtual machine (VM) I use for testing, with access to 4 GB of RAM, the zram virtual swap space is allocated to 3.8 GB. One old Dell I have contains 8 GB of RAM, and the zram is set to 7.6 GB. When RAM is reduced to 2 GB, Zram is reduced to 1.9 GB.

All physical and virtual hosts I have with more than 8 GB of RAM show exactly 8 GB of zram. This includes my primary workstation with 64 GB of RAM and other hosts with 16 GB or 32 GB of RAM.

Based on these few data points, I can draw the conclusion that the current default settings are for 8 GB of zram at most, and for zram to be 95% of RAM on hosts with 8 GB or less.

I have read a number of articles that mention other sizes for zram swap, even up to 100% of RAM, but those all seem to be theoretical rather than reality.

Your distribution may be different, but here are the actual zram swap allocations for Fedora and similar distributions:

  • RAM ⇐ 8 GB: 0.95 × RAM

  • RAM > 8 GB: 8 GB

Be aware that the zram swap size algorithm is not based on any recommendations for the "best" swap size for any given real-world system or application. This zram swap allocation is a rather probabilistic approach to what should work well on a wide range of Linux hosts. However, the fact that the maximum zram swap size is configured for 8 GB and the fact that I have always recommended 8 GB as the maximum amount of traditional swap, I think I can say it's reflective of the optimum sizes for zram swap.

Managing zram swap

Zram defaults are stored in the /usr/lib/systemd/zram-generator.conf configuration file. The following is from one of my test VMs with 5097 GB of RAM allocated.

# cat /usr/lib/systemd/zram-generator.conf
# This config file enables a /dev/zram0 device with the default settings:
# - size - same as available RAM or 8GB, whichever is less
# - compression - most likely lzo-rle
#
# To disable, uninstall zram-generator-defaults or create empty
# /etc/systemd/zram-generator.conf file.
[zram0]
zram-size = min(ram, 8192)

You can change the default Zram swap size in the last line of the zram-generator.conf configuration file. I recommend against doing that, unless you can definitively show a reason for doing so, and test your results once you make any changes. Like many other configuration defaults in Linux, the zram ones have been well-tested and are appropriate for most use cases.

Monitor zram

The zramctl utility can be used to view the current state of zram.

# zramctl
NAME       ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram0 lzo-rle       4.8G   4K   80B   12K       4 [SWAP]

The traditional swapon command can also be used to view swap including zram used as swap:

# swapon --show
NAME       TYPE      SIZE USED PRIO
/dev/zram0 partition 4.8G   0B  100

One thing to be aware of is that zramctl does not report on zram when it contains no data, so the results contain null output. Tools like lsblk, swapon, top, free, htop, and so on, do show zram even when it contains no data.

Deactivate zram

The swapoff -a command turns off zram swap as well as traditional HDD or SSD storage used as swap. The swapon -a command does not show zram when it is empty. Use zramctl /dev/zram0 instead.

# swapon --show
# lsblk
NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sda             8:0    0  120G  0 disk
├─sda1          8:1    0    1G  0 part /boot/efi
├─sda2          8:2    0    1G  0 part /boot
└─sda3          8:3    0  118G  0 part
  ├─vg01-root 253:0    0   10G  0 lvm  /
  ├─vg01-swap 253:1    0    3G  0 lvm  [SWAP]
  ├─vg01-usr  253:1    0   30G  0 lvm  /usr
  ├─vg01-home 253:2    0   10G  0 lvm  /home
  ├─vg01-var  253:3    0   30G  0 lvm  /var
  └─vg01-tmp  253:4    0   10G  0 lvm  /tmp
sr0            11:0    1 1024M  0 rom
zram0         252:0    0    0B  0 disk
# zramctl
#
# zramctl /dev/zram0
NAME       ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram0 lzo-rle         0B   0B    0B    0B       4

Note that /dev/zram0 doesn't show up in these commands as swap space until it's being used for that purpose. This caused me some confusion until my experiments showed it to be the case.

Creating Zram Swap

Zram itself has been around for about 20 years, but has only been in use as swap space on some distributions for the last year or two. The current Linux installation on some or all of your hosts may not have been created with zram for swap. If that's the case, it can be easily remedied.

For Fedora 32, the last release prior to the default use of zram for swap, it only takes three easy commands.

First, verify the presence of the zram-swap.service file, installed as part of the zram RPM package.

# systemctl status zram-swap
● zram-swap.service - Enable compressed swap in memory using zram
     Loaded: loaded (/usr/lib/systemd/system/zram-swap.service; disabled; vendor preset: disabled)
     Active: inactive (dead)

Next, install the zram-generator-defaults and zram-generator packages.

# dnf install zram-generator-defaults zram-generator

Enable and start the zram-swap service:

# systemctl enable zram-swap.service
# systemctl start zram-swap.service

And then verify that zram0 exists, and is being used as swap space:

# lsblk
NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda             8:0    0  120G  0 disk
├─sda1          8:1    0    2G  0 part /boot
└─sda2          8:2    0  118G  0 part
  ├─vg01-root 253:0    0   10G  0 lvm  /
  ├─vg01-swap 253:1    0    3G  0 lvm  [SWAP]
  ├─vg01-usr  253:2    0   35G  0 lvm  /usr
  ├─vg01-tmp  253:3    0   15G  0 lvm  /tmp
  ├─vg01-var  253:4    0   35G  0 lvm  /var
  └─vg01-home 253:5    0   20G  0 lvm  /home
sr0            11:0    1 1024M  0 rom
zram0         252:0    0  7.5G  0 disk [SWAP]

Improve swap with zram

That's all there is to it. It was easy with Fedora. Different distributions will likely be just as easy, with some possible different details in the package names and commands. Give zram swap a try on your computer. In my next article, I'll demonstrate some further zram options.

David Both
David Both is an Open Source Software and GNU/Linux advocate, trainer, writer, and speaker. He has been working with Linux and Open Source Software since 1996 and with computers since 1969. He is a strong proponent of and evangelist for the "Linux Philosophy for System Administrators."

Comments are closed.

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.