Raspberry Pi performance: add ZRAM and change kernel parameters

A couple of weeks ago, I published a review of Pinebook Pro . Since 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 find out if you have the same performance improvements.

After installing Raspberry Pi in my home server room, I noticed that in times of RAM shortage, it became very unresponsive and even hung. To solve this problem, I added ZRAM and made several changes to the kernel parameters.

ZRAM Activation on Raspberry Pi




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

The Raspberry Pi 4 comes with 1, 2, 4, or 8 GB of RAM. I will use 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 often. I used such a zram-swap script for installation and automatic configuration.

Instructions are provided in the repository at the link 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 installation zram-tools. If you use this method, be sure to edit the config in the file /etc/default/zramswap, and install about 1 GB of ZRAM:

sudo apt install zram-tools

After installation, you can view the ZRAM repository 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 parameters for better use of ZRAM


Now fix the behavior of the system when the Raspberry Pi switches to swap at the last moment, which often leads to freezes. Add a few lines to the /etc/sysctl.conf file and reboot.

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

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

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

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

sudo sysctl --system

vm.vfs_cache_pressure = 500 increases the pressure on the cache, which increases the tendency of the kernel to reclaim the memory used to cache directory objects and indexes. You will use less memory for a longer period of time. A sharp drop in productivity is nullified by earlier swaps.

vm.swappiness = 100 increases the parameter 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 immediately after reaching the limit of 1%, but the system will not force synchronous I / O until it reaches dirty_ratio of 50%.

These four lines (when used with ZRAM) will help improve performance if you inevitably run out of RAM and start switching to swap, like mine. Knowing this fact, as well as taking into account memory compression in ZRAM, it is three times better to start this swap in advance.

Pressure on the cache helps because we are actually telling the kernel: β€œHey, listen, I don’t have extra memory to use for the cache, so please get rid of it as soon as possible and keep only the most frequently used / important data.”

Even with reduced caching, if over time most of the installed memory is occupied, the kernel will start an opportunistic swap much earlier, so that the processor (compression) and swap I / O will not pull to the last and use all the resources at once, when it is too late. ZRAM uses a little CPU for compression, but on most systems with a small amount of memory, this affects performance much less than a swap without ZRAM.

Finally


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 to ZRAM and nothing got into the much slower swap file. Try these settings yourself, they work on all Raspberry Pi models. My unsuitable freezing system has turned into a workable and stable one.

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

On this topic:

All Articles