While dealing with Linux systems, you frequently come across the word swap being uttered in conjunction with memory management. For newcomers, swap could appear like a scary term, yet Linux swap file is one of the most significant aspects of Linux that makes your system smoothly operate under intensive workloads.
At its essence, swap is virtual memory. It is an extension of your system’s physical RAM by leveraging the space on your disks as an overflow area. This comes in handy whenever your system’s RAM is completely filled. Rather than crashing or freezing up, Linux relocates some idle data from RAM to swap so that it can accommodate active processes.
There are two methods of implementing swap in Linux:
- Swap Partition – A special partition on your disk allocated for swap space.
- Swap File – A unique file on your filesystem that is used as swap space.
Although swap partitions used to be the default approach, contemporary Linux installations more and more use swap files since they are more versatile. You can create, resize, and delete them without having to modify disk partitions. This renders swap files extremely useful in desktops, laptops, and even servers that operate on virtualized hosts or cloud hosting.
In this article, we’ll dig into Linux swap files. We’ll discuss what they are, why they’re needed, how they operate, how to set them up and manage them, and what best practices you should use. Whether you’re a new user or a power user, after reading this guide, you’ll understand swap files well and be able to use them.
Understanding Memory and Swap

RAM and Its Limitation
Random Access Memory (RAM) is the speed memory in your system where applications and processes execute. The higher the RAM, the better the performance. But RAM has one limitation: it’s a finite resource. Once it’s exhausted, the system can’t load additional data into memory.
Get exclusive access to all things tech-savvy, and be the first to receive
the latest updates directly in your inbox.
Without swap, if your RAM gets exhausted, the system may freeze, slow to a crawl, or start killing processes to free memory. That’s not an ideal situation.
The Role of Swap
Swap space is like a net to fall back on. When the RAM gets congested, Linux relocates less actively used memory pages into swap. Doing so releases RAM for critical processes to run. While swap is significantly slower than RAM (as it resides on disk), it avoids system crashes and ensures stability.
In plain words:
- RAM = high-speed work area for running processes.
- Swap = slow backup area for inactive or less vital information.
Swap Partition vs Swap File
- Swap Partition: An exclusive partition of the disk for swap use. It is static and must be repartitioned to change its size.
- Swap File: An ordinary file within the filesystem that behaves similarly to swap space. It is flexible, simple to resize, and more prevalent in contemporary systems.
Due to this adaptability, swap files are the modern standard today, particularly on personal computers and cloud servers.
Why Use a Swap File?

1. Flexibility
One of the most significant strengths of swap files is flexibility. You don’t have to create or destroy swap files at any time like partitions, without altering the partition table.
2. Dynamic Needs
In cloud servers where workload is volatile, swap files allow for easy tweaking of memory management dynamically.
3. Easier Setup
It takes just a few commands to set up a swap file, while setting up a swap partition involves disk repartitioning, which is risky.
Use in Virtualized Environments
Virtual machines tend to have small disk space. A small swap partition may be inadequate. Resizing a swap file can be done without downtime or reinstalling.
How Swap Functions in Linux
Paging and Swapping
Linux employs a memory management method by the name of paging. The memory is split into units referred to as pages. In case the RAM is exhausted, the unused pages are transferred to swap.
This is usually referred to as swapping out. When they are accessed again, Linux swaps them back into RAM, a process referred to as swapping in.

The Cost of Swap
Because swap is on disk, it’s much slower than RAM. Transferring data between RAM and swap can slow down performance, particularly if your system depends heavily on swap. This thrashing needs to be prevented by making sure your system has sufficient RAM for regular usage.
Swappiness
Linux contains a kernel parameter named swappiness, which determines the extent to which the system will utilize swap. It’s a number between 0 and 100.
- 0 – Swap is reserved for when RAM is nearly depleted.
- 100 – Swap is utilized more, even when RAM is not being used.
- Default swappiness typically defaults to 60. This can be tuned to give better performance based on your usage.
Creating a Swap File
Having gone over the theory, let’s get practical. Here is a step-by-step procedure to create a swap file under Linux.
Step 1: Check Existing Linux Swap File
Before you make a new swap file, verify whether your system has swap already enabled.
swapon --show
free -h
If you see nothing, then your system isn’t utilizing swap.
Step 2: Create a Linux Swap File
Choose how large you want the swap file to be. For instance, let’s create a 2GB swap file.
sudo fallocate -l 2G /swapfile
If your system doesn’t support fallocate, you can use dd:
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
Step 3: Set Correct Permissions
The swap file must be owned by root only.
sudo chmod 600 /swapfile
Step 4: Mark as Swap
Swap the file out to become swap space.
sudo mkswap /swapfile
Step 5: Enable Swap
Mount the swap file immediately.
sudo swapon /swapfile
Step 6: Make It Permanent
To make the swap file active after a reboot, add it to /etc/fstab
:
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Now, your swap file will survive reboots.
Managing Linux Swap Files
Checking Swap Usage
To verify the current swap usage:
swapon --show
free -h
Disabling Swap
If you require disabling swap on a temporary basis:
sudo swapoff /swapfile
To permanently remove it, remove the line from /etc/fstab
and delete the file:
sudo rm /swapfile
Resizing a Linux Swap File
If your loads grow, you might need a bigger swap file. Here’s how:
Disable the existing swap file:
sudo swapoff /swapfile
Resize the file (example: 4GB):
sudo fallocate -l 4G /swapfile
Set permissions again:
sudo chmod 600 /swapfile
Mark it as swap and re-enable:
sudo mkswap /swapfile
sudo swapon /swapfile
Linux Swap File Best Practices
How Much Swap Do You Need?
Your swap file size is determined by your system usage:
- Desktop Users: 2–4GB is typically sufficient.
- Servers: Workload dependent; often equal to or twice RAM.
- Hibernation Users: Swap must be at least equal to your RAM size.
Changing Swappiness
For performance optimization, change swappiness:
cat /proc/sys/vm/swappiness
To set it to 10 (less aggressive swap usage):
sudo sysctl vm.swappiness=10
For permanent setting, add to /etc/sysctl.conf
:
vm.swappiness=10
Preventing Thrashing
If your system keeps thrashing between swap and RAM, it might be a sign of not enough RAM. In such a case, increasing RAM is preferable to using too much swap.
Swap in Newer Systems
SSDs and Swap Files
Most contemporary systems employ SSDs. Although SSDs are considerably faster than old HDDs, intensive swapping can degrade them more quickly. Nevertheless, contemporary SSDs are tough enough that moderate swap usage is not an issue.
Swap on Cloud Servers
Cloud instances tend to have limited RAM. Swap files enable administrators to provide memory flexibility without resizing partitions or having to rebuild instances.
Containerized Environments
Swap can be limited in Docker or Kubernetes. Swap files can add development flexibility but are usually turned off in production for performance predictability.
Troubleshooting Swap Problems
Shared Errors
"swapon: /swapfile: swapon failed: Invalid argument"
Typically due to file permissions set incorrectly. Correct with:
sudo chmod 600 /swapfile
Swap inactive after reboot
Make sure /etc/fstab entry is accurate and references /swapfile.
Performance declines because of swap thrashing, lower swappiness or increase RAM.
Alternatives to Swap Files
Although swap files are the most versatile solution, they aren’t the sole alternative.
- Swap Partition: Older technique, solid but less adaptable.
- zRAM: Compressed block of RAM utilized for swap, more responsive than disk-based swap.
- zSWAP: Compressed cache for swap pages stored in RAM prior to being written out to disk.
These alternatives prove useful in memory-restricted systems such as embedded devices or light servers.
Conclusion
Linux swap file is a handy yet effective feature that assists in keeping systems stable in case of RAM depletion. It serves as a buffer, preventing your programs from crashing suddenly due to memory constraints. Though slower than RAM, swap files offer flexibility and are more manageable compared to swap partitions.
From producing and sustaining swap files to performance tweaking with swappiness, knowing about swap puts you in command of your Linux system. Whether you’re using Linux on a home laptop, a server, or in the cloud, swap files are an important resource management component.
FAQs
What is the difference between a linux swap file and a linux swap partition?
A swap partition is a dedicated section of the disk reserved only for swap usage, while a swap file is a regular file created inside an existing partition. Swap files are more flexible because they can be resized or moved without repartitioning the disk.
Do I need a swap file if I already have a lot of RAM?
Not always. If you have plenty of RAM (e.g., 16GB+), your system may rarely need swap. However, having a swap file is still useful as a safety net for handling unexpected memory spikes or when using features like hibernation.
What happens if my Linux system runs out of swap?
If both RAM and swap are fully used, the system may slow down significantly, and the Out of Memory (OOM) Killer may terminate processes to free up memory.