Methods of payment Abuse

How to Set Up ZRAM on a VPS with Ubuntu 24.04

01.07.2025, 17:46

If your server has limited RAM and the disk isn’t particularly fast, ZRAM can help speed things up noticeably. It uses a portion of your memory as compressed swap space — meaning you can fit 2–3 times more data into RAM, and it's still faster than writing to disk. In this guide, we’ll show you how to enable ZRAM on Ubuntu 24.04, and how to combine it with a regular swap file if needed.

What is ZRAM and Why Use It?

ZRAM is a Linux kernel feature that creates a compressed swap area directly in RAM. Instead of writing unused data to disk (which is slow and wears out SSDs), the system compresses the data and keeps it in memory. This is especially helpful on VPS instances with just 1–2 GB of RAM.

Here’s how it works: a special device is created in memory, and the system begins swapping data to it using a compression algorithm — typically lz4 or zstd. With good compression, 1 GB of ZRAM can hold up to 3 GB worth of data.
ZRAM doesn’t fully replace regular swap, but in many cases, it’s all you need. And if your server runs memory-intensive apps, you can still add a traditional swap file for backup.

How to Enable ZRAM on Ubuntu 24.04

Ubuntu supports ZRAM out of the box, but it’s disabled by default. The easiest and recommended way to enable it is by installing the zram-tools package — it sets everything up automatically and runs as a systemd service.

Step 1: Update Your System

sudo apt update && sudo apt upgrade -y

This ensures you’re working with the latest packages and helps avoid potential issues.

Step 2: Install zram-tools

sudo apt install zram-tools

After installation, Ubuntu will have a zramswap.service that activates ZRAM at boot.

Step 3: Configure ZRAM

Edit the config file:

sudo nano /etc/default/zramswap

Example settings:

ALGO=lz4
PERCENT=50
PRIORITY=100

What they mean:
— ALGO=lz4 — a fast compression algorithm. You can try zstd if you prefer better compression at a small speed cost.
— PERCENT=50 — this sets how much of your RAM will be allocated to ZRAM (as a percentage). You can raise this to 75, but keep in mind — this is still using your RAM.
— PRIORITY=100 — sets the swap priority. The higher the number, the sooner the system will use ZRAM over other swap options.

Step 4: Restart the Service

Apply your changes:

sudo systemctl restart zramswap

For good measure, refresh all swap devices:

sudo swapoff -a && sudo swapon -a

How to Check if ZRAM is Working

Run:

swapon --show

You should see /dev/zram0 listed with the priority you set.

To get more details, use:

zramctl

You’ll see:
— the logical size of the device;
— how much data is stored;
— how much RAM is actually used;
— which compression algorithm is active.

You can also check total memory usage with:

free -h

Or open htop — ZRAM shows up as part of swap. You might want to enable additional columns to see more detailed metrics.

For live performance monitoring under load:

vmstat 1

Keep an eye on the si and so columns — they show how much data is being read from and written to swap. If these stay near zero, your system is handling memory efficiently.

Using ZRAM Alongside a Swap File

ZRAM works well on its own, but sometimes it’s a good idea to also have a traditional swap file — especially if:
— your server has very little RAM (e.g., 512 MB or 1 GB);
— you run heavy apps like databases, compilers, or ML workloads;
— you need to handle sudden spikes in memory usage;
— your hosting provider kills processes when memory runs out.

If you configure both ZRAM and swap, the system will prioritize based on the PRIORITY values. Typically, ZRAM gets a high priority (like 100), and disk swap a lower one (like 10). That way, ZRAM is used first, and the disk-based swap kicks in only when needed.

Final Thoughts

ZRAM is a great way to stretch your available memory on a VPS and reduce disk load. It’s especially useful when system resources are tight, and you’re not running anything too demanding. Setup takes just a few minutes, but the benefits — better stability and responsiveness — can be substantial.

When properly configured, ZRAM-based swap will only be used when absolutely necessary, helping your server run smoother without hitting performance bottlenecks.