How to Configure zram-tools Swap on Debian

This guide demonstrates how to install and configure zram-tools to create compressed swap space directly in your system's RAM on Debian. Utilizing zram enhances multitasking capabilities and prevents out-of-memory slowdowns on resource-limited systems by compressing inactive memory pages instead of writing them to a slower physical disk.

Step 1: Update the Package Repository

Before installing new packages, ensure your local package index is up to date:

sudo apt update

Step 2: Install zram-tools

Debian provides zram-tools in its standard repositories. Install the package using apt:

sudo apt install zram-tools -y

Step 3: Configure zram-tools

The behavior of zram-tools is managed via the /etc/default/zramswap configuration file. Open this file in your preferred text editor:

sudo nano /etc/default/zramswap

Modify or uncomment the following parameters according to your needs:

  • ALGO: Specifies the compression algorithm. zstd offers an optimal balance between compression ratio and speed, while lz4 provides faster speeds with slightly lower compression.

    ALGO=zstd
  • PERCENT: The percentage of total physical RAM allocated to the zram swap device. A setting between 50% and 100% is common depending on your workload:

    PERCENT=50
  • PRIORITY: Sets the priority of the zram swap device. Setting this to a high value (such as 100) ensures the kernel utilizes fast zram swap before utilizing traditional disk-based swap:

    PRIORITY=100

Save the file and exit the editor (in nano, press Ctrl+O, Enter, and then Ctrl+X).

Step 4: Restart the zramswap Service

Apply the configuration changes by restarting the systemd service responsible for initializing the zram device:

sudo systemctl restart zramswap

To ensure the service starts automatically during system boot:

sudo systemctl enable zramswap

Step 5: Verify the zram Swap Allocation

Confirm that the zram swap device is active and operating with the chosen parameters:

zramctl

The output will display the device name (/dev/zram0), chosen compression algorithm, disk size, and current data usage.

You can also verify that the swap space is recognized by the kernel:

swapon --show

Your /dev/zram0 partition will appear in the list with the assigned priority, confirming that compressed RAM swap is fully functional on your Debian system.