Swap partition, that is, the swap area, the system exchanges with Swap when the physical memory is not enough. In fact, the adjustment of Swap is crucial to the performance of Linux servers, especially Web servers. By adjusting Swap, system performance bottlenecks can sometimes be overcome and system upgrade costs can be saved.
Allocating too much swap space will waste disk space, while too little swap space will cause system errors.
If the physical memory of the system is exhausted, the efficiency of the system is reduced, but it can still run; if the swap space is used up, then the system will have an error. For example, a web server can derive multiple service processes (or threads) based on different requests. If the swap space is used up, the service process cannot be started, and the error “application is out of memory” usually appears, which can cause serious problems. The deadlock of the service process. Therefore, the allocation of swap space is very important.
Under normal circumstances, the swap space should be greater than or equal to the size of the physical memory, and the minimum size should not be less than 64M. Generally, the size of the swap space should be 2-2.5 times that of the physical memory. But according to different applications, there should be different configurations: if it is a small desktop system, only a small swap space is required, while a large server system requires different sizes of swap space depending on the situation. Especially for database servers and web servers, as the amount of access increases, the requirements for swap space will also increase. Please refer to the description of each server product for specific configurations.
In addition, the number of swap partitions also has a great impact on performance. Because the operation of Swap exchange is the operation of disk IO, if there are multiple Swap exchange areas, the allocation of Swap space will be operated in turn in all Swaps, which will greatly balance the IO load and accelerate the speed of Swap exchange. If there is only one exchange area, all 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 performance monitoring tools, 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 the problem cannot be solved by increasing the speed of the CPU.
1. Enter a directory:
cd /var/
2. Obtain a 256M file block:
dd if=/dev/zero of=swapfile bs=1024 count=262144
If you need to add a 2G SWAP partition, get the 2G file block:
dd if=/dev/zero of=swapfile bs=1024 count=2098176
3. Create a swap file:
/sbin/mkswap swapfile
4. Activate the swap file:
/sbin/swapon swapfile
5. Check whether swap is correct:
/sbin/swapon -s
6. Add to the fstab file to automatically start the system when it boots:
vi /etc/fstab
Add the following at the end:
/var/swapfile swap swap defaults 0 0
You can also execute the following command, which feels more convenient:
echo "/var/swapfile swap swap defaults 0 0" >>/etc/fstab
DD parameters:
dd command
Function: Copy the specified input file to the specified output file, and format conversion can be performed during the copy process. This command can be used to realize the function of the diskcopy command under DOS. First use the dd command to write the data on the floppy disk into a registered file of the hard disk, and then write the registered file to the second floppy disk to complete the diskcopy function. It should be noted that the registered files on the hard disk should be deleted with the rm command. The system uses standard input files and standard output files by default.
Syntax: dd [options]
if = input file (or device name).
of = output file (or device name).
ibs = bytes Read bytes bytes at a time, that is, the number of bytes read into the buffer.
skip = blocks skip reading the ibs*blocks block at the beginning of the buffer.
obs = bytes Write bytes bytes at a time, that is, the number of bytes written into the buffer.
bs = bytes set the number of bytes in the read/write buffer at the same time (equal to setting ibs and obs)
cbs = byte Convert bytes at a time.
count=blocks Only copy the input blocks.
Note: Alibaba Cloud turns off the SWAP partition in the startup item by default. There is a line swapoff -a in the /etc/rc.d/rc.local file, indicating that swap is disabled. Just delete this line.
Reprint:
给阿里云ECS添加SWAP交换分区