What is Linux Swap?
Think of your computer’s memory (RAM) as your physical desk. When you’re actively working on projects (running applications), you keep the documents on your desk for immediate access. But what happens when your desk gets entirely covered, and you need to open another file?
You take the documents you haven’t looked at in a while and move them to a filing cabinet to make room.
In Linux, Swap is that filing cabinet. It is a designated space on your hard drive or SSD that the Linux kernel uses as “virtual memory.” When your physical RAM is dangerously close to being full, the system temporarily moves inactive memory pages from RAM to the swap space. This frees up RAM for your active, high-priority tasks.
Why Do You Need Swap?
Even if you have a massive amount of RAM, having a swap space is generally considered best practice for a few key reasons:
- Prevents System Crashes (OOM Killer): If your RAM fills up and there is no swap space, the Linux Out-Of-Memory (OOM) killer steps in and starts forcefully terminating applications to save the system from crashing. Swap gives your system a safety net.
- Hibernation: If you want to put your computer into hibernation, the system must write the entire contents of your RAM to the hard drive before powering down. It writes this data to the swap space. (You need a swap size at least as large as your RAM to do this).
- Memory Optimization: Linux is smart. Even if you aren’t out of memory, it will proactively move completely dormant background processes into swap to free up physical RAM for disk caching, which actually speeds up your system.
Note: Because hard drives and SSDs are significantly slower than physical RAM, actively running applications out of swap space will cause severe system slowdowns (known as “thrashing”). Swap is a safety net, not a replacement for buying more RAM.
Swap File vs. Swap Partition
Historically, Linux required a dedicated Swap Partition—a rigid, separate slice of your hard drive. Modifying it required dangerous partition resizing.
Modern Linux distributions (like Ubuntu and its derivatives) now default to using a Swap File. This is simply a dedicated file sitting on your root file system (usually at /swapfile). It behaves identically to a partition but is infinitely easier and safer to resize. The guide below focuses on the swap file method.
How to Check Your Current Swap
Before modifying anything, check what you currently have. Open your terminal and type:
Bash
sudo swapon --show
This outputs the name, type, size, and usage of your swap space.
You can also use the free command to see an overview of your total memory and swap:
Bash
free -h
How to Modify Linux Swap Size (Swap File Method)
If you need to increase or decrease your swap size, you don’t actually “resize” the existing file. Instead, you disable the current swap, delete it, create a new one of the desired size, and turn it back on.
Important: Ensure you have enough free disk space on your drive before increasing your swap size.
Step 1: Turn off the existing swap
First, tell the system to stop using the current swap file. This moves any data currently in swap back into your RAM.
Bash
sudo swapoff -a
Step 2: Delete or Resize the file
(Let’s assume you want to create a new 4GB swap file).
You can use the fallocate command to instantly create a file of the size you want.
Bash
sudo fallocate -l 4G /swapfile
(If fallocate fails or isn’t supported by your file system, you can use sudo dd if=/dev/zero of=/swapfile bs=1M count=4096 instead).
Step 3: Set the correct permissions
For security reasons, only the root user should be able to read and write to the swap file.
Bash
sudo chmod 600 /swapfile
Step 4: Format the file as swap space
Now, tell Linux to format this raw file into usable swap space.
Bash
sudo mkswap /swapfile
Step 5: Enable the new swap
Turn the swap back on.
Bash
sudo swapon /swapfile
Step 6: Verify
Run free -h or sudo swapon --show again to confirm your system is now using the new 4GB swap file.
Step 7: Make it permanent (fstab)
If you just created a brand new swap file (rather than replacing an existing one with the same name), you need to ensure it mounts automatically when you reboot.
Open your file systems table:
Bash
sudo nano /etc/fstab
Ensure this line exists at the bottom of the file:
Plaintext
/swapfile swap swap defaults 0 0
Save and exit (in Nano, press Ctrl+O, Enter, then Ctrl+X).
Bonus: Tuning “Swappiness”
Swappiness is a Linux kernel parameter (ranging from 0 to 100) that dictates how aggressively the system moves data out of RAM and into swap.
- 0: The kernel avoids swapping completely unless absolutely necessary (OOM situations).
- 100: The kernel aggressively swaps data to disk as much as possible.
- 60: The default value on most Linux distributions.
If you have plenty of RAM (e.g., 16GB or 32GB) and you notice your system is writing to the disk unnecessarily, you might want to lower the swappiness value to 10 to keep your system snappy.
To check your current swappiness:
Bash
cat /proc/sys/vm/swappiness
To change it temporarily (until reboot):
Bash
sudo sysctl vm.swappiness=10
To change it permanently, append vm.swappiness=10 to the bottom of your /etc/sysctl.conf file.