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.