skip to content
Logo I like chickens.
they're persistent.
Faust
you better try google.com...

Compressed Memory with zram

/

The server only has 422MB of memory, running stuff like sing-box, fail2ban, and nezha. The remaining usable memory is less than 20MB. Honestly, it’s pretty impressive that it hasn’t crashed yet.

There is disk swap, but it’s as slow as crawling in the mud. Writing to it and reading it back has ridiculously high latency. I saw something called zram, installed it, and decided to note down the process.


What is zram?

Simply put: It allocates a block of memory, compresses the data stored in it, and uses it as swap.

Compared to writing to disk, it’s dozens of times faster, and the compression ratio is pretty good. The zstd algorithm can compress data to about 1/3 to 1/8 of its original size. The trade-off is a bit of CPU usage, but for modern CPUs, this overhead is negligible.

The main differences between traditional disk swap and zram are roughly as follows:

Disk swapzram
LocationDiskMemory (compressed)
SpeedSlowVery fast
CompressionNoneYes (zstd/lz4, etc.)
Suitable forFallback for large memory overflowsDaily use with small memory

Both can coexist. Right now, I’m using disk swap and zram stacked together.


Installation

Terminal window
sudo apt install zram-tools -y

The configuration file is located at /etc/default/zramswap. Two lines are enough:

Terminal window
sudo bash -c 'echo "ALGO=zstd
PERCENT=40" > /etc/default/zramswap'

PERCENT=40 means using 40% of the physical memory for zram. 422MB × 40% ≈ 168MB, which is about right. If the memory is smaller, this percentage can be tuned slightly higher. If you have plenty of memory, there’s really no need to install zram.

Then start it up:

Terminal window
sudo systemctl enable zramswap
sudo systemctl start zramswap

Verify it:

Terminal window
zramctl
free -h

If you see zstd in the ALGORITHM column and [SWAP] in the MOUNTPOINT column, you are good to go.

The output on my end after installation looks like this:

NAME ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram0 zstd 168.8M 40K 4.9K 60K 1 [SWAP]

40KB of data was compressed to only 4.9KB, a compression ratio of almost 8:1, indicating that the data put in at that moment was highly compressible.


Using it with sysctl

After installing zram, vm.swappiness can be tuned higher. This parameter controls how aggressively the kernel swaps out memory pages to swap space. The default is 60.

When using disk swap, people usually lower it because disk IO is too slow, and it’s best to avoid swapping if possible. However, since zram runs in memory, the speed is completely different, so you can be a bit more aggressive:

/etc/sysctl.conf
vm.swappiness = 80
vm.min_free_kbytes = 16384
vm.vfs_cache_pressure = 150

Raising vfs_cache_pressure makes the kernel more aggressive in reclaiming file cache and returning memory back to processes.

Apply the changes with sudo sysctl -p.


Effect after installation

Usable memory went from nearly exhausted (available < 20MB) to around 65MB, significantly reducing the chance of an OOM (Out Of Memory) kill.

It’s not a massive optimization, but it’s enough for machines with limited memory. After all, it only took five minutes.


Common Commands

Terminal window
# Check status and compression ratio
zramctl
# Check memory
free -h
# Restart (used after configuration changes)
sudo systemctl restart zramswap