As we all know, the modern operating system has realized the technology of "virtual memory", which not only breaks through the limitation of physical memory in function, but also enables the program to manipulate a larger space than the actual physical memory. More importantly, "virtual memory" is a safety net to isolate each process, so that each process is not interfered by other programs.
The function of swap space can be simply described as: when the physical memory of the system is not enough, it is necessary to release some space in the physical memory to the currently running program. The released space may come from some programs that have not been running for a long time. These released spaces are temporarily saved in the swap space, and then when those programs are about to run, the saved data is restored from the swap space to the memory. In this way, the system always exchanges when there is not enough physical memory.
Computer users often encounter this phenomenon. For example, when using Windows system, you can run multiple programs at the same time. When you switch to a program that you haven't paid attention to for a long time, you will hear the hard disk "squeak". This is because the memory of this program was "stolen" by those frequently running programs and put in the swap area. So once this program is put on the front end, it will retrieve its own data from the exchange area, put it into memory, and then continue to run.
It should be noted that not all data exchanged from physical memory will be put into Swap (if so, Swap will be overwhelmed), and a considerable amount of data will be directly exchanged to the file system. For example, some programs will open some files and read and write files (in fact, each program must open at least one file, which is the running program itself). When the memory space of these programs needs to be exchanged, it is not necessary to put some data in the exchange space, but it can be put directly in the file. If it is a file reading operation, the memory data will be released directly without exchange, because it can be recovered directly from the file system when needed next time; If you write to a file, you only need to save the changed data to the file for recovery. However, the data of those objects generated by malloc and new functions are different. They need swap space, because they have no corresponding "reserved" files in the file system, so they are called "anonymous" memory data. This kind of data also includes some state and variable data in the stack. Therefore, the exchange space is the exchange space of "anonymous" data.
Break through the 128M swap limit.
It is often seen in some Linux (Chinese version) installation manuals that the swap space cannot exceed 128M. Why is there such a statement? Before explaining the origin of the number "128M", first give the answer to a question: there is no limit of 128M at present! Now the limit is 2G!
The swap space is paged, and the size of each page is the same as that of the memory page, which is convenient for data exchange between the swap space and the memory. When the old version of Linux implemented swap space, the first page of swap space was used as a "bitmap" of all pages of swap space. In other words, each bit on the first page corresponds to a page swap space. If this bit is 1, it means that Swap on this page is available; If it is 0, it means that the page is a bad block and cannot be used. Therefore, the first swap mapping bit should be 0, because the first page swap is a mapped page. In addition, the last 10 mapping bit is also occupied to indicate the version of Swap (the original version is Swap_space, and the current version is swapspace2). Then, if the size of a page is s, this exchange implementation method can manage "8 * (s- 10)- 1" exchange pages. For i386 system, if s=4096, the space size * * * is 133890048, and if1MB = 2 20 bytes, the size is exactly 128M. ..
The reason for realizing swap space management in this way is to prevent bad blocks in swap space. If the system detects bad blocks in the swap, it will mark 0 on the corresponding bitmap, indicating that the page is unavailable. In this way, when using Swap, bad blocks will not be used, which will cause system errors.
Today's system designers believe that:
1. Now the hard disk quality is very good, and there are few bad blocks.
2. Even if there are, there are not many. Just list the bad blocks, and you don't need to build a picture on every page.
If there are many bad blocks, you should not use this hard disk as swap space.
In this way, the current Linux cancels the bit mapping method and the restriction of 128M, and accesses directly by address, which is limited to 2G.
Influence of switching configuration on performance
Allocating too much swap space will waste disk space, while allocating too little swap space will lead to system errors.
If the physical memory of the system runs out, the system will run slowly, but it can still run; If the swap space is insufficient, the system will make an error. For example, a Web server can spawn multiple service processes (or threads) according to different requests. If the swap space runs out and the service process can't start, an "application memory is insufficient" error will usually occur, which will lead to a deadlock of the service process in severe cases. Therefore, the allocation of exchange space is very important.
Generally speaking, the swap space should be greater than or equal to the size of physical memory, and the minimum should not be less than 64M. Usually, the swap space should be 2-2.5 times the size of physical memory. However, according to different applications, there should be different configurations: if it is a small desktop system, it only needs a small exchange space, while a large server system needs different exchange space according to different situations. Especially for database servers and Web servers, with the increase of visits, the demand for exchange space will also increase. See the server product description for specific configuration.
In addition, the number of swap partitions also has a great impact on performance. Because the swap operation is the operation of disk IO, if there are multiple swap areas, the allocation of swap space will be operated on all swaps in turn, which will greatly balance the load of IO and speed up the 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 performance monitoring tools, you will find that the CPU is not very busy at this time, but the system is very slow. This shows that the bottleneck lies in IO, and increasing the speed of CPU can't solve the problem.
System performance monitoring
The allocation of swap space is very important, but performance monitoring during system operation is more valuable. Through performance monitoring tools, we can check the performance index of the system and find the bottleneck of the system performance. This paper only introduces some commands and usages related to Swap under Solaris.
The most commonly used command is Vmstat (there are some commands on most Unix platforms), and you can view most performance indicators.
For example:
# vmstat 3
Procs memory swap io system cpu
R b w swpd free buffer cache si so bi bo in cs us sy id
0 0 0 0 93880 3304 19372 0 0 10 2 13 1 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 1 12 6 0 0 100
…………
Command description:
The parameter after vmstat specifies the time interval for capturing performance indicators. 3 means capture every three seconds. The first line of data does not need to be read, which is worthless and only reflects the average performance since the boot. Starting from the second line, reflect the system performance indicators every three seconds. These performance indicators related to SWAps include:
W according to the program
Indicates the number of processes that need to release memory and swap out at present (within three seconds).
Swpd under memory
It represents the size of swap space used.
Yes, so in exchange.
Si represents the current (within three seconds) total amount swapped back into memory (swapped in) per second, in kilobytes; ; So represents the total amount of memory swapped out per second (in kilobytes) at present (in three seconds).
The greater the number of the above indicators, the busier the system. The busy degree of the system displayed by these indicators is related to the specific configuration of the system. The system administrator should write down the values of these indicators when the system is in normal operation, and compare them when the system has problems, so as to quickly find the problems and formulate the standard indicator values for the normal operation of the system for performance monitoring.
In addition, using Swapon-s can also simply check the current usage of switching resources. For example:
# swapon -s
File name type size use priority
/dev/hda9 partition 36 1420 0 3
It is convenient to check the size of used and unused resources in the swap space.
The switching load should be kept below 30% to ensure the good performance of the system.
System commands for swap operations
Follow these steps to increase swap space:
1) becomes the superuser.
$su - root
2) Create a swap file
# DD if =/dev/zero of = swap file bs = 1024 count = 65536
Create a swap file with contiguous space.
3) Activate the exchange file
#/usr/sbin/swapon exchange file
The swap file refers to the swap file created in the previous step. 4) Now the newly added swap file has worked, but after the system is restarted, the previous steps can't be remembered. Therefore, record the file name and exchange type in the /etc/fstab file, for example:
/path/swapfile none Swap sw,pri=3 0 0
5) Check whether the swap file has been added.
/usr/sbin/swapon -s
Delete the extra swap space.
1) becomes the superuser.
2) Use Swapoff command to reclaim swap space.
#/usr/sbin/swapoff exchange file
3) Edit the /etc/fstab file to delete the entity of this exchange file.
4) Recover the file from the file system.
#rm exchange file
5) Of course, if this swap space is not a file but a partition, then you need to create a new file system and attach it to the original file system.