This article is all about swap memory in Linux – swap partition and swap file, what are they, and how to activate and deactivate them.
Table of Contents
- What Is Swap Memory in Linux?
- Why Do We Create Swap Memory in Linux?
- Swap Partition vs Swap File
- How to Check Swap Space in Linux?
- How to Create Swap Partition in Linux?
- How to Create Swap File in Linux?
- How to Delete Swap File?
- That’s All
What Is Swap Memory in Linux?
Swap Memory is also known as Swap Space. It is a special memory on a hard disk (or SSD). Whenever your computer starts any new application, it uses a part of physical memory called Random Access Memory (RAM) for caching and other purposes. But the RAM has very limited space compared to hard disks. So, the computer frequently tries to free up the RAM’s space by “swapping” it for the Swap Memory’s space. That means the computer will use some swap space for the application’s caching, etc.
The sum of Swap Memory and Physical Memory is called Virtual Memory.
Virtual Memory = Swap Memory + Physical Memory.
Why Do We Create Swap Memory in Linux?
- As said earlier, it frees up the the physical RAM. This is very useful when your computer runs out of RAM. Imagine a scenario where you are doing some research: you have opened lots of tabs in your browser, lots of Virtual Box Machines, and lots of other applications. And if your system runs of RAM, it will freeze and you will not have any other option except shutting down the computer using the power button. You will loose all your taks. But if your have enabled the Swap Memory, your computer will not freeze completely although it becomes slow. The speed at this moment will depend upon the speed of your hard disk. And if you are using SSDs instead of the hard disk, that will be even better.
- Swap Memory is also used for Hibernation. The hibernation is little different from the Sleep (also called Suspend in the Linux world). In Sleep, the RAM still has electricity flowing in it but in the hibernation, RAM is turned off completely and all the contents of it are saved in the Swap Memory of your hard disk.
After describing the meaning and applications of swap memory, I will be talking about the types of swap memory – swap file and swap partition by elucidating the differences between them.
Swap Partition vs Swap File
There is no performance difference between the two. But still, there are some differences:
Swap Partition | Swap File |
---|---|
The swap partition is like any other partition on the hard disk. | The swap file is like any other file on an existing partition. |
The swap partition is used solely for one purpose – swap memory | The partition on which the swap file resides can be used for other purposes as well. |
Personally, I prefer swap files because I can change their size anytime but in the swap partitions, I will need to delete/extend/shrink partitions on the hard disk and that might lead to data loss.
How to Check Swap Space in Linux?
To check the swap space in Linux, you can use the memory command free:
~$ free --human
Output:
total used free shared buff/cache available
Mem: 6.7Gi 2.0Gi 484Mi 31Mi 4.2Gi 4.3Gi
Swap: 14Gi 137Mi 14Gi
In the above command, the flag --human
is used to print the output in human-readable form i.e. use GiB, MiB, etc. instead of bytes.
Another command to check the swap space in Linux is the swapon command:
~$ swapon --show
Output:
NAME TYPE SIZE USED PRIO
/swapfile file 14.6G 206M -2
The above talk was all about the introduction and application of swap memory. In the next few headings, I will talk about how to create one. You can create it during or after the installation of your Linux distribution.
Recommended Reading: How to Install Arch Linux?
How to Create Swap Partition in Linux?
The following steps assume that your system is not using a special type of file system called btrfs. To check your file system types, use df:
~$ df --output=source,fstype
Filesystem Type
dev devtmpfs
run tmpfs
/dev/nvme0n1p5 ext4
tmpfs tmpfs
/dev/nvme1n1p1 ext4
tmpfs tmpfs
tmpfs tmpfs
1st Step: Partition your Hard Disk:
Note: Before your begin, please backup all the partitions on the hard disk and unmount them. And, if your hard disk also has your root partition, you will not be able to unmount this root partition. In that case, boot into Live CD/USB of any Linux distribution and partition from there.
To partition your disk, you can use one of the fdisk, parted, or cfdisk, or other tools. I will recommend the cfdisk because it is interactive, user-friendly, and hence easier.
And don’t forget to set the Partition Type as “Linux swap”.
2nd Step: Make the partition into a swap partition using mkswap:
# mkswap /dev/swap_partition
3rd Step: Enable it using swapon:
# swapon /dev/swap_partition
4th Step: Create a fstab entry to automatically mount the swap partition:
# nano /etc/fstab
The fstab file /etc/fstab enables your computer to mount the partition automatically during boot.
Now, add the following line into the file at the end:
UUID=filesystem_UUID none swap defaults 0 0
Here, in the above line, you need to replace filesystem_UUID with your own UUID. This UUID is an ID and it is unique and hence, your computer will use it to identify the partition. To find UUID, use ls -l /dev/disk/by-uuid/
~$ ls -l /dev/disk/by-uuid/
total 0
lrwxrwxrwx 1 root root 15 Dec 30 00:43 32BA-F9AF -> ../../nvme0n1p1
lrwxrwxrwx 1 root root 15 Dec 30 00:43 3EA6BDEFA6BDA833 -> ../../nvme0n1p4
lrwxrwxrwx 1 root root 10 Dec 31 20:47 9674C20374C1E5D9 -> ../../sda1
lrwxrwxrwx 1 root root 15 Dec 31 20:35 bd4da364-6f99-4859-8ca4-326c89e9b11f -> ../../nvme1n1p1
lrwxrwxrwx 1 root root 15 Dec 30 00:43 d2d9da75-f104-46c8-9ff9-8193f083f2ff -> ../../nvme0n1p5
So, a good example of the fstab entry would be:
UUID=9674C20374C1E5D9 none swap defaults 0 0
How to Create Swap File in Linux?
1: Create a virtual partition
Virtual Partitions are actually a file. To create it you can use dd:
# dd if=/dev/zero of=SWAP-LOCATION bs=1M count=COUNT-SIZE status=progress
Where SWAP-LOCATION is the location of the swap file and COUNT-SIZE determines the size of the file.
Size of the file = bs x COUNT-SIZE
So, if bs=1M and COUNT-SIZE=15000, the swap file will be of 15000M i.e. 15GB approximately.
Just like swap partition, a recommended size will be at least 1.5 times your RAM.
A good example of the above command would be:
# dd if=/dev/zero of=/swapfile bs=1M count=15000 status=progress
2: Modify the read and write permissions of the file using the chmod command
# chmod 600 /swapfile
Now only the owner user (i.e. the root) will have read and write access to the file.
3: Just like Swap Partition, make this file into a swap file using mkswap:
# mkswap /swapfile
4: Enable the file using swapon:
# swapon /swapfile
5: Create a fstab entry to automatically mount the swap file:
~$ nano /etc/fstab
/swapfile none swap defaults 0 0
How to Delete Swap File?
1. Deactivate it using swapoff:
# swapoff /swapfile
2. Remove the fstab line /swapfile none swap defaults 0 0
from /etc/fstab
# nano /etc/fstab
3. Finally, remove the swapfile:
# rm /swapfile
That’s All
That’s all folks. If there are some mistakes in the article or you would like to suggest something, feel free to point them out in the comment section below.
Ƭhere is certainly a great deal to fіnd oᥙt about this topіc.
I really like аll the points you made.