What is a Swap?
Swap, namely the exchange zone, in addition to the installation of Linux, how many people cared about it then? In fact, the adjustment Swap on Linux servers, especially Web server performance is critical. By adjusting the Swap, sometimes across the system performance bottleneck, saving system upgrade costs.
Swap principle is a more complex problem that requires a lot of space to explain. Here only a brief introduction, and discussed in detail Swap implementation details in a future article.
As we all know, modern operating systems to achieve a "virtual memory" of this technology, not only in function to break the limit of physical memory, so the program can manipulate larger than the actual physical memory space, and more importantly, the "virtual memory" is to isolate each a process safety net, so that each process are not subject to interference from other programs.
Role Swap space can be simply described as follows: When the system's physical memory is not enough when it needs to be part of the physical memory space freed up to use for the currently running program. Those spaces may be released from a number of a long time there is no operating procedures, which were released into space Swap space is temporarily stored in, wait until those programs to run, and then restore the data from Swap saved into memory. Thus, the system is always in physical memory is not enough only to carry out the exchange of Swap.
Computer users often encounter this phenomenon. For example, when using a Windows system, you can run multiple programs simultaneously, when you switch to a long time did not care about the program, you will hear the hard drive "rushing" direct sound. This is because the program's memory by those who frequently run program to "steal", and put the Swap area. Therefore, once the program is placed to the front, it will get their data from Swap area, www.linuxidc.com it into memory, and then run.
It should be noted that not all exchange from the physical memory of the data will be put Swap (if this is the case, it will be overwhelmed Swap), a considerable part of the data is exchanged directly to the file system. For example, some program will open some files, the file read and write (in fact, each program is at least open a file that runs the program itself), when these programs need to be swapped out of memory space, there is no need to Swap data file section into the space, and you can put it directly into the file to go. If the file is a read operation, the memory data to be released directly, without exchanging it, because next time you need, you can restore directly from the file system; if it is to write the file, save only need to change the data to a file in order to restore. But those data using malloc and new functions to generate the object is different, they need Swap space because they do not have the "reserve" in the file system, so called "Anonymous" (Anonymous) memory data. Such data also includes a stack of some of the state variables and data. So, Swap space Swap space is "anonymous" data.
How to Set Up Swap partition size
I remember someone comments on Swap partition size is set too: "As long as the waste is not afraid of the hard disk, then the bigger the better, because the linux kernel before physical memory is completely exhausted not to touch swap"
However, according to my experience, this may not be Oh! Much swap space will cause the kernel that has a huge memory space without restraint want to catch the data into memory, causing kernel has been doing memory swap, jointly slow down the system response time.
Honestly, 1G RAM if you do not run things like p2p, it set up a 256MB enough to use; even if there are 2G are provided not set.
Also because if you really do need a software huge memory space to operate, then the swap had hands and feet, but for the best performance spread across multiple physical disks (similar raid equal effect)!
In fact, how to set Swap partition size is the best way to check a Linux system administrator level test, Swap in the end how to set it? I think so: First, we need to know which server should run this program, their respective memory size is how much, after checking the exact size of Swap partition can be determined:
(Memory size + Swap partition size) * 70% = 80% or procedures need to take total memory
Swap partition during the test program is also a great use, for example, administrators can partition by usage Swap, monitoring whether the system memory leaks, while the Web application projects can also provide a better traffic spikes buffer. A Linux system administrator should be able to monitor the situation through the use of Swap partition on the system, the program has a reasonable assessment.
System performance monitoring
Swap space allocation is very important, and performance monitoring system is in operation but more valuable. Through performance monitoring tool that can check the performance of the system to find the system performance bottleneck. This article only introduce under Solaris and Swap-related commands and uses.
The most common is Vmstat command (in most Unix platforms have such a number of command), this command can view most performance indicators.
# 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 93,880 3304 1,937,200,102,131,100,099
0 0 0 0 93,880 3,304,193,720,000,109,800 100
0 0 0 0 93,880 3,304,193,720,000,112,600 100
vmstat back parameter specifies the performance capture interval. 3 shows capture every three seconds. Do not look at the first row of data, there is no value, it simply reflects the average performance since boot. From the second line reflects the system performance within every three seconds. Swap these performance indicators and relevant include the following:
w procs under
It represents the current number of processes (within three seconds) needs to free memory, swap out.
swpd memory under
It indicates the size of the use of Swap space.
Swap under si, so
si represents the current (within three seconds) per second, the total return swap memory (Swap in), in units of kbytes; so represents the current (within three seconds) the exchange of the total amount of memory (Swap out) per second, units of kbytes.
More than the number of indicators, the more that the system is busy. System busy these performance indicators, and the specific system configuration. System administrators should be in the normal system during normal operation, write down the value of these indicators, while the system is a problem, and then to compare, it will quickly identify problems and develop standards index value of the system normal operation, for performance monitor usage.
In addition, the use of Swapon-s can easily view the current usage Swap resources. E.g:
# Swapon -s
Filename Type Size Used Priority
/ Dev / hda9 partition 361420 0 3
It can easily be seen that the size of the Swap space used and unused resources.
Swap should make the load was maintained at 30% or less, so as to ensure good performance of the system.
Swap-related operating system commands
Swap space increases, divided into the following steps:
1) Become superuser
$ Su - root
2) Create a Swap File
# Dd if = / dev / zero of = swapfile bs = 1024 count = 65536
Create a continuous space swap file.
3) Activate Swap File
# / Usr / sbin / swapon swapfile
swapfile refers to the swap file is created in the previous step. 4) Now the newly added Swap file already worked, but after the system reboots and does not remember the first few steps of the operation. Therefore, to be recorded in the file / etc / fstab file name, and Swap types, such as:
/ Path / swapfile none Swap sw, pri = 3 0 0
Whether 5) test file with Swap
/ Usr / sbin / swapon -s
Remove the extra space Swap.
1) Become superuser
2) Use Swapoff order resumption Swap space.
# / Usr / sbin / swapoff swapfile
3) Edit / etc / fstab file, remove the entities in this Swap files.
4) recovering the file from the file system.
5) Of course, if the Swap space is not a file, but a partition, you need to create a new file system, and then attached to the original file system.