What Is Swap Memory and How Does It Work?


What is swap memory?
Swap is a portion of the hard disk or SSD set aside to act as virtual memory when the system's physical RAM is full.
Why Do We Need Swap Memory?
To Handle Memory Overflow
If all your RAM is used up by active processes, swap provides extra space to keep things running rather than crashing or killing apps.To Improve System Stability
Without swap, if your system runs out of RAM, it may start terminating applications, sometimes even essential ones. Swap prevents this.To Allow Hibernation
When a system hibernates, it saves the contents of RAM to swap so it can restore it later. No swap = no hibernation.
To Support Idle Processes
If a process is idle for a long time, the OS can move its data to swap, freeing up RAM for active processes.To Increase Total Available Memory
Though slower than RAM, swap increases the total memory pool (RAM + swap), giving apps more room to work with.
Downsides of Swap
Much Slower Than RAM
Since swap is on disk, it’s way slower. Using it too much leads to "thrashing" and performance issues.Not a Substitute for RAM
You still need enough RAM for good performance—swap is just a safety net.
Swap Usage Commands in Linux
free -h # Shows RAM and swap usage
swapon -s # Shows active swap partitions/files
top / htop # Shows process-wise memory usage including swap
How to enable swap Memory in linux system.
sudo fallocate -l 1G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo nano /etc/fstab
/swapfile none swap sw 0 0
Save and exit (Ctrl+O, Enter, Ctrl+X in nano).
free -h
swapon --show
How Swap Memory Works (Step-by-Step):
1. Normal Operation (Enough RAM)
When your system has enough free RAM, it keeps all running programs and data in RAM because it's super fast.
2. RAM Starts Filling Up
As more applications or processes start running, RAM usage increases.
If RAM usage reaches a certain threshold, the Linux kernel starts looking for ways to free up space.
3. Move Inactive Data to Swap
The kernel finds data in RAM that hasn’t been used recently (like minimized apps, idle background services, etc.).
It moves this "cold" or less-used data from RAM to swap space on disk.
This process is called paging or swapping out.
4. Free RAM for Active Processes
Now that some RAM has been cleared, more active or new processes can be handled smoothly in RAM.
5. Swapping Back When Needed
If an application whose data was moved to swap becomes active again (e.g., you click on a minimized app), the system swaps it back into RAM.
This process is called swapping in.
In Simple Words:
Think of RAM as your desk and swap as your drawer:
You keep important things on your desk (RAM) for fast access.
When your desk gets cluttered, you put less-used stuff into the drawer (swap).
If you need something from the drawer, you take it out again.
Subscribe to my newsletter
Read articles from Sidharth Shukla directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Sidharth Shukla
Sidharth Shukla
I am full stack web developer, currently i am learning devops and cloud technology.