Stop Thrashing Under Memory Pressure: Practical zram + systemd-oomd on Linux
Stop Thrashing Under Memory Pressure: Practical zram + systemd-oomd on Linux When a homelab box or small VPS runs out of free RAM, the failure mode is rarely a clean kill. More often the machine spends minutes thrashing: anonymous pages bounce to a slow disk swap, the page cache collapses, SSH becomes sticky, and then the kernel OOM killer finally fires. Two complementary tools fix different…
When a home lab or small virtual private server runs out of free RAM, the failure mode is rarely a clean kill. Instead, the machine spends minutes thrashing as pages bounce to slow disk swap, the page cache collapses, SSH becomes unresponsive, and the kernel OOM killer eventually fires. Two complementary tools address different aspects of this issue: zram, which provides compressed swap in RAM, and systemd-oomd, a userspace OOM daemon that monitors cgroup v2 pressure stall information (PSI) to kill offending processes before the entire system stalls.
This article walks through a practical setup for both zram and systemd-oomd, with verification steps and a safe rollback method. It does not cover hibernation solutions, in-depth zswap details, or memory capping recipes.
Swap is not an "emergency RAM" solution. Swap exists to allow rarely used anonymous pages to be reclaimed like clean file pages. Without any swap, those anonymous pages remain pinned, leading to more aggressive reclaiming and potential thrashing under memory pressure. While swap on a disk-backed device still works, it can significantly slow down pressure scenarios on a busy SSD or a slow VPS volume, turning moderate pressure into multi-second stalls. zram, however, keeps the swap path in RAM using compression (kernel documentation suggests a rough 2:1 compression ratio, though actual ratios vary by workload).
The following steps outline how to configure zram and systemd-oomd on a Linux system:
1. Ensure your Linux kernel supports zram and cgroup v2. Verify these by checking the output of `mount | grep -E cgroup2` and `cat /proc/pressure/memory`. Your distribution should already have the necessary packages installed (e.g., `zram-generator` for Fedora, Arch, or Debian/Ubuntu).
2. Install the required packages. On Fedora, `zram-generator` is typically pre-installed. For Arch Linux, install the `zram-generator` package, and for Debian/Ubuntu, install `zram-tools` or the appropriate systemd-zram-generator package from your package manager.
3. Configure zram using the `zram-generator` service. Create a minimal configuration file `/etc/systemd/zram-generator.conf` with content like:
```
/dev/null
EOF
[zram0]
zram-size = min(ram / 2, 8192)
compression-algorithm = zstd
swap-priority = 100
options = discard
```
This configuration sets the zram device size to half of the system's RAM (up to a maximum of 8192 MiB), uses the ZSTD compression algorithm, and prioritizes the swap path over disk swap.
4. Reload systemd and start the `systemd-zram-setup@zram0.service` unit to activate the zram device. Verify the zram swap using `zramctl`, `swapon --show`, and `/proc/swaps`. Check live compression statistics with `/sys/block/zram0/mm_stat` and `/sys/block/zram0/comp_algorithm`.
5. Test the setup by intentionally triggering memory pressure, such as running memory-intensive applications. Monitor the system's behavior to ensure that zram swap is being used effectively and that the system does not enter a thrashing state.
6. Disable zswap if zram is used as the primary swap mechanism. Use `sysctl` commands to check if zswap is enabled and disable it if necessary. Persist the change by adding the appropriate kernel parameter to the bootloader configuration.
By following these steps, you can achieve a balanced system that utilizes compressed swap in RAM with zram and proactively manages OOM conditions with systemd-oomd, minimizing system instability under memory pressure.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.