The method of configuring swap area in CentOS

Time:2021-1-13

CentOS is the most stable server version in the world of Linux. Rhas5 has always had a wide range of applications. Before, it has always focused on FreeBSD. Because of the needs of applications, especially in conjunction with MySQL and Oracle, red hat has too many advantages.

CentOS is used to replace the use of red hat as, because CentOS 5 is compiled and released by using the source code of rhas5 and removing the trademark and icon of RedHat. It is the latest compiled version of the source code. I use CentOS 5.2

When installing CentOS 5.2, forget to set swap partition. In the case of small memory, it is more effective to increase the role of swap.

Before configuring swap, check swap and disk space through free and DF to compare changes after configuring swap.

Copy code

The code is as follows:

free -m</p>
<p>[[email protected] ~]# free -m
total used free shared buffers cached
Mem: 1008 955 53 0 17 168
-/+ buffers/cache: 769 239
Swap: 0 0 0</p>
<p> </p>
<p>df -h</p>
<p>[[email protected] ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/xvda1 20G 15G 4.2G 79% /
tmpfs 505M 0 505M 0% /dev/shm
/dev/xvdb 9.9G 5.1G 4.4G 54% /home/data

 

The first step is to create a file block

Copy code

The code is as follows:

[ [email protected]homer ~]#CD / var (enter a folder)</p>
1024000+0 records out
1048576000 bytes (1.0 GB) copied, 23.3843 s, 44.8 MB/s</p>
on whole disk. Use -f to force.
Setting up swapspace version 1, size = 1023996 KiB
no label, UUID=e878f535-ea6f-4516-8a34-f1458951aa36</p>
<p>[[email protected] var]# ll swapfile
-Rw-r – R – 1 root 1048576000 November 27 00:55 swapfile</p>
/var/swapfile file 1023992 0 -1</p>

Add the following at the end:

Copy code

The code is as follows:

/var/swapfile swap swap defaults 0 0</p>
<p>[[email protected] var]# reboot

After the restart, the top command shows the size of the swap partition

Copy code

The code is as follows:

free -m</p>
<p>[[email protected] ~]# free -m
total used free shared buffers cached
Mem: 1008 528 480 0 25 128
-/+ buffers/cache: 375 633
Swap: 999 0 999</p>
<p> </p>
<p>df -h</p>
<p>[[email protected] ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/xvda1 20G 16G 3.2G 84% /
tmpfs 505M 0 505M 0% /dev/shm
/dev/xvdb 9.9G 5.1G 4.4G 54% /home/data

 

According to the above comparison between free and DF and before configuring swap, it is found that swap increases from 0 to 999 (about 1GB), while the system main disk (/ dev / xvda1) decreases from 4.2g to 3.2g, about 1GB, that is, the space of swap comes from the system main disk (/ dev / xvda1)

Problems and Solutions

1. Format and start swap file

Next, execute the mkswap command to format the myswap file into the s file system for the system to use. Switch to the / tmp directory and execute the following command:

Copy code

The code is as follows:

#Mkswap SwapFree (format file to swap file format)</p>

To stop using the newly created swap file, just execute the swapoff / TMP / SwapFree command. If the swap file is no longer used, you can delete it.

2. Check swap

Copy code

The code is as follows:

#swapon -s

3. The newly added swap partition will be started automatically at boot time

It’s too troublesome to start the swap partition or file by executing the swapon command after each boot. At this time, you can use the text editor to add a line to the / etc / fstab file, so that the swap partition and file can be started automatically at boot time

Copy code

The code is as follows:

/Dec / hdb5 swap swap defaults 0</p>

Swap space size:

Generally, the swap space should be greater than or equal to the size of physical memory, and the minimum size should not be less than 64M. Generally, the size of swap space should be 2-2.5 times of physical memory. But according to different applications, there should be different configurations: if it is a small desktop system, it only needs a small swap space, while a large server system needs different sizes of swap space depending on the situation. Especially for database server and web server, with the increase of access, the requirement for swap space will also increase. For specific configuration, please refer to the description of each server product.

Number of swap:

The number of swap partitions also has a significant impact on performance. Because the operation of swap is the operation of disk IO, if there are multiple swap areas, the allocation of swap space will operate on all swaps in turn, which will greatly balance the load of IO and accelerate the speed of swap. If there is only one exchange area, all the exchange operations will make the exchange area very busy, making the system in a waiting state most of the time, and the efficiency is very low. Using the performance monitoring tool, you will find that the CPU is not very busy at this time, but the system is slow. This shows that the bottleneck is Io, and it can not be solved by improving the speed of CPU.

Recommended Today

Algorithm Engineering 3. Mathematical basis, probability theory and statistics

Traditional machine learning can be said to use probability theory everywhere. probability theory 1. Total probability formula and Bayesian formula Total probability formula$$P(A)=\sum\limits_{j=1}^{n}P(B_j)P(A|B_j)$$Bayes formula$$P(B_i|A)=\dfrac{P(A,B_i)}{P(A)}=\dfrac{P(B_i)P(A|B_i)}{\sum\limits_{j=1}^{n}P(B_j)P(A|B_j)}$$Bayesian formula is the core tool of Bayesian statistics. Bayesian school thinks that the probability of event occurrence is not as simple as frequency school, but should add human priors, so that […]