Using zram compressed swap to defer OOM without adding disks
Use zram to create compressed RAM-backed swap that reduces OOM risk for short memory bursts without adding disk latency. Covers manual setup, verification via swapon and mm_stat, and CPU trade-offs.
04 Aug 2026, 00:15 UTC

Problem: memory spikes cause OOM kills and disk swap is too slow
A workload that occasionally exceeds physical RAM can trigger the Out-Of-Memory (OOM) killer or stall the system due to the high latency of disk-based swap. Adding a physical swap partition prevents immediate crashes but introduces disk I/O bottlenecks and increases wear on SSDs. zram provides a middle ground: it creates a block device backed by RAM that stores swapped pages in a compressed format. The kernel treats this as swap space, but the actual RAM footprint is reduced by the compression ratio.
The useful takeaway is that zram is essentially a trade: you use CPU cycles to effectively increase your available memory capacity. It is ideal for smoothing out short memory bursts and reducing pressure, but sustained swapping to zram will increase CPU load and latency.
Prerequisites before touching swap
zram requires kernel support and root privileges to manage block devices and swap activation.
- Kernel Support: The kernel must be built with
CONFIG_ZRAM=yorCONFIG_ZRAM=m. Most modern distributions include the zram module and helper tools likesystemd-zram-generatororzram-tools. - Permissions: Root or sudo access is required for
modprobe,mkswap, andswapon. - Sizing: The device size is set in bytes via
/sys/block/zram*/disksize. A common starting point is a fraction of physical RAM (e.g., 50%). Avoid setting a disksize that exceeds total physical RAM, as the device only expands as data is added, but an oversized target can lead to misleading memory reporting.
Create and enable a zram device manually
While generators automate this process, manual setup is necessary for custom tuning or environments without helper packages. Run these commands on the host; containers require privileged device access to perform these operations.
- Load the module and confirm presence:
sudo modprobe zram lsmod | grep zramExpected check:
lsmodshould return a line containingzram. Risk: This will fail if the kernel lacks the zram module. - Initialize the device size and algorithm:
# Some kernels require hot_add to create /dev/zram0 echo 1 | sudo tee /sys/class/zram-control/hot_add # Set size to 2GB (2 * 1024 * 1024 * 1024 bytes) echo $((2*1024*1024*1024)) | sudo tee /sys/block/zram0/disksize # Select compression algorithm (e.g., lz4, lzo, or deflate) echo lz4 | sudo tee /sys/block/zram0/comp_algorithmNote:
lz4is generally preferred for speed. The algorithm must be set before the device is initialized as swap; changing it after data is written is not supported. - Format and activate as swap:
sudo mkswap /dev/zram0 sudo swapon /dev/zram0Risk:
swaponactivates the device immediately. Like all RAM-backed storage, data in zram is volatile and lost upon reboot or power failure.
Verify activation and compression efficiency
Verification ensures the kernel is utilizing the compressed device and allows you to measure the actual memory savings.
swapon --show
cat /proc/swaps
Expected check: The output must list /dev/zram0 as an active swap device.
To check the compression ratio, read /sys/block/zram0/mm_stat. This file contains several counters; the first two fields represent the number of original pages and the number of compressed pages. By comparing these, you can determine the actual compression ratio achieved by the current workload.
cat /sys/block/zram0/mm_stat
Practical check: Monitor free -h and top during a memory-intensive task. You should see the "Swap" used value increase while CPU usage spikes slightly due to the compression overhead.
Limitations and recovery options
zram is not a replacement for physical RAM. If a workload consistently exceeds the sum of physical RAM and the effective compressed capacity, the system will still trigger OOM kills or enter a state of extreme latency.
Because zram is volatile, recovery is straightforward and does not risk filesystem corruption:
- Disable swap:
sudo swapoff /dev/zram0 - Reset the device:
echo 1 | sudo tee /sys/block/zram0/reset - Unload the module:
sudo modprobe -r zram(only possible if no zram devices are currently active).
For persistent configuration, use /etc/systemd/zram-generator.conf on supported distributions to ensure the device is recreated automatically at boot.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.