Create Swap Space in Linux

How to Create Swap Space in Linux

The operating system moves programs to the main RAM to run them. Modern operating systems can support the simultaneous execution of several background services and programs, but due to the limitation of the system’s main memory, users usually face problems when running several programs. This disorder happens because the memory required to run programs simultaneously is more than the total main memory of the system. In this case, the system faces a lack of main memory and eventually crashes.

To solve this problem, either the main memory of the system should be upgraded, which is expensive, or a part of the disk space should be specified in a virtual way to be used as the main memory. Swap space in the Linux operating system plays the role of this space.

Swap space is one of the main Linux partitions, which is a part of the system’s hard disk that can be introduced to the Linux kernel as a partition or file. Linux kernel uses swap space as a supplement to RAM. When the Linux kernel needs more RAM, it can increase the free space of the RAM by transferring the less used parts in the RAM to the Swap memory, and if needed, use the parts copied in the Swap space.

As we described, it is important to create Swap space in Linux for optimal memory management. You can define the swap space as a dedicated partition or file, which is usually defined as a swap file when starting the Linux operating system.

Maybe you have a question, how can you create Swap virtual space in Linux? In this article, we will answer your question and teach you how to create Swap space in Linux.

Steps to create a Linux swap file

By following the steps of this article, you can optimally manage the main storage memory of your server after purchasing a Linux VPS by creating Swap virtual memory. You can also add any amount of memory you need to the server (1G or 2G or so on).

Step 1: Displaying RAM and Swap space consumption

The first step is to view the current swap space if any, and RAM usage. For this purpose, use the following commands:

free -h

swapon --summary

It should be noted that each of the following commands can be used to display the used space of RAM and Swap:

swapon --summary

free -h

top

htop

vmstat

Step 2: Creating a new file for the Swap space

In this step, create a file with 1G space (this value can be different, we have considered 1G in this tutorial) to be used as Swap space. You can use two commands to create a swap file:

  • Creating a swap file using thefallocatecommand:
sudo fallocate –l 1G /swapfile

Running the fallocate command will create a swap file when your operating system supports this command, but if you get the fallocate failed: Operation not supported “ error message by running the above command, you have to run the next method, which is to use theddcommand.

  • Creating a swap file using theddcommand:
sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576

Step 3: Setting Linux file system permissions

To improve security, you must set permissions for the Swap file through the following command, and only the root user should have permission to read and write the Swap file.

sudo chmod -v 0600 /swap-file

Note: You can also use the following command to set the Swap file permissions:

sudo chown -v root:root /swap-file

Also, the following command is executed to access a list of permissions:

ls -l /swap-file

Step 4: Setting up the Swap area

To set the swap area, you must run the following command:

sudo mkswap /swapfile

Step 5: enabling Swap space

Running theswaponcommand enables the Swap space:

sudo swapon /swapfile

Step 6: Enabling Swap File in Linux Permanently

To ensure that the changes(enabling the Swap file) are applied and saved after restarting the Linux operating system, Open the /etc/fstab file for editing:

sudo vi /etc/fstab

Then enter the following lines in the /etc/fstab file:

/etc/fstab

/swapfile swap swap defaults 0 0

Save and close the file.

Step 7: Checking if Swap is active

By executingswaponorfreecommands, you will make sure that Swap is active:

sudo swapon --show

Output

NAME      TYPE  SIZE   USED PRIO

/swapfile file 1024M 507.4M   -1
sudo free -h

Output

total        used        free      shared  buff/cache   available

Mem:           488M        158M         83M        2.3M        246M        217M

Swap:          1.0G        506M        517M

 

Note: In the early steps of the article, we mentioned that other commands are also executed to check whether the Swap File is active.

Adjusting the value of swappiness

The swappiness feature determines the period and how the Linux kernel uses the swap space. That is, it determines how often and to what extent the system can use Swap virtual memory. The swappiness property allows setting values between 0 and 100 which is set to 60 by default.

The /proc/sys/vm/swappiness file contains swappiness values. To see the default value of swappiness, run the following command:

cat /proc/sys/vm/swappiness

Output

60

The default value of this feature is ideal for all Linux users, but you can change it if you need. For this, consider the following example as a model.

If you want to change the default value of swappiness to 30, run thesysctlcommand:

sudo sysctl vm.swappiness=30

Then, to set this value instead of the default value of swappiness, you need to edit the /etc/sysctl.conf file and add the following line to the file:

vm.swappiness=30

We mentioned earlier that the default value of the swappiness feature is suitable for all users, but sometimes you need to change the default value, the value you want to replace should be appropriate to your system’s workload and memory usage.

Setting how to use Swap and file priority

By default, the Linux kernel uses the first active Swap space, and after using the first Swap space completely, it goes to the second Swap space and continues in the same way. Therefore, you can prioritize to use the Swap space. To prioritize the use of Swap space, you must open the /etc/fstab file and add the desired priority to the file according to our example:

/dev/sdb none swap sw  0 0

/swap-file swap swap pri=5 0 0

/swap-file-2 swap swap pri=10 0 0

Note: – 2 is the default priority that sets the priority in using Swap space.

In the example that we mentioned, the priority is to use /swap-file-2 with pri=10, and after its space is filled, the Linux kernel uses /swap-file with pri=5 and finally goes to /dev/ sdb.

Removing Swap file

Up to this part of the article, you have successfully added the swap file to the Linux system and learned the Practical settings related to the swap space. The last part of this article provided for solving your concern about how to remove the swap file. You may not need the swap file in some situations and you may don’t know how to remove the swap file. Don’t worry, we will guide you in disabling the swap file in this section. Follow the steps below:

  • The first step is to disable Swap by running the following command:
sudo swapoff -v /swapfile
  • In the /etc/fstab file, delete the/swapfile swap swap defaults 0 0entry.

Remove the swapfile by running the following command:

sudo rm /swapfile

FAQ

Different commands can be used to check the swap space size:

  • swapon -s
  • htop
  • To access RAM usage and Swap space you can run thefree -mcommand.
  • To view the swap space, one of the other methods is to refer to the /proc/swaps file.

Yes, for this, after ensuring enough RAM space, you must cycle off the swap so that the data is transferred to RAM again.

Conclusion

In this tutorial, you have successfully configured and created the Swap file in Linux to optimize Linux system memory management and increase available swap space. We hope that you can use your system memory optimally through our training.

You can also usemancommands to get more information about swap file settings:

man 8 mkswap

man 8 swapon

man 8 swapoff

man 8 mount

man 5 fstab

Thank you for choosing our article to read.

Leave a Reply

Your email address will not be published. Required fields are marked.