How to increase swap space on CentOS 8

Swap (swap memory) is a space on the hard disk. When the physical memory is exhausted, the swap memory will be used. When a Linux system is running low on memory, inactive memory pages will be moved from RAM space to Swap memory swap space.

Swap space may exist in the form of an independent memory swap partition or a swap file. Usually, running CentOS on a virtual machine does not have an existing memory swap partition, so the only option is to create a swap file.

This article will describe several steps to add a swap file to CentOS 8 system.

1. Create and activate the Swap file##

Perform the following steps as root or another user with sudo privileges to add swap space on CentOS 8 system.

  1. Start creating files for memory swap space:
sudo fallocate -l 1G /swapfile

In this example, we created a swap file with a size of 1G. If you need more memory swap space, replace 1G with the size you want.

If fallocate is not available on your system, or you get an error message: fallocate failed: Operation not supported, use the dd command to create a swap file.

sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576
  1. Set file permissions so that only root users can read and write swap files:
sudo chmod 600/swapfile
  1. Next, set up Linux swap on this file:
sudo mkswap /swapfile
Setting up swapspace version 1, size =1024MiB(1073737728 bytes)
no label, UUID=0abdb8ba-57d6-4435-8fd8-5db9fc705045
  1. Execute the following command to activate the swap space:
sudo swapon /swapfile
  1. Use the swapon or free command to verify whether the swap space has been activated, like the following:
sudo swapon --show
NAME      TYPE  SIZE   USED PRIO
/swapfile file 1024M 507.4M   -1
sudo free -h
    total        used        free      shared  buff/cache   available
Mem:           488M        158M         83M        2.3M        246M        217M
Swap:1.0G        506M        517M
  1. These changes are persisted by adding a swap entry in the /etc/fstab file.

sudo nano /etc/fstab

Paste the following content into the back of the file:

/swapfile swap swap defaults 00

Two, adjust the swappiness value##

Swappiness is a Linux Kernel attribute value, which defines how often the system uses swap space. The value of Swappiness ranges from 0 to 100. A lower value makes the kernel avoid using swap memory as much as possible, while a higher value makes the kernel use swap memory as much as possible.

The default value of swappiness on CentOS 8 is 30. You can check the current swappiness value by typing the following command:

cat /proc/sys/vm/swappiness
30

When the swappiness value is 30, it is suitable for desktop and development machines. For production servers, you may need to lower this value. For example, to lower the swappiness value to 10, enter:

sudo sysctl vm.swappiness=10

To persist this parameter, you should paste the following content into the /etc/sysctl.conf file, and restart:

vm.swappiness=10

The optimal swappiness value depends on the workload of your system and how your memory is used. You should increase the value of this parameter little by little to find the optimal value.

3. Remove a Swap file##

To deactivate and remove the swap file, follow these steps:

  1. Deactivate the swap space by entering the following command:
sudo swapoff -v /swapfile
  1. Remove the swap entry /swapfile swap swap defaults 0 0 from the /etc/fstab file.

  2. Use rm to delete the actual swap file:

sudo rm /swapfile

Four, summary##

We have demonstrated to you how to create a swap file on CentOS 8, and activate and configure the swap space.

Recommended Posts

How to increase swap space on CentOS 8
How to add swap on CentOS 7
How to add swap space on Ubuntu 20.04
Configure swap space on CentOS7
How to install jdk1.8 on centOS7
How to install MySQL on CentOS 8
How to install Memcached on CentOS 8
How to install R on CentOS 8
How to install FFmpeg on CentOS 8
How to install Virtualbox on CentOS 8
How to install TensorFlow on CentOS 8
How to Update to gcc4.9.x on Centos7
How to install TeamViewer on CentOS 8
How to install Perl 5 on CentOS
How to install Git on CentOS 8
How to install Gradle on CentOS 8
How to install Elasticsearch on CentOS 8
How to install Jenkins on CentOS 8
How to install Java on CentOS 8
How to install Go on CentOS 8
How to install GCC on CentOS 8
How to install Yarn on CentOS 8
How to install Nginx on CentOS 8
How to install Asterisk on CentOS 7
How to install Jenkins on CentOS 8
How to install Vagrant on CentOS 8
How to install Python 3.8 on CentOS 8
How to install Tomcat 9 on CentOS 8
How to install Webmin on CentOS 8
How to install Ruby on CentOS 8
How to install Skype on CentOS 8
How to install htop on CentOS 8
How to install Python on CentOS 8
How to install Elasticsearch on CentOS 8
How to install Postgresql on CentOS 8
How to install Wordpress on Centos
How to install htop on CentOS 8
How to install TeamViewer on CentOS 8
How to install MariaDB on CentOS 8
How to install MongoDB on CentOS 7
How to install Odoo 13 on CentOS 8
How to install Apache on CentOS 8
How to disable SELinux on CentOS 8
How to install OpenCV on CentOS 8
How to install PHP on CentOS 8
How to install MongoDB on CentOS 8
How to install Apache Maven on CentOS 8
How to install Apache Kafka on CentOS 7
[Graphic] How to install tomcat on centos
R&D: How To Install Python 3 on CentOS 7
How to install GCC compiler on CentOS 7
How to modify the hostname on CentOS 8
How to install offline JDK1.8 on centos7.0
How to install and configure Elasticsearch on CentOS 7
How to install Visual Studio Code on CentOS 8
Ways to clear disk space on CentOS 6 or CentOS 7
How to install RPM packages on CentOS Linux
How to set up SSH keys on CentOS 8
How to install and configure VNC on CentOS 8
How to install and use Composer on CentOS 8
How to install and configure Redis on CentOS 8