Optimize zram setup for high RAM systems

Article Summary:

By default Fedora uses zram that is whichever of these is smaller: the system RAM or 8GB.

This should work fine, but we probably want to use a bigger zRAM on systems with a lot of RAM, likely always the same as system RAM, while at it we can take a look into compression (the default seems to be lzo-rle, but zstd is an option) and swappiness.

The use cases most likely to benefit from this are gaming, some types of development (e.g. Java ecosystem being memory hungry), web browsing (if we are pessimistic about browser memory usage) and virtualization.

Article Description:

By default Fedora ships a /usr/lib/systemd/zram-generator.conf with the folowing contents:

$ 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)

Resulting in:

$ zramctl 
NAME       ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram0 lzo-rle         8G  <snip>  <snip>   <snip>         [SWAP]

From the .conf file man page:

•   zram-size=

   Sets the size of the zram device as a function of MemTotal, available as the ram variable. Additional variables may be provided by DIRECTIVES.

   Arithmetic operators (^%/\*-+), e, π, SI suffixes, log(), int(), ceil(), floor(), round(), abs(), min(), max(), and trigonometric functions are supported.

   Defaults to min(ram / 2, 4096).

We effectively can chose either max(ram / 2, 8192) or max(ram, 8192) depending on how much the user trusts zRAM won’t eat their normal RAM.

There’s research on compression for zram, but we can analyze the available research and choose whether to recommend zstd by default or whether some other options make sense.

Apparently there’s the swappiness recommendations that PopOS uses, we can look into those as well.

Note: We can potentially later create a Docs page with more information, Fedora Magazine just will bring more visibility for this. Generally every single time I had to figure out how to optimize zram I rely on the Arch wiki.

I have read and understand the Ai-Assisted Contributions Policy


For Editor Use Only

Editor:

Image Editor:

Publication Date:

Preview Link:

Sounds good to me. +1. :slightly_smiling_face:

Let us know when you have the article ready for review in WP and well get to work on it.
If you have ideas for a featured image suggestions always appreciated.

We assume this should go out as soon as it is ready.

Sorry for the delay, I should probably give some update on this.

So far I ended up with this zram config:

$ cat /etc/systemd/zram-generator.conf 
[zram0]
zram-size = max(8192, ram * 2)
compression-algorithm = zstd

And these two sysctl files:

$ cat /etc/sysctl.d/99-vm-zram-parameters.conf 
vm.swappiness = 180
vm.watermark_boost_factor = 0
vm.watermark_scale_factor = 125
vm.page-cluster = 0
 cat /etc/sysctl.d/99-vm-vfs-tuning.conf 
vm.vfs_cache_pressure = 75
vm.dirty_background_ratio = 5
vm.dirty_ratio = 20

My research mostly consisted of reading the zram page on the Arch wiki and the zram-tuning repo.

My use cases on this system so far mostly revolved around Android Studio usage.

So to sum up what I have so far:

  • We can use zstd compression and it’s probably the best compression to use
  • Apparently it’s possible to set the compression level, but I couldn’t get it to work
  • Apparently it’s possible to have two types of compression at once, the one which is for the stuff on RAM being currently used (which you should use a faster compression) and the stuff that hasn’t been used in a while (which should be something that crompresses more even if slower with a goal of saving RAM), but I couldn’t get it to work
  • Some of the sysctl comes from zram-tuning/docs/recommended-profile.md at main · reapercanuk39/zram-tuning · GitHub and some from https://wiki.archlinux.org/title/Zram#Optimizing_swap_on_zram
  • Double the ram seems like the best choice so far, on my 32 GB system that gives me 64 GB zram
  • I’m setting the config to use at least 8 GB as a fallback which shoudl be comptaible with the default setting