In fact, there are many ways to add, here is only the official help inside a method
Check swap space
First, check for any swap files that exist in the system.
Swapon-s
If the return information is the above diagram, then the Swap file does not exist.
Check File system
Before you set up the swap file, check to see if there are enough hard drives
Df-hal
Create and enable the swap file
Use the DD command to create a Swap file
DD If=/dev/zero of=/swapfile bs=1024 count=512k
Format and activate the swap file
Format the Swap partition
Mkswap/swapfile
Do-swap-4
Activate swap partition
Swapon/swapfile
After completing the steps above, execute
Swapon-s
Do-swap-5
After execution, it will appear as below/above
Filename Type Size Used Priority
/swapfile file 524284 0-1
adding system Startup Items
Adding a system boot key is to automatically mount Swap when the machine restarts, so we need to modify the Fstab configuration
Vi/etc/fstab
Add in the last line
/swapfile Swap Defaults 0 0
After you add a startup entry, give the Swap file the appropriate permissions
Chown Root:root/swapfile
chmod 0600/swapfile
Configure Swappiness
In CentOS, the default value for Swappiness is 60.
Cat/proc/sys/vm/swappiness
Do-swap-6
In a Linux system, the use of swap, as well as the proportions used, can be determined by the swappiness parameter. This value is between 0 and 100, and if it is 0, swap is not used, and if it is 100, then the system will be very active in using swap rather than memory. (Remember a long time ago, introduced in detail: swappiness, but do not know in that blog, because the blog changed n times)
Adjust the value of swappiness to a suitable parameter, so as to optimize the use of Swap. The recommended setting is 10.
Using the SYSCTL command
Sysctl vm.swappiness=10
Do-swap-7
Now that we have modified only temporarily, reboot the system will restore the default 60, so we also need to modify: sysctl.conf
Vi/etc/sysctl.conf
Add on the last line
Do-swap-8
# Search for the vm.swappiness setting. Uncomment and change it as necessary.
vm.swappiness=10
Here we have set up the swap partition, even though the swap partition will still be loaded after rebooting the system.