Raspberry Pi Performance: Adding ZRAM and Changing Kernel Parameters

A couple of weeks ago I posted Pinebook Pro review. Since the Raspberry Pi 4 is also based on ARM, some of the optimizations mentioned in the previous article are quite suitable for it. I would like to share these tricks and see if you experience the same performance improvements.

After installing the Raspberry Pi in your home server room I noticed that in moments of lack of RAM, it became very unresponsive and even hung up. To solve this problem, I added ZRAM and made a few changes to the kernel parameters.

Activating ZRAM on Raspberry Pi

Raspberry Pi Performance: Adding ZRAM and Changing Kernel Parameters

ZRAM creates a block storage in RAM named /dev/zram0 (or 1, 2, 3, etc.). The pages written there are compressed and stored in memory. This allows for very fast I/O and also frees up memory through compression.

The Raspberry Pi 4 comes with 1, 2, 4, or 8GB of RAM. I will be using the 1 GB model, so adjust the instructions depending on your model. With 1 GB of ZRAM, the default swap file (slow!) will be used less frequently. I used such a script zram-swap for installation and automatic configuration.

Instructions are provided in the repository linked above. Installation:

git clone https://github.com/foundObjects/zram-swap.git
cd zram-swap && sudo ./install.sh

If you want to edit the config:

vi /etc/default/zram-swap

In addition, you can activate ZRAM by setting zram-tools. If you use this method, be sure to edit the config in file /etc/default/zramswap, and install about 1GB ZRAM:

sudo apt install zram-tools

Once installed, you can view ZRAM storage statistics with the following command:

sudo cat /proc/swaps
Filename				Type		Size	Used	Priority
/var/swap                               file		102396	0	-2
/dev/zram0                              partition	1185368	265472	5
pi@raspberrypi:~ $

Adding Kernel Options for Better ZRAM Usage

Now let's fix the system behavior when the Raspberry Pi switches to swap at the last moment, which often leads to freezes. Let's add some lines to the file /etc/sysctl.conf and reboot.

These lines 1) delay the inevitable exhaustion of memory, by increasing the pressure on the kernel cache and 2) start preparing for memory exhaustion earlier, initiating paging in advance. But it will be a much more efficient paging of compressed memory via ZRAM!

Here are the lines to be added at the end of the file /etc/sysctl.conf:

vm.vfs_cache_pressure=500
vm.swappiness=100
vm.dirty_background_ratio=1
vm.dirty_ratio=50

Then reboot the system or activate the edits with the following command:

sudo sysctl --system

vm.vfs_cache_pressure=500 increases pressure on the cache, which increases the kernel's tendency to reclaim memory used for caching directory and index objects. You will use less memory over a longer period of time. The drastic drop in performance is offset by earlier swapping.

vm.swappiness = 100 increases the parameter for how aggressively the kernel will swap pages of memory, since we use ZRAM first.

vm.dirty_background_ratio=1 & vm.dirty_ratio=50 β€” background processes will start recording as soon as the 1% limit is reached, but the system will not force synchronous I/O until it reaches 50% dirty_ratio.

These four lines (when used with ZRAM) will help improve performance if you have inevitably RAM runs out and the transition to swap begins, like mine. Knowing this fact, and also taking into account the memory compression in ZRAM by a factor of three, it is better to start this swap early.

Cache pressure helps because we're basically saying to the kernel, "Hey, look, I don't have extra memory to use for cache, so please get rid of it as soon as possible and only keep the most used/important data."

Even with less caching, if over time most of the installed memory is taken up, the kernel will start the op-swap much sooner, so that CPU (compression) and swap I/O won't pull to the last and use all resources at once when it's too late. ZRAM uses a bit of CPU for compression, but on most low-memory systems this has a much smaller performance impact than a swap without ZRAM.

In conclusion

Let's look at the result again:

pi@raspberrypi:~ $ free -h
total used free shared buff/cache available
Mem: 926Mi 471Mi 68Mi 168Mi 385Mi 232Mi
Swap: 1.2Gi 258Mi 999Mi

pi@raspberrypi:~ $ sudo cat /proc/swaps 
Filename Type Size Used Priority
/var/swap file 102396 0 -2
/dev/zram0 partition 1185368 264448 5

264448 in ZRAM is almost one gigabyte of uncompressed data. Everything went into ZRAM and nothing went into the much slower swap file. Try these settings for yourself, they work on all Raspberry Pi models. My unusable hanging system has turned into a workable and stable one.

In the near future, I hope to continue and update this article with some of the results of testing the system before and after installing ZRAM. Now I just don't have time for it. In the meantime, feel free to run your own tests and let me know in the comments. The Raspberry Pi 4 is a beast with these settings. Enjoy!

On the subject:

Source: habr.com

Add a comment