Linux adds Swap partition to solve memory shortage problem

preparation
Firstly, check if your system already has a Swap partition:
swapon -s
or
free -m
If no result is returned or the Swap column in free-m has a value of 0, it means that your system does not have a Swap partition.
Create SWAP partition
We can use the 'fall' command to create a 1GB Swap partition:
fallocate -l 1G /swapfile
If this command cannot be used, please install the utility Linux package:
apt install util-linux
Then set the permissions for this file:
chmod 600 /swapfile
Then activate the SWAP partition
mkswap /swapfile
swapon /swapfile
At this point, you can use the swap - s or free - m command to check if the Swap partition has been activated.
Set up auto start upon startup
We need to edit the file/etc/fstab and add the following content:
echo "/swapfile swap swap defaults 0 0" >> /etc/fstab
Great job done, use the free - m command to check if the Swap partition is correct:
Adjust the Swappiness value of the system kernel
Swapiness is a property of the Linux kernel that defines the frequency at which the system uses swap space. The value of Swapiness ranges from 0 to 100 (default is 60), with a low value causing the kernel to avoid swapping as much as possible, and a high value causing the kernel to use swap space more actively.
This value defaults to 60, and we can use the cat/doc/sys/vm/swappiness command to check the current value.
Usually we can change it to 10:
echo "vm.swappiness=10" >> /etc/sysctl.conf
Then use the sysctl - p command to make it effective.
Close Swap
Sometimes we need to close the Swap partition, you can use the following command:
Firstly, disable the Swap partition:
swapoff -v /swapfile
Then check/etc/fstab and delete the/swap file swap swap defaults 0 0 line.
Finally, delete the file/swap file:
rm /swapfile

  • linux, wsap, linuxswap, 添加Swap, 内存不够用
  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

How to change the root user password of Linux operating system

Changing the root user password of the Linux operating system is a critical operation, please...

Can websites not use SSL certificates? There may be issues with websites not installing SSL certificates

Website security certificate, also known as website SSL certificate. Nowadays, we see many...

Several of the most common encryption and decryption methods

Encryption and decryption are the most commonly used data conversion methods in communication....

Method for modifying server hostname in Linux system

How to modify the hostname in LinuxHow to modify the Linux hostname? You can temporarily modify...

Call WordPress to customize the content of article categories

If we have a custom WordPress article classification, how can we operate it separately if...