GuidesDevelopers and hostingSwap in memory with zram

Swap in memory with zram

zram on Debian 13, compressed swap that lives in memory instead of on disk, so a server that runs short of memory slows down gently instead of flooding its disk, set up with systemd's zram-generator in a few minutes.

Tested on systemd-zram-generator 1.2.1 on Debian 13 (trixie) on a Melonslab server Updated September 26, 2026

Recommended server for this guide

VC-S Micro · 2 vCPU · 8 GB Memory · 250 GB Storage

Month to month, no lock-in 7-day money-back guarantee

€7.99/mo

Deploy now
On this page

What you will set up

Swap is where Linux moves memory that has not been used for a while, when memory runs short. Swap on disk is slow, and a server that swaps a lot floods its disk with reads and writes, so everything on it slows down. zram keeps swap in memory instead, compressed: what the kernel moves there takes a fraction of its size, and getting it back does not wait for the disk.

We recommend zram instead of swap on disk. When you build a server in the Melonslab panel, leave swap disabled, and set up zram as below.

Every step below was run on a Melonslab VC-P Alloy (2 vCPU, 8 GB) with Debian 13:

  • zram gave the server 7.7 GB of swap, compressed with zstd.
  • A program filled 8 GB of memory, more than the server has. 1.5 GB of it went to zram, where it took 368 MB, about a quarter of its size, and no program was stopped for lack of memory.
  • The settings were still in place after a reboot.

1. Check for swap on disk

As root:

swapon --show

This prints nothing when the server has no swap. If it lists a file or a partition, turn it off, and comment out its line in /etc/fstab so that it stays off after a reboot:

swapoff -a
sed -i '/\sswap\s/s/^/#/' /etc/fstab

If it was a file, such as /swapfile, you can delete it afterwards with rm /swapfile.

2. Install zram-generator

apt update
apt install -y systemd-zram-generator

The package switches zram on straight away, with Debian's defaults: half of the memory, at most 4 GB, compressed with the kernel's default lz4. The next step replaces them.

3. Choose the size and the compression

Create /etc/systemd/zram-generator.conf:

[zram0]
zram-size = min(ram, 8192)
compression-algorithm = zstd

zram-size is in megabytes: as much as the server's memory, at most 8 GB, which is Fedora's default. This is how much can be moved to zram before it is compressed, not how much memory it takes. zstd compresses better than lz4, for a little more CPU.

Then create /etc/sysctl.d/99-zram.conf:

vm.swappiness = 180
vm.page-cluster = 0

A vm.swappiness above 100 tells the kernel that swapping to zram is cheaper than dropping files from its cache, which the kernel documentation suggests for swap in memory. vm.page-cluster = 0 stops the kernel from reading ahead in swap, which helps on a disk but only wastes work in memory.

4. Restart and check

The new size and compression take effect when zram is set up again, so restart the server:

systemctl reboot

Log in again, and check:

swapon --show
zramctl

swapon --show lists /dev/zram0 as the only swap, and zramctl shows zstd under ALGORITHM. Under memory pressure, DATA shows how much has been moved to zram, and COMPR how much memory it takes:

NAME       ALGORITHM DISKSIZE  DATA  COMPR  TOTAL STREAMS MOUNTPOINT
/dev/zram0 zstd          7.7G  1.5G 368.3M 379.8M       2 [SWAP]

Troubleshooting

zramctl shows lz4 and half of the memory. zram still runs with the defaults it started with when you installed the package. Restart the server, as in step 4.

swapon --show still lists swap on disk after a restart. Its line in /etc/fstab is still active. Do step 1 again, and restart.

Run it on your own server

VC-S Micro

€7.99/mo

vCPU
2
Memory
8 GB
Storage
250 GB
Transfer
10 TB
Standard
HDD · RAID 10
  • Full root access
  • Native /64 IPv6
  • RAID-protected storage
  • Malmö, Sweden
  • Month to month, no lock-in
  • 7-day money-back guarantee
All guides