How to Configure and Enable Swap Space on Arch Linux
Categories:
5 minute read
Swap space plays a critical role in maintaining system stability and performance, particularly in environments with limited physical memory. On Arch Linux, a lightweight and flexible distribution favored by advanced users, swap is not enabled by default during installation. However, configuring swap space is straightforward and highly customizable depending on your system’s needs.
In this guide, we’ll cover what swap space is, why it’s important, and walk through the steps to create, configure, and manage swap space on an Arch Linux system using either a swap file or a dedicated swap partition.
What is Swap Space?
Swap space is a designated area on a storage device (such as a hard drive or SSD) used when the system’s physical RAM (Random Access Memory) is fully utilized. It acts as an overflow space that holds less-used memory pages, freeing up RAM for more active processes.
There are two main types of swap space:
- Swap partition: A separate partition specifically formatted and reserved for swap.
- Swap file: A file located within an existing file system that the OS treats as swap space.
While swap partitions offer slightly better performance in some scenarios, swap files provide greater flexibility and are easier to resize or relocate.
Why Use Swap on Arch Linux?
Even though modern systems often come with ample RAM, enabling swap on Arch Linux can benefit you in multiple ways:
- Prevents system crashes when RAM is exhausted.
- Supports hibernation (if using a swap partition or file large enough).
- Allows smooth operation of memory-intensive applications.
- Provides a buffer for unexpected memory spikes.
Check for Existing Swap
Before configuring new swap space, check if swap is already enabled on your system:
swapon --show
If no output is returned, swap is not currently active.
Alternatively, verify swap status via:
free -h
Look at the “Swap” row to confirm whether any space is in use or available.
Method 1: Creating a Swap File (Recommended)
Step 1: Choose Swap File Size
Swap file size depends on your system’s RAM and intended use. General guidelines:
- 2 GB RAM or less → 2–4 GB swap
- 4–8 GB RAM → 1–2 GB swap
- For hibernation → Swap size ≥ RAM size
Assume we create a 2 GB swap file for this example.
Step 2: Create the Swap File
sudo fallocate -l 2G /swapfile
Alternatively, if fallocate
is not suitable (e.g., if you need zeroed blocks), use:
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress
Step 3: Set Permissions
Ensure the swap file has correct permissions to prevent security risks:
sudo chmod 600 /swapfile
Step 4: Format the File as Swap
sudo mkswap /swapfile
Step 5: Enable the Swap File
sudo swapon /swapfile
Verify it is active:
swapon --show
You should see /swapfile
listed.
Step 6: Make Swap Persistent Across Reboots
Edit the /etc/fstab
file:
sudo nano /etc/fstab
Add the following line at the end:
/swapfile none swap defaults 0 0
Save and exit. The swap file will now be enabled automatically at boot.
Method 2: Creating a Dedicated Swap Partition
If you prefer a separate partition for swap:
Step 1: Identify the Partition
Use lsblk
or fdisk -l
to find a free partition, e.g., /dev/sdXn
.
lsblk
Let’s assume the target is /dev/sda3
.
Step 2: Format as Swap
sudo mkswap /dev/sda3
Step 3: Enable the Swap Partition
sudo swapon /dev/sda3
Step 4: Make Persistent
Add to /etc/fstab
:
/dev/sda3 none swap defaults 0 0
Or use UUID for reliability:
sudo blkid /dev/sda3
Example output:
/dev/sda3: UUID="abcd-1234" TYPE="swap"
Then in /etc/fstab
:
UUID=abcd-1234 none swap defaults 0 0
Adjusting Swappiness and Cache Pressure
Swap behavior can be fine-tuned using two kernel parameters:
1. Swappiness
This value (0–100) controls how aggressively Linux moves data from RAM to swap:
0
: Avoid swapping unless absolutely necessary60
: Default100
: Swap very aggressively
To view current value:
cat /proc/sys/vm/swappiness
To temporarily change:
sudo sysctl vm.swappiness=10
To set permanently, edit /etc/sysctl.d/99-sysctl.conf
(or create it):
sudo nano /etc/sysctl.d/99-sysctl.conf
Add:
vm.swappiness=10
2. VFS Cache Pressure
This value controls how aggressively the kernel recycles memory used for caching directory and inode information.
Check current value:
cat /proc/sys/vm/vfs_cache_pressure
Set a more conservative value (e.g., 50
):
sudo sysctl vm.vfs_cache_pressure=50
To make it persistent, add the following line to /etc/sysctl.d/99-sysctl.conf
:
vm.vfs_cache_pressure=50
Disabling Swap
If you decide to disable swap:
Step 1: Turn Off Swap
sudo swapoff -a
Step 2: Remove from /etc/fstab
Edit /etc/fstab
and delete or comment out the swap entry.
Step 3: (Optional) Delete Swap File
sudo rm /swapfile
Or delete the swap partition using tools like gparted
or fdisk
.
Monitoring Swap Usage
Use the free
command:
free -h
Or use top
, htop
, or vmstat
:
vmstat 1
You can also install htop
for a graphical interface:
sudo pacman -S htop
Within htop
, look at the “Swap” bar near the top.
Conclusion
Enabling and configuring swap space on Arch Linux is a crucial system optimization step, especially for systems with limited RAM or for users planning to hibernate their machines. Whether you use a swap file or a dedicated partition, the setup is straightforward and flexible.
By managing swappiness and monitoring usage, you can fine-tune performance to match your workload and hardware capabilities. And since Arch Linux emphasizes user control and customization, understanding how swap works gives you even more leverage to optimize your system just the way you like it.
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.