Beginner

What is zram? Set it up on Ubuntu so a low-RAM VPS rides out peaks better

Compress data in RAM instead of sending it to disk, set up in a few commands, with how to measure it and remove it

Choose your version

Tested onUbuntu 26.04, Ubuntu 24.04

On this page
  1. How zram works
  2. Why it's worth a try
  3. Should your server use zram?
  4. zram, zswap and disk swap compared
  5. Install zram on Ubuntu
  6. 1. Check the server and prepare the module
  7. 2. Install zram-tools
  8. 3. Set the size and priority
  9. Check that zram is working
  10. Measure whether it pays off on your server
  11. I already have a swapfile. Do I remove it?
  12. Does zram survive a kernel update?
  13. Troubleshooting
  14. Not for you? Remove it
  15. FAQ
  16. Compressed and still short? Time for real capacity

zram is swap space that lives in RAM and compresses data before storing it. When RAM gets tight, Linux can fold the data it rarely touches into a smaller space, without going to disk yet.

Think of vacuum bags in a wardrobe. The wardrobe doesn't get bigger, but the winter coats you seldom wear take up less room.

How zram works#

Without zram

  • RAM gets tight
  • Idle data is sent out to disk
  • Disk is slower than RAM

With zramRecommended

  • RAM gets tight, but the data stays in RAM
  • The CPU compresses the idle data
  • It is kept in zram, in a smaller space

The trade is CPU time spent compressing and decompressing.

The better the data compresses, the more room you get back. Data that is already compressed, such as images, video or encrypted data, usually saves little, so no single ratio holds for every server.

Why it's worth a try#

What you get Why
A cushion for short spikes Idle data takes up less RAM
Less waiting on disk when the system must swap This part is read and written in RAM
Fewer swap writes to disk Linux uses zram first, when it has the higher priority
No changes to your apps It works at the Linux level
Easy to try Resize it or remove it any time

Should your server use zram?#

Worth a tryRecommended

  • RAM gets tight at times, then recovers
  • The CPU has idle time
  • Several programs sit open, waiting
  • You want a cushion before disk swap

Not yet

  • RAM falls short all day
  • The CPU is already maxed out
  • The main data in RAM barely compresses
  • A database or app that suffers when swapped

First see where your server is tight.

Check RAM, CPU and swap
free -h
vmstat 1 5
swapon --show

Look at available in free -h and id in vmstat. If id stays near 0, the CPU is barely idle, and that's the thing to fix first.

zram, zswap and disk swap compared#

zram zswap Disk swap
Where data goes Compressed RAM A compressed cache in RAM in front of disk Disk
Needs disk swap No Yes, as the backing store It is the store
Uses CPU to compress Yes Yes No
When it's full Uses the next swap in line, if any Moves old data to disk swap No more swap space
Best when You want idle data kept in RAM first You have disk swap and want fewer writes You want a cushion that costs no RAM

Start with one. If both are on, zswap tries to take data before it reaches a swap device (opens in a new tab), zram included, which makes zram's effect hard to measure. This guide uses zram only. On the Ubuntu 22.04 and 24.04 kernels we checked, zswap is off by default.

Terminal
cat /sys/module/zswap/parameters/enabled

You should see N. If you see Y, zswap is on, so decide which one you'll use first.

Install zram on Ubuntu#

These commands are for kernels where uname -r ends in -generic.

1. Check the server and prepare the module#

Terminal
uname -r
systemd-detect-virt --container
df -h /

You should see the kernel name, the word none, and free disk space. If the middle line isn't none, you're inside a container and need to set this up on the host instead.

Ubuntu 26.04

The zram module ships with the kernel, so there's no extra package to install. Just refresh the package list and load the module.

Terminal
sudo apt update
sudo modprobe zram
lsmod | grep zram

Ubuntu 24.04

Update the system and reboot first. A new server often runs an older kernel than Ubuntu's archive offers, and if you skip this the extra package won't be found. It takes about 5 minutes. If you're asked about a config file, press Enter to keep the current one.

Terminal
sudo apt update
sudo apt full-upgrade -y
sudo reboot

Wait about a minute, reconnect over SSH, then install the package that contains zram (about a 110 MB download).

Terminal
sudo apt install -y linux-modules-extra-$(uname -r)
sudo modprobe zram
lsmod | grep zram

When it works: the last line shows zram. If it doesn't, go to the troubleshooting table.

2. Install zram-tools#

Terminal
sudo apt install -y zram-tools
systemctl is-enabled zramswap

You should see enabled, meaning the service starts on its own after boot.

3. Set the size and priority#

/etc/default/zramswap
sudo tee /etc/default/zramswap > /dev/null <<'EOF'
ALGO=lz4
PERCENT=50
PRIORITY=100
EOF
sudo systemctl restart zramswap
systemctl status zramswap --no-pager

When it works: the status shows active (exited). That's normal: the service sets things up and then exits.

Setting In plain words
ALGO=lz4 The compression method; the package picks it because it is faster than zstd, while zstd compresses smaller
PERCENT=50 It can take data, measured before compression, up to half of RAM
PRIORITY=100 Used before any swap that has no priority set

Check that zram is working#

Terminal
zramctl
swapon --show
free -h

The output of swapon --show looks something like this. Your numbers will differ.

Sample layout, not a test result
NAME       TYPE      SIZE USED PRIO
/dev/zram0 partition   1G   0B  100
/swapfile  file        2G   0B   -2

When it works: /dev/zram0 is listed with priority 100, and the Swap row in free -h has grown. Usage can read 0B at first, because RAM isn't tight yet.

Measure whether it pays off on your server#

Let the server run as usual through a peak, then compare with before.

Terminal
zramctl --output NAME,ALGORITHM,DISKSIZE,DATA,COMPR,TOTAL
swapon --show
vmstat 1 5
Field What it tells you
DATA Data before compression
COMPR Data after compression
TOTAL RAM actually used to store it, overhead included
USED of /swapfile How much you still lean on disk
us, sy, id in vmstat Whether the CPU has room or is maxed out

Room you get back, roughly = DATA minus TOTAL. Say DATA is 300M and TOTAL is 180M: you get 120M back. These numbers only show how to work it out.

I already have a swapfile. Do I remove it?#

No. Set up this way, you get two layers of cushion.

  1. RAM is where the work happens
  2. zram, priority 100, takes some data first when RAM gets tight
  3. swapfile on disk takes what spills over when zram is full
1 RAM, 2 zram, 3 swapfile, used in this order

This order applies when the kernel picks a place for data about to be swapped (opens in a new tab), so don't be surprised if older data stays in the swapfile.

If you don't have a swapfile yet and want that second layer, follow how to add swap on Ubuntu. It works alongside zram.

Should I change swappiness?

Start with the current value. The kernel docs say values above 100 may be worth considering for swap in RAM, but the right value depends on the workload. If you also have a swapfile, a high value affects data that spills to disk as well. See the current value with sysctl vm.swappiness.

Does zram survive a kernel update?#

After a kernel update and reboot, check with swapon --show.

Ubuntu 26.04

The module ships with every kernel, so there's nothing more to do. If /dev/zram0 is missing, restart the service.

Terminal
sudo systemctl restart zramswap
swapon --show

Ubuntu 24.04

The step people miss: the package from step 1 is tied to that kernel version. If /dev/zram0 is missing, install the one that matches the new kernel.

Terminal
sudo apt install -y linux-modules-extra-$(uname -r)
sudo systemctl restart zramswap
swapon --show

You should see /dev/zram0 back.

Troubleshooting#

Problem Likely cause Fix
Module zram not found No module for the running kernel yet Pick the version at the top that matches your server, then redo step 1
linux-modules-extra not found The server isn't updated, or you picked the wrong version Check the version with lsb_release -ds. If it matches, update, reboot and try again
zram-tools not found The universe repository is off sudo add-apt-repository universe, then sudo apt update
zramswap shows failed Read the reason in the log first sudo journalctl -u zramswap -b --no-pager
Device or resource busy An old zram device is still in use, or another tool set one up Check zramctl and use only one tool to configure it
The server got slower High CPU, or truly not enough RAM Lower PERCENT and measure again, or remove it and add RAM

Not for you? Remove it#

Before turning it off, check that available in free -h is comfortably larger than DATA in zramctl, because that data has to be decompressed back into RAM or moved to another swap.

Terminal
free -h
zramctl

If there's room, turn it off and remove it.

Terminal
sudo systemctl disable --now zramswap
sudo apt purge -y zram-tools
swapon --show

When it works: /dev/zram0 is gone, and your existing swapfile is still there. You can keep the extra module installed.

FAQ#

What is zram?

An area in memory that compresses data before storing it. It's commonly used as swap, so the RAM you have holds more, at the cost of some CPU.

Does zram add RAM to a VPS?

It doesn't add real RAM, but it stores compressible data in less space. The real effect has to be measured on your own workload.

Is the data in zram lost on reboot?

Yes, like anything in RAM. Files on disk aren't affected, and the service creates zram again after boot once the module is ready.

How many GB of zram should I set?

Start at half of RAM, as in this guide, then watch DATA, TOTAL and CPU during peaks before adjusting. No single size suits every workload.

Can I use zram with Docker?

Yes, when the host is a Linux machine you manage. Set it up on the host, not in each container, and give containers sensible memory limits.

Compressed and still short? Time for real capacity#

zram gets more out of the server you have, but if your measurements show RAM is tight every day, a server with more RAM is the more direct fix. See how to choose a VPS size for your workload, or if your server is brand new, finish securing Ubuntu first.

PlanNVMe diskBandwidthPer hourPer month
1 vCPU · 2 GB32 GB2 TB฿0.38/hr฿229/mo
2 vCPU · 4 GB40 GB4 TB฿0.70/hr฿419/mo
4 vCPU · 8 GB80 GB10 TB฿1.37/hr฿819/mo

Prices when this page was built

To try it without touching your main server, start an s-1vcpu-2gb at ฿0.38/hr, capped at ฿229/mo a month. When you're done, delete the server to stop paying for it.

References: zram (opens in a new tab), zswap (opens in a new tab) and swappiness (opens in a new tab) in the kernel docs, swap priority (opens in a new tab), and Ubuntu's zram-tools (opens in a new tab)

Follow the guide on your own server

Create a Cloud Server with Ubuntu, Debian or Windows Server, billed by the hour in Thai baht. Support answers 24/7 in Thai and English.