Linux

Linux Swap Optimization: Improving System Performance

This guide helps you properly configure swap in Linux for systems with limited RAM. You'll learn how to adjust the swappiness parameter, create a swap file, and enable zram memory compression to reduce disk usage and improve system responsiveness.

Updated at February 16, 2026
15-30 min
Medium
FixPedia Team
Применимо к:Ubuntu 20.04+Debian 11+CentOS 8+RHEL 8+Fedora 35+

Introduction / Why This Is Needed

The swap file (swap) is a disk area that Linux uses as an extension of RAM. When physical memory runs low, the system moves inactive memory pages to swap, freeing up RAM for active processes.

Problem: Default swap settings (especially swappiness=60) are often suboptimal for desktops and servers with small amounts of RAM (2-8 GB). The system may start using the slow disk (especially HDD) too early and too actively, leading to lag, interface stuttering, and long application load times.

Solution: This guide will show you how to configure swap for a balance between speed and stability. We will:

  1. Configure the vm.swappiness parameter — controlling the system's tendency to use swap.
  2. If necessary, create/increase the swap file.
  3. Enable zram — a RAM memory compression technology that is often faster than disk-based swap.

After completing these steps, you will have a more responsive system, especially when RAM is insufficient.

Requirements / Preparation

Before starting, ensure that:

  1. You have terminal access with superuser (sudo) privileges.
  2. You know your Linux distribution (Ubuntu, Debian, CentOS, etc.) and disk type (SSD/HDD).
  3. You have checked the current swap status (this is the first step of the guide).
  4. There is sufficient free disk space (if you plan to create/increase the swap file). Recommended size: 1-2 GB for systems with 2-4 GB RAM, up to 4 GB for 4-8 GB RAM.
  5. Zram requires a Linux kernel 4.0 or newer (all modern distributions meet this requirement).

Step-by-Step Guide

Step 1: Check Current Swap Settings

First, let's see if your system has swap and how it is being used.

# General swap information (size, usage)
free -h

# More detailed information
swapon --show

# Current value of the swappiness parameter
cat /proc/sys/vm/swappiness

What to look for:

  • In the free -h output, check the Swap: line. If it shows 0B, swap is not configured.
  • The default swappiness is often 60. This means the system will start actively using swap when about 40% of RAM is filled. For desktops with small RAM, this value is usually too high.

💡 Tip: If you have an SSD and low RAM (2-4 GB), start by configuring zram (Step 4) and set swappiness in the 10-20 range.


Step 2: Configure the swappiness Parameter

The vm.swappiness parameter manages the balance between moving memory pages to swap and cleaning the cache in RAM. Values range from 0 to 100.

  • 0 — the system will avoid swap until all RAM and cache are exhausted.
  • 100 — the system will actively use swap.

Recommended values:

  • Desktop with SSD + low RAM (2-4 GB): 10-20
  • Server/workstation with large RAM (8+ GB): 10-30
  • System with HDD (older drives): 30-60 (to minimize fragmentation, but HDDs are slow anyway)

Temporary setting (until reboot):

# Set the value, for example, 15
sudo sysctl vm.swappiness=15

# Verify
cat /proc/sys/vm/swappiness

Permanent setting (after reboot):

# Edit the configuration file
sudo nano /etc/sysctl.conf

Add the following line to the end of the file:

vm.swappiness=15

Save (Ctrl+O, Enter) and close the editor (Ctrl+X). You can apply it either by rebooting or by running:

sudo sysctl -p

Step 3: Create and Activate a Swap File (If Needed)

If you have no swap or its size is insufficient (e.g., less than 1 GB for 4 GB RAM), create a new one.

Important: For SSDs, it's better to use a swap file rather than a separate partition, as a file is easier to modify. However, on very old HDDs, a partition might be slightly more efficient.

# 1. Create a file of the desired size (e.g., 2 GB).
#    The 'fallocate' flag creates the file instantly. For certainty, you can use 'dd'.
sudo fallocate -l 2G /swapfile

# If fallocate is not supported by your filesystem, use dd (slower):
# sudo dd if=/dev/zero of=/swapfile bs=1024 count=2097152

# 2. Set correct permissions (only root can read/write)
sudo chmod 600 /swapfile

# 3. Format the file as swap
sudo mkswap /swapfile

# 4. Activate the swap file immediately
sudo swapon /swapfile

# 5. Verify that the file appears in the list
swapon --show
free -h

Make the swap file permanent (to mount at boot):

# Add an entry to /etc/fstab
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

⚠️ Important: Ensure there is enough free space on the target disk partition (df -h). A swap file cannot be placed on a full partition.



Zram (formerly zswap) is a kernel module that creates a virtual block device in RAM and compresses data intended for swap. It is much faster than any disk-based swap (even on SSD) and does not wear out the disk.

Check if zram is enabled:

lsmod | grep zram

If the output is empty, the module is not loaded.

Installation and configuration (example for Ubuntu/Debian):

# Install utilities to manage zram
sudo apt update && sudo apt install zram-tools -y

# For RHEL/CentOS/Fedora:
# sudo dnf install zram-generator -y

Configure zram parameters. The main configuration file for zram-tools is /etc/default/zramswap (Ubuntu) or /etc/systemd/zram-generator.conf (RHEL family).

Example for Ubuntu/Debian (/etc/default/zramswap):

# Edit the file
sudo nano /etc/default/zramswap

Set:

# Size of the zram device in megabytes. Usually 50-100% of RAM for small amounts.
# For 2 GB RAM: 1024 (1 GB). For 4 GB: 2048 (2 GB).
# Do not set 100% of RAM; leave space for the OS.
DEVICE_SIZE=2048

# Compression algorithm (lzo-rle or zstd). zstd provides better compression but requires CPU.
ALGO=lzo-rle

Save the file.

Apply the settings:

# Restart the service (or reboot)
sudo systemctl restart zramswap

# Verify that the device is created and active
cat /proc/swaps
free -h

You should see a new line, e.g., /dev/zram0. Swap usage will now go to this compressed area in RAM.


Step 5: Apply All Settings Permanently

We have already configured:

  1. vm.swappiness via /etc/sysctl.conf.
  2. The swap file via /etc/fstab (if created).
  3. Zram via the zramswap service (it will add the device to /etc/fstab or use systemd by itself).

Finalization:

  1. Reboot the system to ensure all settings are applied automatically.
    sudo reboot
    
  2. After reboot, check the final status:
    free -h
    cat /proc/sys/vm/swappiness
    swapon --show
    
    Ensure that swap (and zram, if configured) is active and swappiness has the desired value.

Verification

  1. Swap usage: Perform normal tasks (opening a browser, editor). Monitor free -h or the htop utility (the SWAP column). Usage should be minimal during normal operation.
  2. System responsiveness: When opening applications or switching between them, there should be no long "stutters" related to disk read/write (listen for HDD activity if present).
  3. Logs (optional): If you encounter memory issues, check kernel logs:
    sudo dmesg | grep -i "killed\|oom\|swap"
    
    Ideally, there should be no OOM killer (out-of-memory killer) messages.

Possible Issues

ProblemPossible CauseSolution
Swap file does not activate after bootError in /etc/fstab or missing file.Check the syntax of the line in /etc/fstab. Ensure the /swapfile exists (ls -lh /swapfile).
System still actively uses swapswappiness is too high, or RAM is genuinely low.Decrease vm.swappiness to 5-10. Consider adding physical RAM. Zram can help, but it does not replace real memory for heavy workloads.
Zram does not work, no /dev/zram0 deviceKernel module not loaded or service not running.Check `lsmod
"Invalid argument" error during mkswapFile not created fully (e.g., fallocate on some filesystems).Delete the file (sudo rm /swapfile) and create it with dd: sudo dd if=/dev/zero of=/swapfile bs=1M count=2048.
System hangs with active swap on HDDHigh load on a slow hard drive.Urgent: Decrease swappiness. Enable zram (if CPU allows). Long-term: Replace HDD with SSD.

F.A.Q.

Why do I need swap if I have plenty of RAM?
Can I completely disable swap?
Which is better: increase swap file or configure zram?

Hints

Check current swap settings
Configure the swappiness parameter
Create and activate a swap file (if needed)
Configure zram memory compression
Apply all settings permanently
FixPedia

Free encyclopedia for fixing errors. Step-by-step guides for Windows, Linux, macOS and more.

© 2026 FixPedia. All materials are available for free.

Made with for the community