How do I add swap in CentOS? CentOS to add swap partitions

Source: Internet
Author: User
Tags sleep centos

The use of Digitalocean for a long time, a few days ago in the compilation of PHP when the process was killed situation, after my spit and communicate with others found that the reason for the memory exhaustion. In fact, because the MySQL process at that time consumed a lot of memory, and then feel the need to manually add swap (swap partition), so as not to compile any later when the process is K.
On Linux swap (swap partition), similar to the virtual memory of Windows, when there is not enough memory, a part of the hard disk space virtual memory use, thus resolving the memory capacity of the situation.
So how do you add Swap manually in CentOS? Www.111cn.net

1. Check Swap space
Before you set up a swap file, it is important to check that there are any existing swap files in the system.
Run the following command:

Swapon-s

If the summary of the information returned is empty, the Swap file does not exist.

2, check the file system
Before you set up the swap file, it is also necessary to check the file system to see if there is enough disk space to set swap. Run the following command:

Df-hal

Check the returned information and have enough disk space left.

3. Create and allow Swap files
The following uses the DD command to create a Swap file.

DD If=/dev/zero of=/swapfile bs=1024 count=512k

Parameter interpretation:
if= FileName: Enter a filename, default to standard input. The source file is specified. < If=input file >
of= FileName: Output file name, default to standard output. The destination file is specified. < Of=output file >
Bs=bytes: Set the read/output block size to bytes bytes at the same time
Count=blocks: Copies only blocks blocks, the block size equals the number of bytes specified by BS.

4. Format and activate the Swap file
You have created the Swap file and you need to format it before you can use it. To run the command:

Mkswap/swapfile

To activate Swap, run the command:

Swapon/swapfile

The above steps are done and run the command again:

Swapon-s

You will find an overview of the information returned:

Filename Type Size Used Priority
/swapfile file 524284 0-1

If you want to mount Swap automatically when the machine restarts, you will also need to modify the Fstab configuration.
Open the/etc/fstab file with vim and add the following line at the end:

/swapfile Swap Defaults 0 0

Finally, give the Swap file the appropriate permissions:

Chown Root:root/swapfile
chmod 0600/swapfile

At the same time, we can also modify the swappiness of the Linux Swap space, reduce the cache on the hard disk.
Linux uses part of the hard disk as a SWAP partition, used for process scheduling – processes are running programs – tune the currently unused process to ' wait (standby) ', and even ' sleep ', once it is used, and then tuned to ' active ', the process of sleep Swap partition, empty the memory out of the ' activity ' process. Www.111cn.net
If the memory is large enough, you should tell Linux not to use the Swap partition too much, and you can set it by modifying the parameters of the swappiness. Swappiness=0 the maximum use of physical memory, and then the swap space, swappiness=100 when the positive use of the swap partition, and the memory of the data in a timely manner into the swap space.
In CentOS, the default value for Swappiness is 60.
You can see from the following command:

Cat/proc/sys/vm/swappiness

return value 60

We can adjust the value of swappiness to a suitable parameter so as to optimize the use of Swap. Here we set it to 10.
Using the SYSCTL command:

Sysctl vm.swappiness=10

But this is only a temporary change, after you reboot the system will restore the default 60, to permanent settings, but also need to modify the sysctl.conf in vim:

Vi/etc/sysctl.conf

At the end of this document, add this line:

# Search for the vm.swappiness setting. Uncomment and change it as necessary.
vm.swappiness=10

Input: X, save quit vim.

As a result, the Swap partition is restarted and will take effect.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.