Using zram for swap and tmp

I spied that the bookworm image comes with a swapfile configured. Given the uConsole is designed to rum off an SD card I disabled it and swapped it out for zram compressed in memory swap using zram-generator.

While I was at it I also configured some zram block devices for the /tmp, /var/tmp, and /var/log directories and the chromium cache in ~/.cache/chromium. Chromium is a tad snappier as a result.

Here’s my zram-generator config:

[zram0]
zram-size = 1024

[zram1]
mount-point = /var/compressed
options = X-mount.mode=1777
zram-size = 256

[zram2]
mount-point = /var/tmp
zram-size = 256

[zram3]
mount-point = /var/log
zram-size = 128

[zram4]
mount-point = /tmp
zram-size = 256

I had to apply the recommended “high-quality hack” in the zram-generator docs to set the block devices to 1777. I also used bind mounts so I could delete the old data in those mount locations.

Anyone else used/using zram or similar to reduce SD card writes?

5 Likes

I followed some random guide, which used GitHub - foundObjects/zram-swap: A simple zram swap service for modern systemd Linux

I don’t disable the swapfile, I just make it highly unfavored using sysctl config as mentioned in the guide:

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

zram-generator looks pretty cool. If I get a chance I’d like to try it out.

My to do list:

  • Write a benchmark to measure what kind of difference zram makes on my system (performance, memory usage, when the swapfile gets used, etc.)
  • Try out zram-generator
2 Likes

Today I took inspiration from the “Reduce SD card writes” option from the pi-apps “Better Chromium” package to better configure chromium caches with my zram-gernerator config.

On boot chromium now automatically creates the relevant directories and symlinks.

Here’s the hook in /etc/chromium.d/reduced_writes:

#!/bin/bash
profiles="$(find "$HOME/.config/chromium/" -maxdepth 1 -type d '(' -name Default -o -name 'Profile *' ')' | sed 's+.*/++g')"

echo "$profiles" | while read -r profile ;do  
  rm -rf "$HOME/.config/chromium/$profile/Service Worker"
  mkdir -p "/var/compressed/$USER/.config/chromium/$profile/Service Worker"
  ln -s "/var/compressed/$USER/.config/chromium/$profile/Service Worker/" "$HOME/.config/chromium/$profile/Service Worker"
done

mkdir -p "/var/compressed/$USER/.cache/chromium"
rm -rf "$HOME/.cache/chromium"
ln -s "/var/compressed/$USER/.cache/chromium/" "$HOME/.cache/chromium"
2 Likes

log2ram is also a good way to reduce writes to the SD
I believe it also has zram options built in as well