Skip to main content

Managing Swap Space

1. Viewing Swap Usage

Command Description Example Output
free -h Displays memory and swap usage in a human-readable format
total        used        free      shared  buff/cache   available
Mem:           7.7G        2.1G        3.2G        156M        2.4G        5.2G
Swap:          2.0G          0B        2.0G
swapon --show Lists all active swap partitions and files with details
NAME      TYPE  SIZE     USED  PRIO
/swapfile file  2.0G     0B    -2
cat /proc/swaps Displays detailed information about active swap spaces
Filename                Type        Size     Used     Priority
/swapfile               file        2097152  0        -2
vmstat Shows virtual memory statistics including swap usage
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r  b   swpd   free   buff  cache   si   so  bi    bo   in   cs   us sy id wa st
0  0      0  3298560  123456  456789    0    0   12    34   56   78   9  1 90  0  0

Example: Check current swap usage with free

free -h

Example: List all active swap spaces

swapon --show

2. Creating Swap Partition

Step Description Action
1 Check current swap usage swapon --show
2 Identify a suitable disk partition or file lsblk
3 Create a swap file (e.g., 1GB) dd if=/dev/zero of=/swapfile bs=1M count=1024
4 Set appropriate permissions chmod 600 /swapfile
5 Set up the swap space mkswap /swapfile
6 Enable the swap file swapon /swapfile
7 Make swap persistent across reboots Add to /etc/fstab: /swapfile none swap sw 0 0

Example: Enabling Swap

swapon /swapfile
Note: Ensure the swap file is not accessible to other users by setting strict permissions.

3. Enabling Swap Partition

Command Description Notes
sudo swapon /dev/sdX2 Enable a swap partition Replace sdX2 with actual device name
sudo swapon --show Display active swap space Verify swap is enabled
sudo swapon --all Enable all swap partitions listed in /etc/fstab Useful for system boot

Example: Enabling Swap Partition

# List current swap partitions
sudo swapon --show

# Enable a specific swap partition
sudo swapon /dev/sdb2

# Verify it's active
sudo swapon --show
Note: Ensure the swap partition is properly formatted and added to /etc/fstab for persistence across reboots.

4. Creating Swap File

Example: Create a 1GB Swap File

# Create a 1GB swap file
sudo fallocate -l 1G /swapfile

# Set proper permissions
sudo chmod 600 /swapfile

# Set up the swap space
sudo mkswap /swapfile

# Enable the swap file
sudo swapon /swapfile
Command Description Notes
fallocate -l 1G /swapfile Creates a 1GB file with zeroed contents Alternative: dd if=/dev/zero of=/swapfile bs=1M count=1024
chmod 600 /swapfile Sets file permissions to owner-only read/write Required for security; swap files must not be readable by others
mkswap /swapfile Set up the file as a swap space Must be run before enabling it with swapon
swapon /swapfile Enable the swap file immediately Changes are lost on reboot unless added to /etc/fstab
Note: The swap file must not be readable by other users for security reasons.
Warning: Using fallocate is preferred over dd because it allocates space more efficiently and avoids filling the disk with zeros.

Example: Make Swap File Permanent

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

# Verify swap is active
sudo swapon --show

5. Setting Up Swap File

Creating a swap file is a common way to add virtual memory to a Linux system. Below are the steps and configurations required to set up a swap file.

Step Description Action
1. Check Current Swap Verify existing swap usage swapon --show
2. Create Swap File Allocate space for swap (e.g., 1GB) sudo fallocate -l 1G /swapfile
3. Set Permissions Secure the swap file to prevent unauthorized access sudo chmod 600 /swapfile
4. Format as Swap Initialize the file for use as swap space sudo mkswap /swapfile
5. Enable Swap Activate the swap file sudo swapon /swapfile
6. Make Permanent Ensure swap is enabled after reboot echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Example: Verify Swap Activation

free -h

Output should show the new swap space in the "Swap" row.

Note: It is recommended to set the swap file permissions to 600 to prevent unauthorized access.
Warning: Creating a large swap file can consume significant disk space. Ensure sufficient storage is available.

Swap File Configuration Summary

  • Size: Typically 1-2x RAM size for systems with less than 4GB RAM
  • Location: Preferably on a fast SSD or separate partition
  • Permissions: Must be 600 to prevent access by non-root users
  • Activation: Use swapon to activate and fallocate to create

6. Configuring Permanent Swap

Step Description Action
1 Check current swap status swapon --show
2 Create a swap file (e.g., 1GB) sudo fallocate -l 1G /swapfile
3 Set proper permissions sudo chmod 600 /swapfile
4 Set up the swap space sudo mkswap /swapfile
5 Enable the swap file sudo swapon /swapfile
6 Make it permanent by editing /etc/fstab echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Note: Ensure the swap file is not readable by other users for security reasons.

7. Disabling Swap

Command Description Notes
sudo swapoff -a Disables all swap spaces currently in use Does not remove swap space from /etc/fstab
sudo swapoff /dev/sdXn Disables a specific swap partition Replace sdXn with actual device name

Example: Disable all swap spaces

sudo swapoff -a
Note: Disabling swap does not remove it from /etc/fstab. To permanently disable, edit /etc/fstab to comment out or remove the swap entry.

8. Adjusting Swap Priority

Parameter Description Default Value Notes
/proc/sys/vm/swappiness Controls the tendency of the kernel to swap pages 60 Range: 0-100. Lower values mean less swapping
/proc/sys/vm/swappiness Value of 0 means never swap 0 Not recommended for most systems
/proc/sys/vm/swappiness Value of 100 means always swap 100 Can cause performance issues

Example: Setting swappiness to 10

sudo sysctl vm.swappiness=10

Example: Making swappiness persistent across reboots

echo "vm.swappiness=10" | sudo tee -a /etc/sysctl.conf
Note: A swappiness value of 10 is generally recommended for desktop systems to avoid excessive swapping while still providing good memory management.

9. Tuning Swappiness

Parameter Value Range Description Default
vm.swappiness 0 - 100 Controls the preference of the kernel to swap out pages vs. reclaiming memory 60

Example: Setting Swappiness to 10

sudo sysctl vm.swappiness=10

Example: Making Swappiness Change Permanent

echo "vm.swappiness = 10" | sudo tee -a /etc/sysctl.conf
Note: Lower values (e.g., 10) mean the system will try to avoid swapping pages, preferring to reclaim memory from caches and buffers instead.
Warning: Setting vm.swappiness to 0 can lead to performance issues under memory pressure as the system might not swap out less-used pages effectively.

10. Monitoring Swap Activity

Command Description Notes
free -h Displays memory usage including swap Shows total, used, and available memory in human-readable format
vmstat Reports virtual memory statistics Includes swap in/out rates and memory usage details
sar -S System activity reporter for swap Provides historical data on swap usage over time
iostat -x I/O statistics including swap activity Shows detailed I/O metrics related to swap operations

Example: Check current swap usage

free -h
              total        used        free      shared  buff/cache   available
Mem:           7.7G        2.1G        3.2G        156M        2.4G        5.2G
Swap:          2.0G          0B        2.0G

Example: Monitor swap usage over time

vmstat 1 5
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0      0 3285644 123456 2456789    0    0   123   456   123   456  5  3 92  0  0
 0  0      0 3285644 123456 2456789    0    0   123   456   123   456  5  3 92  0  0
 0  0      0 3285644 123456 2456789    0    0   123   456   123   456  5  3 92  0  0
 0  0      0 3285644 123456 2456789    0    0   123   456   123   456  5  3 92  0  0
 0  0      0 3285644 123456 2456789    0    0   123   456   123   456  5  3 92  0  0
Note: The si and so columns in vmstat show swap in/out rates. If these values are consistently high, it indicates frequent swapping which can degrade performance.

Was this article helpful?