Home  >  Article  >  Operation and Maintenance  >  Detailed explanation of the role of swap partition under Linux

Detailed explanation of the role of swap partition under Linux

小云云
小云云Original
2018-01-24 13:22:252125browse

This article mainly introduces a detailed explanation of the role of swap partition under Linux. The editor thinks it is quite good and has certain reference value. Friends in need can refer to it. I hope it can help everyone.

This article mainly studies the related contents of swap partition under Linux. The specific introduction is as follows.

swap partition introduction

Embedded Linux Chinese site news, the Swap partition of the Linux system, that is, the swap area, the role of the Swap space can be simply described as: when the physical memory of the system is not enough , it is necessary to release a part of the space in the physical memory for use by the currently running program. The freed space may come from some programs that have not operated for a long time. The freed space is temporarily saved in the Swap space. When those programs are about to run, the saved data is restored from Swap to the memory. In this way, the system always performs Swap when the physical memory is insufficient. In fact, Swap adjustment 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.

As we all know, modern operating systems have implemented the "virtual memory" technology, which not only breaks through the limitations of physical memory in terms of functionality, allowing programs to manipulate space larger than the actual physical memory, More importantly, "virtual memory" is a safety protection net that isolates each process so that each process is not interfered by other programs.

Maybe computer users often encounter this phenomenon. For example, when using a Windows system, you can run multiple programs at the same time. When you switch to a program that you have not paid attention to for a long time, you will hear the hard disk beeping. This is because the memory of this program has been "stolen" by frequently running programs and placed in the Swap area. Therefore, once this program is placed on the front end, it will retrieve its data from the Swap area, put it into memory, and then continue running.

In addition, not all data exchanged from physical memory will be put into Swap (if so, Swap will be overwhelmed), a considerable part of the data will be swapped directly to the file system . For example, some programs will open some files and read and write files (in fact, every program must open at least one file, which is the running program itself). When the memory space of these programs needs to be swapped out, there is no need to The data in the file part is placed in the Swap space, but it can be placed directly into the file. If it is a file reading operation, the memory data is released directly and does not need to be swapped out, because it can be restored directly from the file system when needed next time; if it is a file writing operation, only the changed data needs to be saved to the file for recovery. But the data of objects generated by malloc and new functions are different. They require Swap space because they do not have corresponding "reserve" files in the file system, so they are called "anonymous" memory data. This type of data also includes some status and variable data in the stack. Therefore, Swap space is an exchange space for "anonymous" data.

Breaking through the 128M Swap limit

Some Linux (domestic Chinese version) installation manuals have such instructions: Swap space cannot exceed 128M. Why is this said? Before explaining the origin of the number "128M", let me first give an answer to the question: There is no 128M limit at all now! The current limit is 2G!

The Swap space in the Linux system is paged, and the size of each page is the same as the size of the memory page, which facilitates data exchange between the Swap space and the memory. When old versions of Linux implemented Swap space, they used the first page of Swap space as a "bit map" for all Swap space pages. This means that every bit on the first page corresponds to a page of Swap space. If this bit is 1, it means that Swap of this page is available; if it is 0, it means that this page is a bad block and cannot be used. In this way, the first Swap mapping bit should be 0, because the first Swap page is a mapping page. In addition, the last 10 mapping bits are also occupied to indicate the Swap version (the original version was Swap_space, the current version is swapspace2). Then, if the size of one page is s, this Swap implementation method can manage a total of "8 * (s - 10) - 1" Swap pages. For the i386 system, s=4096, the total space size is 133890048. If 1 MB=2^20 Byte is considered, the size is exactly 128M.
This way of managing Swap space is to prevent bad blocks in Swap space. If the system detects that there are bad blocks in Swap, it marks 0 on the corresponding bit map, indicating that this page is unavailable. In this way, when using Swap, bad blocks will not be used and cause errors in the system.

Current system designers believe:

1. The quality of hard disks is now very good, with very few bad blocks.
2. Even if there are, there are not many. You only need to list the bad blocks, and there is no need to create a mapping for each page.
3. If there are many bad blocks, this hard disk should not be used as Swap space.

So, now Linux has canceled the bit mapping method, and also canceled the 128M limit. Access directly by address, limited to 2G.

The impact of Swap configuration on performance

Allocating too much Swap space will waste disk space, and if there is too little Swap space, system errors will occur.

When the system's physical memory is used up, the system will run very slowly, but it can still run; if the Swap space is used up, an error will occur in the system. For example, a web server can spawn multiple service processes (or threads) according to different number of requests. If the Swap space is used up, the service process cannot be started, and an "application is out of memory" error will usually occur. In severe cases, it will cause Deadlock of service process. Therefore, the allocation of Swap space is very important.

Generally, the Swap space should be greater than or equal to the size of the physical memory, and the minimum should not be less than 64M. Usually the size of the Swap space should be 2-2.5 times the physical memory. However, according to different applications, there should be different configurations: if it is a small desktop system, only a smaller 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 number of visits increases, the requirements for Swap space will also increase. For specific configurations, please refer to the instructions of each server product.

In addition, the number of Swap partitions also has a great impact on performance. Because the Swap operation is a disk IO operation, if there are multiple Swap areas, the allocation of Swap space will be performed on all Swaps in turn, which will greatly balance the IO load and speed up the Swap exchange. If there is only one swap area, all swap operations will make the swap area very busy, leaving the system in a waiting state most of the time, which is very inefficient. 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.
System performance monitoring

Allocation of Swap space is certainly important, but performance monitoring while the system is running is even more valuable. Through performance monitoring tools, you can check various performance indicators of the system and find system performance bottlenecks. This article only introduces some commands and uses related to Swap under Solaris.

The most commonly used is the Vmstat command (there are such commands on most Unix platforms). This command can view most performance indicators. For example:

vmstat 3

procs memory swap io system cpu 
r b w swpd free buff cache si so bi bo in cs us sy id 
0 0 0 0 93880 3304 19372 0 0 10 2 131 10 0 0 99 
0 0 0 0 93880 3304 19372 0 0 0 0 109 8 0 0 100 
0 0 0 0 93880 3304 19372 0 0 0 0 112 6 0 0 100 
…………

Command description:

The parameters following vmstat specify the time interval for performance indicator capture. 3 means capture every three seconds. There is no need to read the first row of data, it is of no value. It only reflects the average performance since startup. Starting from the second line, system performance indicators are reflected every three seconds. Among these performance indicators, those related to Swap include the following:

w under procs

It indicates that the memory and swap need to be released currently (within three seconds) The number of processes going out.

swpd under memory

It indicates the size of the Swap space used.

si under Swap, so

si represents the current (within three seconds) total amount of memory swapped back (Swap in) per second, in kbytes ; so represents the current (within three seconds) total amount of memory swapped out (Swap out) per second, in kbytes.

The greater the number of the above indicators, the busier the system is. The system busyness represented by these indicators is related to the specific configuration of the system. System administrators should write down the values ​​of these indicators when the system is running normally. When a problem occurs with the system, they can compare it and quickly find the problem, and formulate standard indicator values ​​for the normal operation of the system to provide performance. Monitor usage.

In addition, you can also easily check the current usage of Swap resources using Swapon-s. For example:

swapon -s

Filename Type Size Used Priority

/dev/hda9 partition 361420 0 3

can be convenient You can clearly see the size of used and unused resources in Swap space.

The Swap load should be kept below 30% to ensure good system performance.

To increase Swap space, follow the following steps:

1) Become a super user

$su - root

2) Create a Swap file

dd if=/dev/zero of=swapfile bs=1024 count=65536

Create a swap file with continuous space.

3) Activate Swap file

/usr/sbin/swapon swapfile

swapfile refers to the swap file created in the previous step.

4) Now the newly added Swap file has taken effect, but after the system is restarted, the previous steps will not be remembered. Therefore, the name of the file and the Swap type should be recorded in the /etc/fstab file, such as:

/path/swapfile none Swap sw,pri=3 0 0

5) Check whether the Swap file is added with

/usr/sbin/swapon -s

to delete excess Swap space.

1) Become a super user

2) Use the Swapoff command to reclaim the Swap space.

#/usr/sbin/swapoff swapfile

3) Edit the /etc/fstab file and remove the entity of this Swap file.

4) Recycle this file from the file system.

#rm swapfile

5) Of course, if the Swap space is not a file, but a partition, you need to create a new file system and then attach it to the original file system .

Related recommendations:

Oracle swap 100% case analysis

How to prevent Oracle from using Linux swap partition

How MySQL avoids using Linux swap partitions and improves read and write performance

The above is the detailed content of Detailed explanation of the role of swap partition under Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn