Understanding and Configuring Swap Space on Ubuntu Linux
Swap space plays a crucial role in the performance and stability of a Linux system, especially when the physical RAM is fully utilized. In this article, we’ll explore the concept of swap space, its significance, and the steps to configure it on Ubuntu Linux.
What is Swap Space?
Swap space, also known as virtual memory, is a designated area on a storage device that the operating system uses as an extension of physical RAM. When the RAM is fully occupied, the system can transfer less frequently used data to the swap space to free up RAM for more critical processes. This prevents the system from becoming unresponsive due to memory exhaustion.
Why Do You Need Swap Space?
- Preventing Out of Memory Issues: In situations where physical RAM is insufficient, having swap space prevents the system from running out of memory. This helps avoid crashes or freezes due to lack of available memory.
- Handling Hibernation: Swap space is crucial for hibernation functionality. When a system hibernates, the contents of the RAM are written to the swap space, allowing the system to resume its state when powered on again.
- Supporting Memory-Intensive Tasks: Swap space allows the system to handle memory-intensive tasks that may exceed the available physical RAM.
How To Add Swap Space on Ubuntu 22.04 LTS
Adding swap space on Ubuntu 22.04 LTS involves a few steps. Swap space is used as virtual memory when your system’s physical RAM is fully utilized. Here’s a step-by-step guide to add swap space:
Method 1: Create a Swap File
1. Check Current Swap:
Before adding swap space, check if you already have a swap partition or file by running:
$ swapon --show
2. Create a Swap File:
If we don’t have an existing swap file, you can create one. The size of the swap file depends on your system’s requirements. For example, to create a 2GB swap file:
$ sudo fallocate -l 2G /swapfile
If fallocate is not available, you can use dd:
$ sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
3. Set Permissions:
Ensure that only the root user can read and write to the swap file:
$ sudo chmod 600 /swapfile
4. Make it a Swap Space:
Mark the file as swap:
$ sudo mkswap /swapfile
5. Activate the Swap File:
Enable the swap file:
$ sudo swapon /swapfile
To make the swap file permanent, add it to the /etc/fstab file:
$ echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Method 2: Create a Swap Partition
1. Create a Partition:
Use a partitioning tool (e.g., fdisk or gparted) to create a new partition. Mark the partition type as 82 (Linux swap).
2. Format the Partition:
Format the partition as swap:
$ sudo mkswap /dev/sdXn
Replace /dev/sdXn with our actual partition identifier.
3. Activate the Swap Partition:
Enable the swap partition:
$ sudo swapon /dev/sdXn
To make the swap partition permanent, add it to the /etc/fstab file:
$ echo '/dev/sdXn none swap sw 0 0' | sudo tee -a /etc/fstab
Verify
Verify that the swap space is added by running:
$ free -h
We should see the swap space in the output. Remember to adjust the size of the swap space according to your system requirements. If your system has an SSD, consider using a smaller swap space, or none at all, as SSDs have limited write endurance.
FAQ — Swap Space on Ubuntu Linux
1. What is swap space?
Swap space is a reserved area on your storage device that Ubuntu uses as virtual memory. When your physical RAM is fully used, the system moves less-active data from RAM to swap to free up memory for active tasks.
2. Why do I need swap space on Ubuntu?
Swap helps prevent out-of-memory errors, supports memory-intensive workloads, and is required if you want to use hibernation. Without swap, your system might freeze or kill processes when RAM runs out.
3. What forms can swap take on Ubuntu?
Ubuntu supports both swap files and swap partitions. A swap file is easier to resize and configure after installation, while a swap partition is a dedicated partition on disk.
4. How do I check if swap is already enabled?
You can check active swap with the command:
swapon --show
If output is empty, no swap is currently in use.
5. How much swap should I allocate?
There’s no one-size-fits-all answer, but general guidance is:
- At least enough to cover hibernation if you use it (swap ≥ RAM).
- For systems with less RAM (<4 GB), swap equal to RAM or a bit more is helpful.
- Modern systems with lots of RAM may need less swap unless hibernation or heavy memory usage is expected.
6. How do I create a swap file?
A typical way is:
- Create the file (e.g., 2 GB):
sudo fallocate -l 2G /swapfile
- Restrict permissions:
sudo chmod 600 /swapfile
- Mark it as swap and enable it.
- Add it to
/etc/fstabto enable on boot.
7. Can I resize swap later?
Yes! If you used a swap file, you can disable the current file, delete or shrink it, then create a new one with the desired size. For partitions, you may need to resize or recreate the partition. (Always back up data before resizing partitions.)
8. What is swappiness and does it matter?
Swappiness is a Linux kernel setting that determines how aggressively the system moves data from RAM to swap. Lower values favor keeping things in RAM, which can improve performance. The default on Ubuntu is typically 60, but adjusting it (e.g., to 10) can benefit some workloads.
9. Does Ubuntu automatically create swap?
Yes — recent Ubuntu releases often create a default swap file during installation, even without a dedicated swap partition.
10. What happens if my system runs out of swap?
If both RAM and swap are exhausted, the system may terminate processes to free memory or become unstable. Swap acts as a safety net but isn’t as fast as RAM.









