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:
- Configure the
vm.swappinessparameter — controlling the system's tendency to use swap. - If necessary, create/increase the swap file.
- 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:
- You have terminal access with superuser (sudo) privileges.
- You know your Linux distribution (Ubuntu, Debian, CentOS, etc.) and disk type (SSD/HDD).
- You have checked the current swap status (this is the first step of the guide).
- 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.
- 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 -houtput, check theSwap:line. If it shows0B, swap is not configured. - The default
swappinessis 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
swappinessin 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.
Step 4: Configure Zram Memory Compression (Recommended for SSD)
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:
vm.swappinessvia/etc/sysctl.conf.- The swap file via
/etc/fstab(if created). - Zram via the
zramswapservice (it will add the device to/etc/fstabor use systemd by itself).
Finalization:
- Reboot the system to ensure all settings are applied automatically.
sudo reboot - After reboot, check the final status:
Ensure that swap (and zram, if configured) is active andfree -h cat /proc/sys/vm/swappiness swapon --showswappinesshas the desired value.
Verification
- Swap usage: Perform normal tasks (opening a browser, editor). Monitor
free -hor thehtoputility (theSWAPcolumn). Usage should be minimal during normal operation. - 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).
- Logs (optional): If you encounter memory issues, check kernel logs:
Ideally, there should be nosudo dmesg | grep -i "killed\|oom\|swap"OOM killer(out-of-memory killer) messages.
Possible Issues
| Problem | Possible Cause | Solution |
|---|---|---|
| Swap file does not activate after boot | Error 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 swap | swappiness 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 device | Kernel module not loaded or service not running. | Check `lsmod |
"Invalid argument" error during mkswap | File 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 HDD | High load on a slow hard drive. | Urgent: Decrease swappiness. Enable zram (if CPU allows). Long-term: Replace HDD with SSD. |