Configure swap space on CentOS7

Foreword###

Swap usually performs better on traditional mechanical hard disks. Using swap on SSDs may cause problems, especially after the hardware is aging. Therefore, for DigitalOcean and other users who use SSD-based cloud hosting services, we do not recommend enabling swap. This will even affect other users who share Host with your virtual machine.

For DigitalOcean users, the best way to improve performance is to update the Droplet. Generally speaking, the performance of the upgraded host will be improved, and it is less susceptible to hardware problems.

Configure Swap

  1. Check the swap information of the system to see if swap has been configured. A system can have multiple swaps, but generally one is enough.
    Execute swapon -s, if the command does not return a result, it means that the system has not been configured with swap.
    Or, we can also use free -m to view the overall memory usage of the system. Here you can see the usage status of memory and swap (display unit is MB):
    Here you can see that the total swap space of our system is 0, that is, swap has not been configured. This is consistent with the results we saw in swapon.
free -m
    total       used       free     shared    buffers     cached
Mem:39533153637811107-/+ buffers/cache:1963756
Swap:004095
  1. Check the available storage space. Usually, we create a separate partition as swap. However, sometimes due to hardware or software limitations, the method of creating a new partition cannot be implemented. In this case, a swap file can be created to achieve the same function.
df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        59G  1.5G   55G   3% /
devtmpfs        2.0G     02.0G   0%/dev
tmpfs           2.0G     02.0G   0%/dev/shm
tmpfs           2.0G  8.3M  2.0G   1%/run
tmpfs           2.0G     02.0G   0%/sys/fs/cgroup
  1. Create Swap file
  1. Swap file enabled
    Now we have the swap file, but the system does not know that it should be used as swap. This requires us to tell the system to format the file as swap and enable it.
    First, we need to change the permissions of the swap file to ensure that only root can read it, otherwise there will be a big security risk. Use the chmod command to perform authority operations: sudo chmod 600 /swapfile
    In this way, the file can only be read and written by root. Use the ls -lh command to check:
ls -lh /swapfile
- rw-------1 root root 4.0G Oct 3011:00/swapfile

Then, use the following command to tell the system to use the file for swap: sudo mkswap /swapfile

Now, this swap file can be used as swap space. Enter the following command to start using the swap: sudo swapon /swapfile

We can enter the following command to confirm whether the setting has taken effect:

swapon -s
Filename                Type        Size    Used    Priority
/swapfile               file        41943000-1

You can see that the returned result already has the swap we just set. Use the free tool to confirm:

free -m
    total       used       free     shared    buffers     cached
Mem:39533153637811107-/+ buffers/cache:1963756
Swap:409504095

At this point, our swap has been set up, and the operating system will use it when needed.

  1. Make the swap file permanent
    So far we have enabled the swap file in the system, but once the system restarts, the server cannot automatically enable the file. To make swap take effect automatically after the system restarts, we can do so by modifying the fstab file (this is a table for managing file systems and partitions).
    Open the file with sudo permission to edit: sudo vim /etc/fstab
    Add the following line at the end of the file to tell the operating system to automatically use the swap file just created:
    /swapfile swap swap sw 0 0
    After adding, save and exit. After the server restarts, it will check the file and automatically enable swap.

Change Swap configuration (optional)

There are several options involving swap that may affect the performance of the system. In most cases these options are optional

The swappiness parameter determines the frequency at which the system exchanges data from memory to swap space. The value is set between 0 and 100, which represents the strength of the system to exchange data from memory to swap space.

The closer the value is to 0, the more the system tends not to perform swap and only perform swap operations when necessary. Since swap is much slower than memory, reducing reliance on swap means higher system performance.

The closer the value is to 100, the more the system tends to swap. The memory usage habits of some applications are more suitable for this situation, which is also related to the purpose of the server.

Enter the following command to view the current swappiness value: cat /proc/sys/vm/swappiness #30

CentOS 7 defaults to a swappiness of 30, which is a moderate value for most desktop systems and local servers. For VPS systems, a value that may be close to 0 is more appropriate.

Use the sysctl command to modify the swappiness. For example, set swappiness to 10: sudo sysctl vm.swappiness=10

This modification will take effect until the next restart. If you want to permanently modify the value, you need to edit the sysctl configuration file:

sudo vim /etc/sysctl.conf
Paste the following at the end of the file:
vm.swappiness =10

After editing, save and exit, then swappiness will be set to this value every time the server restarts.

Another configuration item that can be considered for change is vfs_cache_pressure, which involves the storage of special file system metafile entries. Frequent reading of this type of information is very performance-consuming, so extending its storage time in the cache can improve system performance.

View the current set value of cache pressure through the proc file system: cat /proc/sys/vm/vfs_cache_pressure #100

This value is relatively high, which means that the system removes inode information from the cache faster. A conservative value is 50, use the sysctl command to set:

sudo sysctl vm.vfs_cache_pressure=50

This command is only valid before restarting. To make this setting effective permanently, you need to edit the sysctl configuration file:

sudo vim /etc/sysctl.conf
Add the following at the end of the file:
vm.vfs_cache_pressure =50
Save and exit, the server will automatically set the cache pressure to 50 after each restart.

to sum up###

At this point, our system memory has gained some breathing space. With swap space, some common problems can be effectively avoided.

If you still encounter out of memory (OOM, out of memory) error messages, or your system cannot run the applications you need, then the best way is to optimize your application configuration or upgrade your server.

Welcome to subscribe to "Uncle K Blockchain"-Focus on blockchain technology learning

Blog address: http://www.jouypub.com

Short Book Homepage: https://www.jianshu.com/u/756c9c8ae984

segmentfault homepage: https://segmentfault.com/blog/jouypub

Tencent Cloud Homepage: https://cloud.tencent.com/developer/column/72548

Recommended Posts

Configure swap space on CentOS7
Configure Ocserv on CentOS 6
Configure rsyslog log client on CentOS
Configure python3 environment on centos7 and
How to add swap on CentOS 7
Configure Nginx reverse proxy based on CentOS 7
How to add swap space on Ubuntu 20.04
Configure Nginx load balancing based on CentOS 7
Configure Nginx forward proxy based on CentOS 7
Centos7 configure JDK
How to install and configure Elasticsearch on CentOS 7
Ways to clear disk space on CentOS 6 or CentOS 7
How to install and configure VNC on CentOS 8
Configure Nginx to start automatically based on CentOS 7
How to install and configure Redis on CentOS 8
How to install and configure phpMyAdmin on CentOS 6
How to install and configure Owncloud on CentOS 8
How to install and configure Redmine on CentOS 8
Install Docker on Centos7
install LNMP on centos7.4
Build k8s1.9.9 on centos7
Install Java on Centos 7
Xfs configuration on centos7
Nodejs install on centos7
Install FFmpeg on CentOS 8
Install RabbitMQ on CentOS 7
Install Node.js on Centos
Maven install on centos7
Install MongoDB on CentOS 7
Install Surelog on CentOS8
Configure lamp under centos6.8
Centos7 configure IP address
Deploy vuepress on centos7
Openjdk install on centos7
Install Jenkins on centos7
Use RapidSVN on CentOS7
Centos7 configure nodejs environment
install RabbitMQ on centos
Install RabbitMQ on CentOS 7
install Docker on centos6.5
install oracle on centos
Configure CentOS7 GPU environment
Install Elasticsearch 6 on centos7
Install RabbitMQ on CentOS7
Deploy Jenkin on centos7
How to configure FTP server with Vsftpd on CentOS 8
How to install and configure NFS server on CentOS 8
How to configure FTP server with Vsftpd on CentOS 8
Install mysql online on centos
Install ElasticSearch 7.x on CentOS 7
Glusterfs cluster installation on Centos7
Build MariaDB replication on CentOS
How to install and configure Postfix mail server on CentOS8
Configure tomcat on ubuntu server
Configure static IP under CentOS 7
Install MySQL 8.0.16 on Linux Centos
Deployment of graphite on centos7
Install docker transfer on Centos7
Centos configure multiple virtual IP
Install docker on Centos system
install EPEL repo on centos