Using zram for compressed RAM-backed swap space in Linux
When using a cheap VPS, available RAM is often limited. While adding a disk-backed swap space can help, it's much slower than RAM.
zram is a Linux kernel feature that creates a compressed block device in RAM that can be used as swap space. When memory is swapped to and from the zram device, it is compressed and decompressed on-the-fly.
In my case, 1.1 GB of swap data is being compressed down to just 93 MB using the zstd compression algorithm:
$ /sbin/zramctl
NAME ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram0 zstd 4G 1.1G 93M 98.4M 2 [SWAP]This compression ratio of over 10:1 means you can effectively get more usable memory without upgrading your VPS plan. zram is particularly effective because many applications nowadays allocate memory without fully utilizing it, making it highly compressible.
Another Linux kernel feature, zswap, provides a compressed cache for swap pages before they are written to disk. Unlike zram, which creates a separate compressed block device, zswap works as a cache layer for existing swap devices. I have a separate note here: Using zswap for compressed swap caching in Linux. I haven't yet made up my mind on which one is better for my use case.
A simple way to enable zram on a modern Linux system is to use the systemd-zram-generator package:
sudo apt install systemd-zram-generatorThen create a configuration file to specify the zram device size and compression algorithm:
# /etc/systemd/zram-generator.conf
[zram0]
zram-size = 4096
compression-algorithm = zstdAfter rebooting, you should see the zram device created and mounted as swap space.
$ cat /proc/swaps
Filename Type Size Used Priority
/dev/zram0 partition 4194300 1170944 100References: