How to Increase Swap Space on Linux

Here's a step-by-step guide to increase swap space on Linux systems, including both creating a new swap file and resizing existing swap space.

Bertie Atkinson

Swap space serves as an extension of your physical RAM, providing a safety net when your system runs low on memory. Think of it as an overflow area – when your RAM fills up, the system can temporarily move less-used data to this swap space on your disk, preventing out-of-memory crashes. Let me walk you through the process of checking, creating, and increasing swap space on Linux.

Understanding Your Current Swap Situation

Before making any changes, let’s examine your current swap configuration:

# Check current memory and swap usage
free -h

# View all swap areas in use
swapon --show

# See partition information including swap
sudo fdisk -l

# Check swap configuration in /etc/fstab
cat /etc/fstab | grep swap

The output of free -h will show you both your RAM usage and swap usage in human-readable format. The “Swap:” line shows total swap space, how much is used, and how much is free.

The easiest and safest approach is creating a swap file. Unlike partitioning, this doesn’t require modifying disk structures and can be done on a running system.

Step 1: Create a swap file of your desired size

# Create a 4GB swap file (adjust size as needed)
sudo fallocate -l 4G /swapfile

# Alternative method if fallocate isn't available
sudo dd if=/dev/zero of=/swapfile bs=1G count=4

The fallocate command quickly allocates space without actually writing data. If it’s not available, the dd command works too, but takes longer since it writes zeros to the entire file.

Step 2: Secure the swap file

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

This is crucial for security—swap can contain sensitive data from memory, so we restrict access to root only.

Step 3: Format as swap space

# Mark the file as swap space
sudo mkswap /swapfile

This command prepares the file with the proper format for use as swap space.

Step 4: Enable the swap file

# Activate the swap file
sudo swapon /swapfile

Your system will immediately begin using this new swap space.

Step 5: Make the change permanent

# Add to /etc/fstab to persist across reboots
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

This ensures your swap file is automatically activated when your system reboots.

Step 6: Verify the new swap space

# Check that the new swap space is available
free -h
swapon --show

You should see your total swap space has increased by the amount you allocated.

Method 2: Increasing Swap with a New Swap Partition

If you prefer using a dedicated partition (often recommended for servers with predictable swap needs), here’s how:

Step 1: Identify available disk space

# List block devices
lsblk

# Check available space
df -h

Look for unallocated space or a partition you can repurpose.

Step 2: Create a new partition

# Start the partitioning tool for your disk
sudo fdisk /dev/sdX  # Replace sdX with your disk identifier

Within fdisk:

  1. Press n to create a new partition
  2. Choose partition type and number
  3. Specify the size
  4. Change the partition type by pressing t and entering the swap code (82)
  5. Write changes with w

Step 3: Format the partition as swap

# Format the new partition
sudo mkswap /dev/sdXY  # Replace with your new partition

Step 4: Enable the swap partition

# Activate the swap partition
sudo swapon /dev/sdXY

Step 5: Make the change permanent

# Add to /etc/fstab
echo '/dev/sdXY none swap sw 0 0' | sudo tee -a /etc/fstab

Method 3: Resizing an Existing Swap File

If you already have a swap file but need more space:

# Turn off the existing swap
sudo swapoff /swapfile

# Resize the file (example: increase to 8GB)
sudo fallocate -l 8G /swapfile

# Format it as swap
sudo mkswap /swapfile

# Turn the swap back on
sudo swapon /swapfile

# Verify the new size
free -h

Optimizing Swap Performance

After increasing your swap space, you might want to adjust how aggressively Linux uses it:

# Check current swappiness value (0-100)
cat /proc/sys/vm/swappiness

# Set a new value temporarily (higher = more aggressive swapping)
sudo sysctl vm.swappiness=10

# Make the setting permanent
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf

The swappiness value ranges from 0 to 100:

  • Lower values (e.g., 10): The system will avoid using swap space when possible, preferring to keep applications in RAM.
  • Higher values (e.g., 60): The system will more aggressively move data from RAM to swap, which might free up more RAM but could slow things down.

For desktop systems, a value of 10-20 often works well. For servers, the optimal setting depends on your specific workload.

Understanding Swap Size Guidelines

How much swap space do you need? It depends on your system’s RAM and workload:

  • Systems with less than 2GB RAM: Often use 2x RAM for swap
  • Systems with 2-8GB RAM: Usually equal to RAM amount
  • Systems with more than 8GB RAM: Typically 8GB or based on specific needs

For hibernation support, you generally need at least as much swap as you have RAM.

Troubleshooting Common Issues

If you encounter problems, here are some solutions:

  • “swapon: /swapfile: skipping – it appears to have holes” This happens when fallocate doesn’t work correctly. Use the dd method instead.
  • “swapon: /swapfile: insecure permissions” Fix with: sudo chmod 600 /swapfile
  • Swap not mounting at boot Check your /etc/fstab entry for errors.

By following these steps, you’ll successfully increase your system’s swap space, providing that extra memory cushion when your applications need it. This ensures more stable performance, especially when running memory-intensive tasks.

Remove Swap File

  • sudo swapoff /swapfile # Disable swap
  • sudo rm /swapfile # Delete the file
  • sudo nano /etc/fstab # Remove the swapfile entry
Share This Article