How to resize ZRAM (compressed swap) managed by systemd/zram-generator

Fedora Linux 33 was released last week with some interesting changes like switching the default file system to BtrFS, handing DNS resolution over to systemd-resolved, and enabling ZRAM instead of a swap partition by default. I’ll focus on the latter feature in this article.

ZRAM is a Linux kernel subsystem for handling compression of volatile memory (RAM). Compression sacrifices some processor time in exchange for more space-efficient utilization of your available RAM. ZRAM acts like a traditional storage drive backed swap partition, except that it lives entirely in your system RAM. The exact compression ratio you can expect depends on what you’re keeping in memory.

Fedora Linux uses systemd/zram-generator to manage the creation of the virtual ZRAM device during system boot. This is handled through a systemd service called systemd-zram-setup@zram0.service. This service is enabled by default in new installations and on upgrade from earlier versions of Fedora Linux.

If you have a swap partition and ZRAM enabled, the system will prefer swapping out memory to the ZRAM device before falling back to the slower swap partition. Note that you can’t hibernate into something that only exists in memory. Hibernation still isn’t officially supported by Fedora Linux so this wasn’t a concern for their decision to switch to ZRAM by default. However, I’ve updated my tutorial on enabling hibernation in Fedora Linux with more details.

The default configuration of the systemd/zram-generator will create a ZRAM device of 50 % of the physical volatile memory up to 8 GB (whichever is smaller). My typical workload involves a lot of text processing and data that compresses well, so I wanted to increase the size of the ZRAM device. My primary computer has 32 GB of physical volatile memory and 16 processor threads, so it should have plenty of backing resources to spare. I also wanted to increase the ZRAM device size on my servers and virtual private servers (VPS).

To increase the size of your ZRAM device, create the file /etc/systemd/zram-generator.conf and include the below configuration example. (You can also leave an empty file in this path to disable ZRAM.)

[zram0]
zram-fraction=0.5
max-zram-size=4096

The limits given above matches the default limits (50 % of available memory or 8 GB in MB). Change the limits to your preference. Keep in mind that the system can become unstable if you set it too high. I suggest staying with the default factor of 0.5 and increasing the max size to match your available system memory. For example, set max-zram-size=8192 if you have 16 GB of RAM.

You can experiment with different settings to discover what works best for you. Check out the zram-generator.conf manual page for more configuration options like how to change the default compression algorithm.

I followed my own advice on my primary desktop computer. However, I set the limits fairly high on my low-memory budget VPSes. These servers run under constant high-memory pressure, but I was able to effectively double their available memory with ZRAM. Constant memory constraints is the hallmark of budget VPSes. I’m a bit annoyed with myself for not exploring compressed RAM earlier.

You can recreate the ZRAM device immediately without a reboot if you have enough physical memory to hold an uncompressed copy of all your currently compressed memory. The commands free and zramctl can help you determine the current memory pressure and compression ratio. Run the command systemctl restart systemd-zram-setup@zram0.service to recreate the ZRAM device without rebooting. However, the safest and quickest option is probably to save your work and reboot.

The configuration file at /etc/zram.conf is a legacy file that is no longer used.

Update (): Fedora Linux 34 changed the default maximum zram size from 4 GB to 8 GB, and from 25 % to 50 % of physical RAM. The swap-create@zram0.service was also renamed to systemd-zram-setup@zram0.service.