How does CentOS manually add/remove virtual memory SWAP swap files & partitions?

CentOSHow to manually add/remove virtual memory SWAP swap files & partitions?

What is the swap partition? SWAP is the swap area, and the role of the SWAP space is whenLinuxWhen the physical memory of the system is insufficient, part of the physical memory will be released to supplement the insufficient physical memory, so that the currently runningsoftwareprogram use.

Benefits of using Swap for swap partitions

The adjustment of SWAP optimization settings is very important for the performance application of the Web server. If the physical memory is insufficient, the virtual memory SWAP partition settings can be used to effectively save the cost of LINUX system upgrades.

What should be the size of the swap partition?

The size of the SWAP swap partition is determined according to the size of the actual system memory and the software used.

The suggestions for CentOS and RHEL6 are as follows. Please make appropriate optimization adjustments according to the specific situation:

  • 4GB of RAM requires a minimum of 2GB of swap space
  • 4GB to 16GB RAM requires a minimum of 4GB of swap space
  • 16GB to 64GB of RAM requires a minimum of 8GB of swap space
  • 64GB to 256GB of RAM requires a minimum of 16GB of swap space

View the current memory and swap space size (default unit is k, -m unit is M):
free -m

The displayed results are as follows (example):
total used free shared buffers cached
Mem: 498 347 151 0 101 137
-/+ buffers/cache: 108 390
Swap: 0 0 0

If Swap is 0, it means no, and you need to manually add the SWAP swap partition.

(Note: VPS with OPENVZ architecture does not support manually adding a SWAP swap partition)

There are 2 types of adding SWAP swap space:

  • 1. Add a SWAP swap partition.
  • 2. Add a SWAP swap file.

It is recommended to add a SWAP swap partition; if there is not much free space left, add a swap file.

View SWAP information (including SWAP swap file and partition details):

swapon -s
Or
cat /proc/swaps

(If there is no SWAP value displayed, it means that the SWAP space has not been added)

Here is an example of how to create a SWAP file:

1. Create a 1GB swap

dd if=/dev/zero of=/home/swap bs=1k count=1024k
mkswap /swapfile
swapon /swapfile
echo "/home/swap swap swap default 0 0" | sudo tee -a /etc/fstab
sudo sysctl -w vm.swappiness=10
echo vm.swappiness = 10 | sudo tee -a /etc/sysctl.conf

2. Create a 2GB swap

dd if=/dev/zero of=/home/swap bs=1k count=2048k
mkswap /home/swap
swapon /home/swap
echo "/home/swap swap swap default 0 0" | sudo tee -a /etc/fstab
sudo sysctl -w vm.swappiness=10
echo vm.swappiness = 10 | sudo tee -a /etc/sysctl.conf

(Finish)

The following are additional detailed references:

1. Use the dd command to create a swap file

1G memory
dd if=/dev/zero of=/home/swap bs=1024 count=1024000

2G memory:
dd if=/dev/zero of=/home/swap bs=1k count=2048k

In this way, a /home/swap file is created, the size of 1024000 is 1G, and the size of 2048k is 2G.

2. Make a file in swap format:
mkswap /home/swap

3. Use the swapon command to mount the file partition to the swap partition
/sbin/swapon /home/swap

Let's take a look with the free -m command and find that there is already a swap file.
free -m

But after restarting the system, the swap file becomes 0 again.

4. In order to prevent the swap file from becoming 0 after restart, modify the /etc/fstab file

At the end (last line) of the /etc/fstab file add:
/home/swap swap swap default 0 0

(So ​​even if the system is restarted, the swap file is still valuable)

Or directly use the following command to add the restart automatic mount configuration command:
echo "/home/swap swap swap default 0 0
" | sudo tee -a /etc/fstab

Under what circumstances does the VPS use the SWAP exchange space?

It is not after all the physical memory is consumed before using the SWAP swap space, but it is determined by the parameter value of swappiness.

[root@~]# cat /proc/sys/vm/swappiness
60
(The default value of this value is 60)

  • swappiness=0 means the maximum use of physical memory, and then the space for SWAP exchange.
  • swappiness=100 indicates that the swap space is actively used, and the data in the memory is transferred to the swap space in time.

How to set the swappiness parameter?

Temporary modification:

[root@~]# sysctl vm.swappiness=10
vm.swappiness = 10
[root@~]# cat /proc/sys/vm/swappiness
10
(This temporary modification has taken effect, but if the system is restarted, it will return to the default value of 60)

Permanent modification:

Add the following parameters to the /etc/sysctl.conf file:
vm.swappiness=10

(Save, it will take effect after restart)

or enter the command directly:
echo vm.swappiness = 10 | sudo tee -a /etc/sysctl.conf

Delete the SWAP swap file

1. Stop the swap partition first

/sbin/swapoff /home/swap

2. Delete the swap partition file

rm -rf /home/swap

3. Delete the automatic mount configuration command

vi /etc/fstab

Remove this line:

/home/swap swap swap default 0 0

(This will delete the manually added swap file)

Things to note

  • 1. Only the root user can be used to add or delete swap operations.
  • 2. It seems that the swap partition allocated when installing the VPS system cannot be deleted.
  • 3. The swap partition is generally twice the size of the memory.

Hope Chen Weiliang Blog ( https://www.chenweiliang.com/ ) shared "CentOS how to manually add/delete virtual memory SWAP swap files & partitions? , to help you.

Welcome to share the link of this article:https://www.chenweiliang.com/cwl-158.html

Welcome to the Telegram channel of Chen Weiliang's blog to get the latest updates!

🔔 Be the first to get the valuable "ChatGPT Content Marketing AI Tool Usage Guide" in the channel top directory! 🌟
📚 This guide contains huge value, 🌟This is a rare opportunity, don’t miss it! ⏰⌛💨
Share and like if you like!
Your sharing and likes are our continuous motivation!

 

Comment

Your email address will not be published. Required fields * Callout

scroll to top