Swap space is the part of hard disk used by operating system as memory. It is basically used to swap files & data in and out of memory (RAM) so that they can be loaded as programs. Whenever your operating system runs low on memory, it looks at the swap space to store files that can be loaded later when some memory becomes free. Sometimes even this swap space become insufficient. In such cases, you may have to increase or recreate swap space. In this article, we will learn how to create swap space in Ubuntu/Debian.
How to Create Swap Space in Ubuntu/Debian
Here are the steps to create swap space in Ubuntu/Debian.
1. Check if Swap Memory is Enabled
First of all, check if swap memory is enabled on your system. For this, open terminal and run the following command.
$ sudo swapon -s $ free -m
2. Create Swap Space
Next, we will create a file that is used as swap space on your disk. Before you do that, check that there is enough free space on your disk for this purpose. Basically, you will need at least the space of your RAM memory for this purpose. For example, if you have 2GB RAM, you need a swap file of 4GB.
$ sudo fallocate -l 4G /swapfile $ chmod 600 /swapfile
3. Convert Swap File to Swap Space
At this point, the above new file is just a file with allocated space but it has not been set as swap space. You can do this with the following command.
$ sudo mkswap /swapfile
4. Activate Swap Space
Run the following command to swap space in your system.
$ sudo swapon /swapfile
5. Verify Swap Space
Verify the Swap Space with the following commands.
$ sudo swapon /swapfile $ free -m
6. Make Swap Permanent
Please note, in this case, the swap space will continue to exist only as long as system reboot. In other words, this is a temporary setting that will be reset on system reboot.
In order to make this permanent, open fstab folder in a text editor.
$ sudo vi /etc/fstab
Add the following entry to it.
/swapfile none swap sw 0 0
Save and close the file to apply changes.
7. Configure Swap space
In this optional step, you can configure how often you want the operating system to utilize the swap space. For this purpose, open /etc/sysctl.conf file in a text editor.
$ sudo vim /etc/sysctl.conf
Add the following lines to it.
vm.swappiness=10
Save and close the file. Run the following command to reload sysctl configuration.
$ sudo sysctl -p
In this article, we have learnt how to create Swap space in Ubuntu/Debian.
Also read:
How to Fix “mv: Argument List too long”
How to Repair MySQL Databases & Tables
How to Optimize MySQL Tables
cURL Command to Call REST APIs
How to Remove WWW from URL in Apache
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.