Top Tags

Icrease Swap in Ubuntu

How to increase swap in Ubuntu

Understanding Swap Space

Swap space in Linux is a dedicated area on your storage device that the kernel uses as virtual memory when physical RAM is exhausted. It acts as an overflow area, allowing the system to continue operating when memory demands exceed available RAM.

Technical Background

How Swap Works:

  • When RAM is full, the kernel's memory management system identifies less frequently used memory pages
  • These pages are moved to the swap space through a process called "swapping out"
  • When the data is needed again, it's "swapped in" back to RAM
  • This process is managed by the kernel's swap daemon (kswapd)

Performance Considerations:

  • Swap I/O is significantly slower than RAM (typically 100-1000x slower for HDDs, 10-100x for SSDs)
  • Excessive swapping ("thrashing") can severely degrade system performance
  • Modern systems with SSDs handle swap more efficiently than traditional HDDs
  • The kernel uses swap reluctance (controlled by vm.swappiness) to balance RAM and swap usage

Recommended Swap Sizes:

  • RAM < 2GB: Swap = 2x RAM
  • RAM 2-8GB: Swap = RAM size
  • RAM 8-64GB: Swap = 0.5x RAM (minimum 4GB)
  • RAM > 64GB: Swap = 4-8GB (or based on hibernation needs)
  • For hibernation: Swap ≥ RAM size + 10% overhead

Setup Swap File

Step-by-Step Process

  1. Disable all swap spaces - Required before modifying swap configuration
  2. Allocate swap file - Create a contiguous file of specified size
  3. Set permissions - Secure the swap file (root only)
  4. Format as swap - Initialize the file with swap structures
  5. Activate swap - Enable the new swap space
bash
1sudo swapoff -a
2sudo fallocate -l 40G /swapfile
3sudo chmod 600 /swapfile
4sudo mkswap /swapfile
5sudo swapon /swapfile

Command Breakdown

swapoff -a

  • Disables all active swap spaces listed in /proc/swaps
  • The -a flag means "all" swap devices
  • Required before resizing or removing swap files

fallocate -l 40G /swapfile

  • Rapidly allocates space by updating file metadata (much faster than dd)
  • -l 40G specifies the size (40 gigabytes)
  • Creates a contiguous file at /swapfile location
  • Note: Not all filesystems support fallocate (ext4, XFS support it; Btrfs doesn't)

chmod 600 /swapfile

  • Sets read/write permissions for owner only (root)
  • Security critical: prevents non-privileged users from reading swap contents
  • Swap files with incorrect permissions won't be activated

mkswap /swapfile

  • Formats the file with swap area structures
  • Creates a swap signature and metadata
  • Establishes the page size and swap area boundaries

swapon /swapfile

  • Activates the swap space immediately
  • Adds the swap file to the kernel's active swap pool
  • Can specify priority with -p flag (higher priority = 0, default = -2)

Alternative Method Using dd (For Filesystems Without fallocate Support)

For systems where fallocate isn't supported (like Btrfs or NFS), use dd:

bash
1sudo dd if=/dev/zero of=/swapfile bs=1M count=40960 status=progress
2# count=40960 means 40960 MB = 40 GB
3# bs=1M sets block size to 1 megabyte for faster writing
4# status=progress shows real-time progress

Verify Swap Configuration

After activating swap, verify it's working:

bash
1# Check swap status
2sudo swapon --show
3
4# View detailed swap information
5free -h
6
7# Check swap usage and priority
8cat /proc/swaps
9
10# Monitor swap activity in real-time
11vmstat 1

Expected output from swapon --show:

NAME TYPE SIZE USED PRIO /swapfile file 40G 0B -2

Write it to permanent storage

To ensure the swap file persists across reboots, add it to the filesystem table:

bash
1sudo nano /etc/fstab
2/swapfile none swap sw 0 0

Understanding fstab Entry

The /etc/fstab (filesystem table) entry format:

<file system> <mount point> <type> <options> <dump> <pass> /swapfile none swap sw 0 0

Field Breakdown:

  • File system: /swapfile - path to the swap file
  • Mount point: none - swap isn't mounted at a directory
  • Type: swap - identifies this as swap space
  • Options: sw - standard swap options
  • Dump: 0 - don't backup with dump utility
  • Pass: 0 - don't check with fsck at boot

Alternative: Programmatic fstab Update

To avoid manual editing, use this command:

bash
1# Check if entry already exists
2grep -q '/swapfile' /etc/fstab || echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
3
4# Verify the entry was added
5grep '/swapfile' /etc/fstab

Optimizing Swap Performance

Adjust Swappiness

The vm.swappiness parameter controls how aggressively the kernel swaps memory pages:

bash
1# View current swappiness (default is usually 60)
2cat /proc/sys/vm/swappiness
3
4# Set swappiness temporarily (until reboot)
5sudo sysctl vm.swappiness=10
6
7# Make it permanent
8echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
9
10# Apply changes without reboot
11sudo sysctl -p

Swappiness Values:

  • 0: Swap only to avoid out-of-memory (OOM) conditions
  • 1-10: Minimal swapping, best for desktop systems with adequate RAM
  • 10-30: Low swapping preference, good for servers
  • 60: Default value, balanced approach
  • 100: Aggressive swapping, prioritizes keeping memory free

Cache Pressure Configuration

Control how aggressively the kernel reclaims memory from caches:

bash
1# View current value (default is 100)
2cat /proc/sys/vm/vfs_cache_pressure
3
4# Lower value (50) = retain caches longer
5echo 'vm.vfs_cache_pressure=50' | sudo tee -a /etc/sysctl.conf
6
7# Apply changes
8sudo sysctl -p

Managing Multiple Swap Spaces

Linux supports multiple swap areas with different priorities:

bash
1# Create a second swap file with higher priority
2sudo fallocate -l 8G /swapfile2
3sudo chmod 600 /swapfile2
4sudo mkswap /swapfile2
5sudo swapon -p 10 /swapfile2 # Higher priority (default is -2)
6
7# Add to fstab with priority
8echo '/swapfile2 none swap sw,pri=10 0 0' | sudo tee -a /etc/fstab

Priority Explanation:

  • Higher priority values are used first (range: -1 to 32767)
  • Useful for putting faster storage (SSD) at higher priority
  • Swap spaces with equal priority are used in round-robin fashion

Monitoring and Troubleshooting

Check Swap Usage Over Time

bash
1# Monitor swap in/out activity
2vmstat 5 5 # Update every 5 seconds, 5 times
3
4# Columns to watch:
5# si (swap in): KB/s swapped from disk to RAM
6# so (swap out): KB/s swapped from RAM to disk
7
8# Detailed memory statistics
9sar -r 5 5 # Requires sysstat package
10
11# Top processes using swap
12for file in /proc/*/status; do
13 awk '/VmSwap|Name/{printf $2 " " $3}END{print ""}' $file;
14done | sort -k 2 -n -r | head -10

Identify Swapping Issues

High swap usage indicators:

bash
1# Check if system is thrashing (excessive swapping)
2iostat -x 2 5 # Monitor disk I/O
3
4# If %util is constantly high + high swap I/O = thrashing
5# Solution: Add more RAM or reduce memory usage

Removing or Resizing Swap

Safely Remove Swap File

bash
1# Disable the swap file
2sudo swapoff /swapfile
3
4# Remove from fstab
5sudo sed -i '/\/swapfile/d' /etc/fstab
6
7# Delete the file
8sudo rm /swapfile

Resize Existing Swap

bash
1# Disable current swap
2sudo swapoff /swapfile
3
4# Resize the file
5sudo fallocate -l 60G /swapfile # New size: 60GB
6
7# Re-initialize and activate
8sudo mkswap /swapfile
9sudo swapon /swapfile
10
11# Verify new size
12free -h

Security Considerations

Encrypted Swap

For sensitive systems, consider encrypting swap:

bash
1# Install cryptsetup
2sudo apt install cryptsetup
3
4# Add to /etc/crypttab
5echo 'swap /dev/sdX2 /dev/urandom swap,cipher=aes-xts-plain64,size=256' | sudo tee -a /etc/crypttab
6
7# Update fstab to use encrypted swap
8sudo nano /etc/fstab
9# Change: /dev/sdX2 to /dev/mapper/swap

Clearing Swap on Shutdown

To clear sensitive data from swap on shutdown:

bash
1# Create systemd service
2sudo nano /etc/systemd/system/swap-clear.service

Add this content:

ini
1[Unit]
2Description=Clear swap before shutdown
3DefaultDependencies=no
4Before=shutdown.target
5
6[Service]
7Type=oneshot
8ExecStart=/sbin/swapoff -a
9
10[Install]
11WantedBy=shutdown.target

Enable the service:

bash
1sudo systemctl enable swap-clear.service

Best Practices Summary

  1. Size appropriately: Base swap size on RAM and use case
  2. Secure permissions: Always set chmod 600 on swap files
  3. Tune swappiness: Lower values (10-30) for modern systems with adequate RAM
  4. Use SSDs wisely: While SSDs handle swap better than HDDs, excessive swapping still wears them
  5. Monitor regularly: Check swap usage patterns to optimize configuration
  6. Priority matters: Use multiple swap spaces with priorities for performance
  7. Consider zswap: For systems with limited storage, compressed RAM-based swap can help
  8. Plan for hibernation: Ensure swap ≥ RAM if you use suspend-to-disk

Troubleshooting Common Issues

Issue: fallocate: fallocate failed: Operation not supported

bash
1# Solution: Use dd instead (filesystem doesn't support fallocate)
2sudo dd if=/dev/zero of=/swapfile bs=1M count=40960

Issue: swapon: /swapfile: insecure permissions 0644, 0600 suggested

bash
1# Solution: Fix permissions
2sudo chmod 600 /swapfile
3sudo swapon /swapfile

Issue: System still runs out of memory despite swap

bash
1# Check if swap is being used
2free -h
3# If swap is full, you need more RAM or must reduce memory usage
4# Consider enabling zswap for compression:
5echo 1 | sudo tee /sys/module/zswap/parameters/enabled

Issue: Slow system performance with available RAM

bash
1# Check if swap is being used unnecessarily
2free -h
3# If swap is used but RAM is available, reduce swappiness:
4sudo sysctl vm.swappiness=1