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
Tested onUbuntu 26.04, Ubuntu 24.04
On this page
- How zram works
- Why it's worth a try
- Should your server use zram?
- zram, zswap and disk swap compared
- Install zram on
Ubuntu
- 1. Check the server and prepare the module
- 2. Install zram-tools
- 3. Set the size and priority
- Check that zram is working
- Measure whether it pays off on your server
- I already have a swapfile. Do I remove it?
- Does zram survive a kernel update?
- Troubleshooting
- Not for you? Remove it
- FAQ
- 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 | |
| No changes to your apps | It works at the |
| 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.
free -h
vmstat 1 5
swapon --showLook 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.
cat /sys/module/zswap/parameters/enabledYou 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#
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.
sudo apt update
sudo modprobe zram
lsmod | grep zramUbuntu 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.
sudo apt update
sudo apt full-upgrade -y
sudo rebootWait about a minute, reconnect over SSH, then install the package that contains zram (about a 110 MB download).
sudo apt install -y linux-modules-extra-$(uname -r)
sudo modprobe zram
lsmod | grep zramWhen it works: the last line shows zram. If it doesn't, go to the troubleshooting table.
2. Install zram-tools#
sudo apt install -y zram-tools
systemctl is-enabled zramswapYou should see enabled, meaning the service starts on its own after boot.
3. Set the size and priority#
sudo tee /etc/default/zramswap > /dev/null <<'EOF'
ALGO=lz4
PERCENT=50
PRIORITY=100
EOF
sudo systemctl restart zramswap
systemctl status zramswap --no-pagerWhen 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#
zramctl
swapon --show
free -hThe output of swapon --show looks something like this. Your numbers will differ.
NAME TYPE SIZE USED PRIO
/dev/zram0 partition 1G 0B 100
/swapfile file 2G 0B -2When 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.
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.
- RAM is where the work happens
- zram, priority 100, takes some data first when RAM gets tight
- swapfile on disk takes what spills over when zram is full
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.
sudo systemctl restart zramswap
swapon --showUbuntu 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.
sudo apt install -y linux-modules-extra-$(uname -r)
sudo systemctl restart zramswap
swapon --showYou 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.
free -h
zramctlIf there's room, turn it off and remove it.
sudo systemctl disable --now zramswap
sudo apt purge -y zram-tools
swapon --showWhen 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.
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)