Swap space is virtual memory allocated on the hard disk that is used to hold inactive memory pages when RAM is depleted. In this article, we learn all about swap space and how to create a swap file in Linux.
Table of contents.
- How much do we need.
- Creating a swap file.
- Removing a swap file.
Other than RAM which stores data concerning active processes, swap space is allocated memory in the hard disk used when RAM is depleted. We can have swap space in three forms, one as a dedicated partition during initial installation, a swap file, or a combination of both. A swap file is used when there is no free disk space for creating a dedicated partition.
Commonly used commands to manage swap space are, free - to check its utilization, fdisk - to check the general disk utilization, htop, and vmstat.
Commands involving swap files are: mkswap - to create a swap space, swapon - to specify where swapping should take place and free - command to confirm these configurations.
A pro of swap space is that it is a form of virtual memory which provides the OS with the illusion that it has more memory and therefore it can handle multiple processes/application data even if the actual memory is not enough. It does this by moving inactive memory pages to an allocated file or partition on the hard disk(Swap space) to free up RAM for incoming applications.
Also, considering the large sizes of disk space/main memory, with swap space we find a use for this unused disk space.
A drawback of swap space is that it is not RAM and hence not as fast since it requires disk access which is very taxing to a processor.
Swap space can also be seen as a wastage of disk space for example we can have 10GBs of swap space where not all the 10GBs will be used.
How much do we need.
Swap space should be twice the physical RAM plus 1X RAM for any RAM above 2GB. It should be also greater than 32MB in size.
That is, if M is RAM and S is swap space in GBS, then, if M < 2
S = M * 2 otherwise, S = M + 2
IF M < 2 S = M * 2 ELSE S = M + 2
Therefore, if we have 2GB of physical RAM, we should have at least 3GB of swap space.
In machines with large amounts of RAM, swap space is not always large and it's OK since RAM can handle application data.
We can also use the following schema:
RAM Swap Swap (with hibernation) 256MB 256MB 512MB 512MB 512MB 1GB 1GB 1GB 2GB 2GB 1GB 3GB 3GB 2GB 5GB 4GB 2GB 6GB 6GB 2GB 8GB 8GB 3GB 11GB 12GB 3GB 15GB 16GB 4GB 20GB 24GB 5GB 29GB 32GB 6GB 38GB 64GB 8GB 72GB 128GB 11GB 139GB
What is swap with hibernation, when a system hibernates(closing the laptop cover), its state is saved in the hard disk before it 'powers down'. When it 'wakes up', the state is restored, that is why the values on the last row are larger.
However, not all systems hibernate. We confirm this by executing the command:
$ which pm-hibernate
If the above command returns output, it means that the system can hibernate.
Creating a swap file.
To create a swap file, we follow the listed steps;
- We first create a swap file. This is where inactive memory pages are sent when RAM is full:
$ sudo fallocate -l [size] /swapfile
In the above command, we specify the size and name of the file. The file in this case is called swapfile and it is located in the root directory. For example, to create 8GB of swap space we write:
$ sudo fallocate -l 8G /swapfile
- We change the permissions of the file such that only the root user can read or write to the file:
$ sudo chmod 600 /swapfile
- The mkswap command will officially make this file the Linux swap area:
$ sudo mkswap /swapfile
- Now to enable the swap file:
$ sudo swapon /swapfile
The steps are shown in the screen shot below:
- Make the changes permanent by editing the /etc/fstab file and adding the following line:
/swapfile swap swap defaults 0 0
After the operation we should reboot the system for changes to take effect.
- Now to verify a successful configuration:
$ sudo swapon --show
We can also use the free command:
$ sudo free -h
We can also use the lsblk command:
A swapiness value determines how often the kernel will use the swap space. It usually ranges from 0 - 100 where the higher the value the more often swap space is utilized and the lower the value the more the kernel relies on physical RAM.
By default the value is 60, we can confirm this by concatenating the /proc/sys/vm/swappiness file:
$ cat /proc/sys/vm/swapiness
On a personal Linux kernel, this value is usually OK, but as mentioned earlier, swap space is space on the hard disk and hard disk access is slow and costly, especially for production servers. Usually, such servers have large amounts of RAM, enough to make use of very small sizes of swap space.
Another reason is thrashing, this is whereby the kernel enters a cycle of swapping memory frequently such that it becomes its main activity and thus the system is unusable.
In such cases we can reduce the swapiness value as follows:
$ sudo sysctl vm.swappiness-[value]
For example, to reduce it to 30, we write:
$ sudo sysctl vm.swappiness=30
To make this permanent we add the following line to the /etc/sysctl.conf file and reboot:
Removing a swap file.
Maybe a partition freed up or we increased the amount of RAM on our system or for any other reason we want to be done with a swap file.
- First we deactivate it using the swapoff command:
$ sudo swapoff -v /swapfile
Then, we remove the line we previously added to the /etc/fstab file.
And finally, since it is just another file, we delete it using the rm command:
$ sudo rm /swapfile
And that's it.
RAM(Random Access Memory) is a limited resource in most cases. This however does not change the memory requirements for various applications such as browsers, code editors, etc. Normally if we start an application and the memory is not enough to hold its data, it crashes. This is solved by virtual memory/swap space. It makes the computer think it has enough memory even if in reality it does not. In such cases, when the memory is full and new applications need to be started, inactive memory pages are stored in swap space. This operation frees up RAM which will hold this data of the newly started applications. This is the reason we can have multiple chrome tabs and an IDE and some other applications open at the same time.
For a comprehensive guide to swap space, swap files, and partitions, this documentation is helpful.