월. 8월 4th, 2025

Swap space serves as crucial virtual memory when your Linux system exhausts physical RAM. This guide covers practical management techniques to optimize performance, especially for memory-intensive tasks.


What is Swap Space?

Swap is a designated area on your storage drive that acts as overflow memory. When RAM fills up, inactive pages move to swap, preventing crashes. While slower than RAM, it’s essential for:

  • Handling unexpected memory spikes
  • Running memory-heavy applications
  • Supporting hibernation (suspend-to-disk)
  • Maintaining system stability on low-RAM devices

Checking Existing Swap Configuration

Verify current swap with:

sudo swapon --show        # Active swap devices/files
free -h                   # Memory and swap usage
cat /proc/swaps           # Detailed swap information

Creating a Swap File: Step-by-Step

Step 1: Allocate Space
Create a 4GB swap file (adjust size as needed):

sudo fallocate -l 4G /swapfile

Alternative for older systems:
sudo dd if=/dev/zero of=/swapfile bs=1M count=4096

Step 2: Set Permissions
Restrict access for security:

sudo chmod 600 /swapfile

Step 3: Format as Swap
Initialize the file:

sudo mkswap /swapfile

Step 4: Enable Temporarily
Activate immediately:

sudo swapon /swapfile

Step 5: Enable Permanently
Add to /etc/fstab:

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Adjusting Swappiness Values

Swappiness (0-100) controls how aggressively Linux moves data to swap.

  • Check current value:

    cat /proc/sys/vm/swappiness

    (Default: 60)

  • Temporarily change to 20 (less aggressive):

    sudo sysctl vm.swappiness=20
  • Make permanent:
    Edit /etc/sysctl.conf and add:

    vm.swappiness=20

Recommended Settings:

  • Desktop/Laptop: 10-30 (prioritize RAM)
  • Server: 30-60 (balance stability)
  • Database/Gaming Rig: 1-10 (minimize swap use)

Resizing Swap Safely

  1. Disable existing swap:
    sudo swapoff -v /swapfile
  2. Delete old file:
    sudo rm /swapfile
  3. Recreate with new size (e.g., 8GB):
    sudo fallocate -l 8G /swapfile
  4. Reinitialize and enable (repeat Steps 2-5 from creation guide).

Removing Swap Completely

sudo swapoff -v /swapfile    # Deactivate
sudo rm /swapfile            # Remove file
sudo nano /etc/fstab         # Delete corresponding line

Advanced Considerations

  • Multiple Swap Files: Distribute across drives for performance
  • ZRAM: Compress swap in RAM (ideal for SSDs/limited storage)
  • Hibernation: Swap size must exceed total RAM
  • SSD Wear: Modern SSDs handle swap efficiently; no need for special configuration

> Pro Tip: Monitor swap usage with htop or vmstat 1. Consistent high usage (>50%) indicates need for more RAM or application optimization.

By strategically configuring swap space, you significantly enhance system resilience without hardware upgrades. Test settings under your typical workload to find the optimal balance between RAM and swap utilization.

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다