A few weeks ago, I published a PineBook Pro review article on this blog. Like the Pinebook Pro, the Raspberry Pi and more recently the Raspberry Pi 4 are also ARM based. I have applied some of the optimizations described in that previous article to the Raspberry Pi 4 with good results. I wanted to share these fixes and hopefully find out if you too experience a similar improvement in performance.
After adding a Raspberry Pi to my homelab, at times when the memory was low, it would become very unresponsive and even freeze. To help address this, I added ZRAM and made some changes to the kernel parameters.
Enable ZRAM on Raspberry Pi
ZRAM Creates RAM based block storage named /dev/zram0 (or 1, 2, 3, etc.). The pages written there are compressed and stored in memory. This allows very fast I/O and the compression savings provide additional memory.
Raspberry Pi 4 comes with a maximum memory of 1GB, 2GB, 4GB or 8GB of RAM. I will use the 1GB model. This requires some adjustments depending on your use case. The default /swapfile (slow!) with 1GB of ZRAM will now be the last resort. I ended up using zram-swap script To install and auto configure.
Instructions are listed on that page. Here’s how to install:
git clone cd zram-swap && sudo ./install.sh
Optional, if you want to edit the configuration:
Alternatively, you can enable ZRAM by installing
zram-tools, If you use this method, make sure to edit the configuration Located in
/etc/default/zramswap And setup about 1GB of ZRAM:
sudo apt install zram-tools
Once installed, you can view the storage statistics of ZRAM using 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:~ $
Add kernel parameters to make better use of ZRAM
Now, instead of leaving swapping until the last minute, where it often brings the Raspberry Pi to a crippling halt. I have added the following lines to it /etc/sysctl.conf and rebooted.
these lines 1) inevitable delay, Running out of memory. This is done by increasing the kernel’s cache pressure and 2) Start preparing for running out of memory early By increasing your tendency to swap out your Raspberry Pi. However, swap will now be stored via a much faster ZRAM!
Here are the lines you’ll want to add at the end of your /etc/sysctl.conf file:
vm.vfs_cache_pressure=500 vm.swappiness=100 vm.dirty_background_ratio=1 vm.dirty_ratio=50
Then rebootor enable it with:
sudo sysctl --system
vm.vfs_cache_pressure=500 – Increases cache pressure, which increases the tendency of the kernel to reclaim memory used for caching directory and inode objects. You will use less memory in the long run. Soon the performance hit is negated by the downside of swapping.
vm.swappiness=100 – Since we’re using ZRAM first, increasing how aggressively the kernel will swap memory pages.
vm.dirty_background_ratio=1 and vm.dirty_ratio=50 – Background processes will start writing immediately when 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, if like me you know you Will Eventually ends up using more than installed memory = swapping. Thus, knowing that you will swap and ZRAM makes swapping less expensive and can store data 3 times smaller and start this swap exchange sooner than later.
Cache pressure helps because we’re actually telling the kernel… “Hey, look man, I don’t have any extra memory to use for cache, so please get rid of it as soon as possible and only the most Store the most frequently used/critical data”
Even with low caching, if over time we use up most of the installed memory, the kernel will very quickly start opportunistic swapping in ZRAM so that CPU (compression) and swap I/O delay for a very long time/all at once Don’t be ZRAM uses some CPU for compression but in most low-memory scenarios this is much less of a performance hit than swapping without ZRAM enabled.
Let’s take a look at the results 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
ZRAM contains 264448 about 1G of data, compressed. Everything changed to ZRAM and nothing too slow / hit the swapfile. Try this setup, it will work on all Raspberry Pi models. My setup went from unusable and freezing to performant and stagnant.
In the near future, I hope to update and update this article with some before versus after results. I don’t have time to do that right now. In the meantime, feel free to test your own and let me know in the comments. raspberry pi 4 There’s a beast with these tweaks. enjoy!
Linux Performance: Why You Should Almost Always Add Swap Space (2017)
Linux performance: almost always add swap. Part 2: ZRAM (2020)