How to Enable Zswap for Better Memory Management on Arch Linux

How to enable Zswap for better memory management on Arch Linux

Efficient memory management is vital for maintaining the responsiveness and stability of a Linux system, particularly when multitasking or using memory-intensive applications. Arch Linux, being a lightweight and customizable distribution, gives users full control over how memory is handled, including advanced features like Zswap.

Zswap is a compressed cache for swap pages that aims to improve system performance by reducing I/O to the swap device, which is typically slower than RAM. In this guide, we will explore what Zswap is, how it works, and how you can enable and configure it on Arch Linux to optimize memory usage on your system.


📌 What is Zswap?

Zswap is a kernel feature introduced in Linux 3.11 that provides a compressed RAM cache for swap pages. When a system decides to move memory pages to swap space (to free up physical RAM), Zswap first compresses these pages and keeps them in RAM rather than immediately writing them to the swap partition or file.

If the compressed data fills up the allocated cache or the system comes under memory pressure, the oldest compressed pages are written out (evicted) to the swap device. This approach has several benefits:

  • Reduces disk I/O
  • Speeds up swapping operations
  • Extends the lifespan of SSDs
  • Improves overall performance in low-memory conditions

Zswap is disabled by default in most distributions, including Arch Linux, but enabling it is relatively simple and can lead to noticeable performance improvements.


🧠 Zswap vs ZRAM

Zswap is often confused with ZRAM, but they serve slightly different purposes:

  • Zswap: Acts as a compressed write-back cache for the swap device. It only kicks in when swapping occurs and retains compressed data in RAM temporarily.
  • ZRAM: Creates a compressed block device in RAM that is used directly as swap space—no backing disk required.

Both can be used for similar purposes, but Zswap is generally easier to integrate with existing swap setups, making it a good choice for Arch Linux users looking to boost performance without extensive reconfiguration.


✅ Prerequisites

Before enabling Zswap, make sure:

  1. You are running a kernel version 3.11 or newer (Arch Linux uses much newer kernels, so you’re good).
  2. You have swap enabled (either a swap file or a swap partition).
  3. You have root access on your system.

🔍 Step 1: Check Your Current Zswap Status

First, check if Zswap is currently enabled:

cat /sys/module/zswap/parameters/enabled

If the output is Y, Zswap is already enabled. If it returns N, it is disabled.

You can also inspect other Zswap parameters with:

cat /sys/module/zswap/parameters/*

This shows current values such as compression algorithm, max pool percentage, and more.


⚙️ Step 2: Enable Zswap Temporarily (for Testing)

Before making permanent changes, you can enable Zswap for your current boot session. This is useful for testing purposes.

Add Kernel Parameters

Reboot your system and add kernel parameters through the bootloader:

  1. If you use systemd-boot:

    • Edit the file:
      /boot/loader/entries/arch.conf
  2. If you use GRUB:

    • Edit the file:
      /etc/default/grub

    • Find the line:

      GRUB_CMDLINE_LINUX_DEFAULT="quiet"
      
    • Add Zswap parameters:

      GRUB_CMDLINE_LINUX_DEFAULT="quiet zswap.enabled=1 zswap.compressor=zstd zswap.max_pool_percent=20 zswap.zpool=z3fold"
      
    • Update GRUB:

      sudo grub-mkconfig -o /boot/grub/grub.cfg
      

Explanation of Parameters

  • zswap.enabled=1 — Enables Zswap
  • zswap.compressor=zstd — Uses Zstandard for compression (faster and efficient)
  • zswap.max_pool_percent=20 — Allocates 20% of RAM for Zswap cache
  • zswap.zpool=z3fold — Uses z3fold, a memory-efficient pool type

You can choose lz4, lz4hc, zstd, or lzo as the compressor. zstd is often the best balance of speed and compression ratio.


🔁 Step 3: Reboot and Verify Zswap Is Active

After rebooting, verify that Zswap is enabled:

cat /sys/module/zswap/parameters/enabled

It should return Y.

Check other settings:

cat /sys/module/zswap/parameters/compressor
cat /sys/module/zswap/parameters/max_pool_percent
cat /sys/module/zswap/parameters/zpool

You can also monitor Zswap usage in real time:

cat /sys/kernel/debug/zswap/*

If /sys/kernel/debug/zswap doesn’t exist, mount the debug filesystem:

sudo mount -t debugfs none /sys/kernel/debug

🔐 Step 4: Make the Configuration Permanent

To keep the configuration across reboots, ensure your kernel parameters are saved.

  • For systemd-boot, make sure your arch.conf entries are persistent.
  • For GRUB, ensure GRUB_CMDLINE_LINUX_DEFAULT has the parameters and that you’ve run grub-mkconfig.

To double-check, run:

cat /proc/cmdline

It should include all Zswap parameters.


🛠️ Step 5: Troubleshooting and Tuning

Issue: Zswap Not Using Much Memory

Zswap only engages when swapping begins. If your system has ample free RAM, it may not use Zswap at all. To test, force the system to use swap:

sudo swapoff -a
sudo swapon -a
stress-ng --vm 2 --vm-bytes 90% --timeout 60s

Then check:

cat /sys/kernel/debug/zswap/stored_pages

Adjusting Pool Size

You may increase or decrease the memory used by Zswap via zswap.max_pool_percent.

Example for 30%:

zswap.max_pool_percent=30

Use cautiously—too high a value can reduce available RAM for other processes.


📉 Monitoring Zswap Usage

You can use htop, vmstat, or zswap-stats scripts to monitor Zswap in action.

To view swap activity:

vmstat 1

Look at the si (swap in) and so (swap out) columns.

For a more detailed view:

sudo watch -n 1 cat /sys/kernel/debug/zswap/*

💡 Should You Use Zswap?

✅ Use Zswap if

  • You have limited RAM (e.g., 2–8GB).
  • You use an SSD (avoids unnecessary writes).
  • You want smoother performance under memory pressure.
  • You prefer swap files over partitions.

❌ Avoid Zswap if

  • You already use ZRAM (they serve similar functions).
  • You don’t use swap at all.
  • You’re running a real-time or latency-critical system.

You can combine Zswap with other memory optimization strategies like swappiness tuning or earlyoom for even better results.


🔚 Conclusion

Zswap is a powerful yet often underutilized Linux kernel feature that can significantly improve system responsiveness and reduce swap latency. On Arch Linux, enabling Zswap is straightforward and provides benefits for both desktop users and server environments, especially on machines with limited memory or SSD-based storage.

By compressing pages before swapping and keeping them in RAM longer, Zswap reduces write operations, speeds up memory reclamation, and helps maintain a smoother computing experience under load.

Experiment with different settings like compression algorithms and pool size to find the best configuration for your workflow. With a few tweaks and the right setup, Zswap can be a valuable addition to your Arch Linux toolbox for better memory management.