ZRAM Boosts Raspberry Pi Performance

Linux is a two-edged sword. On the one hand, there’s so much you can configure. On the other hand, there’s so much you can configure. It is sometimes hard to know just what you should do to get the best performance, especially on a small platform like the Raspberry Pi. [Hayden James] has a suggestion: enable ZRAM and tweak the kernel to match.

Although the post focuses on the Raspberry Pi 4, it applies to any Linux system that has limited memory including older Pi boards. The idea is to use a portion of main memory as a swap file. At first, that might seem like a waste since you could use that memory to, you know, actually run programs. However, the swap devices are compressed, so you get more swap space and transfers from these compressed swap devices and main memory are lightning-fast compared to a hard drive or solid state disk drive.

In addition to turning on the RAM-based swap, it is important to tune the kernel to make more use of swap now that it is relatively fast. The suggested settings change the following system parameters:

  • vm.vfs_cache_pressure – Increase the frequency of clearing file caches to free more memory.
  • vm.swappiness – Make the kernel more likely to use swap. Note that the ZRAM swap will have a higher priority than slower swap files.
  • vm.dirty_background_ratio – Allow a specified amount of memory pages to be dirty before writing to swap.
  • vm.dirty_ratio – Absolute limit on dirty pages before I/O starts blocking until the dirty pages are written out.

Of course, these parameters may or may not do the best thing for your setup, so you might want to experiment a bit. Look inside /proc/vmstat if you want to learn more about how many dirty pages you have, among other things.

That’s the nice thing about Linux. You can change things, see what’s happening, and change them again if you like. If you want more insight into your system’s operation with htop, you’ll want to read up on that program. This kind of technique is just the thing for those tiny Linux systems.

 

18 thoughts on “ZRAM Boosts Raspberry Pi Performance

  1. Thanks for sharing. As you mentioned this won’t help in every scenario. It give us all a chance to test and tweak on our own. For those of us who are indeed running low on memory and using SD card swap… it will most likely make a noticeable improvement. For example, this solved my Zabbix on Raspberry Pi swap i/o performance lockups. Happy hacking!

    1. I’m the author of the `zram-swap` script mentioned in the blog post. I wound up writing my own because I couldn’t find a replacement for `zram-config` that met all of my wants: simple design, graceful failure handling and some form of simple user-facing configuration.

      The `zram-config` Ubuntu package is pretty dated; It does work, but not quite as advertised. The scripting is brittle and the multiple devices thing is needlessly complicated. A lot of what the script does manually can be simplified away because the same functionality is now baked into zram itself or a function of the zramctl management tool.

      Some context: You haven’t needed to allocate individual zram devices per core since Linux 3.14 – the kernel automatically multithreads compression/decompression as of Linux 3.15 – so there’s absolutely no benefit to multiple devices. Manually allocating multiple devices just adds more places where the script can fail during setup or teardown. Speaking of failure if the script does fail for some reason it just breaks and leaves dangling half-configured zram devices that require manual cleanup. This doesn’t happen often, but it does happen.

      `zram-config` also makes a significant error calculating memory usage: where the package claims “allocates 50% of ram as zram swap” what it’s really doing is allocating a zram device whose uncompressed (virtual) size is equal to half of physical ram. This is then compressed and occupies ~1/3 to 1/2 of that size in real memory (ie: 1/6 to 1/4 of total installed RAM) when filled. This probably isn’t what most people expect or want after reading the documentation.

      If you like `zram-config` take my zram-swap script for a spin, it solves the same problem in a slightly more robust way and without the gripes mentioned above. The script is here: https://github.com/foundObjects/zram-swap — if you do take it for a spin make sure you stop and disable `zram-config` before switching, the two scripts won’t play nicely together.

      1. Could somebody explain the rationale behind this?

        Wouldn’t creating these ram-disks (as I understood this does) take away valuable RAM from the system? Swap’s point was to offload memory somewhere to free it for something else.

        1. Swap to disk says “if i wind up overcommitted on ram, give me 100x penalty but let me live” and you can live a long long time, if you give enough swap. Zswap says “here’s a lot less size, but the cost penalty is more like 2x” and it turns out for a lot of systems which only occasionally peak into Swap, and can tend to manage that, it’s better to incur a 2x penalty to a 100x penalty

          Numbers may be off, the principle holds. Yes, memory is valuable. So is speed.

  2. Similarly, back when the 386 came out, CPUs were getting so fast but most HDD’s were still in the PIO or early DMA era and thus dog slow. Installing Stacker/Drvspace could actually speed up disk I/O despite the heavy CPU implications, because it meant less total data had to trickle on and off the disk.

    1. Tl. Dr. No, you can’t execute compressed code and compression takes time.

      Depending on what you compress you get different compression ratios. Text can get close to 50% (of original size) while binary data/executables usually is less compressible. (you can test this by using zip since it is based on the same compression that Zram probably use)

      Since you can’t execute compressed code the maximum size of the program you can execute is ram minus operating system parts.
      Since ram was/is at a premium, your program can page out data to disk while it is not used and page it back in when it is needed. Databases is one way of doing this.

      Zram can make it possible for your pi to run more, not lager, programs than space in ram by compressing the ones not in use at the moment.

      Also this can lower the wear on the sdcard, but I am just guy guessing here.

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.