How to add swap in Linux)

Source: Internet
Author: User

1. View System swap space usage

[Root @ jumper USR] # Free
Total used free shared buffers cached
Mem: 513980 493640 20340 0 143808 271780
-/+ Buffers/cache: 78052 435928
Swap: 1052248 21256 1030992

2. Create a swap file in a suitable space

[Root @ jumper USR] # mkdir swap
[Root @ jumper USR] # cd swap
[Root @ jumper swap] # dd If =/dev/Zero of = swapfile BS = 1024 COUNT = 10000
10000 + 0 records in
10000 + 0 records out
[Root @ jumper swap] # ls-Al
Total 10024
Drwxr-XR-x 2 root Root 4096 July 28 14:58.
Drwxr-XR-x 19 Root 4096 July 28 14:57 ..
-RW-r -- 1 Root 10240000 July 28 14:58 swapfile

[Root @ jumper swap] # mkswap swapfile
Setting up swapspace version 1, size = 9996 kib

3. Activate the swap file
[Root @ jumper swap] # Swapon swapfile
[Root @ jumper swap] # ls-l
Total 10016
-RW-r -- 1 Root 10240000 July 28 14:58 swapfile
[Root @ jumper swap] # Free
Total used free shared buffers cached
Mem: 513980 505052 8928 0 143900 282288
-/+ Buffers/cache: 78864 435116
Swap: 1062240 21256 1040984
[Root @ jumper swap] #

 

 

Swap, that is, in the SWAp zone, how many people care about it except when installing Linux? In fact, swap adjustment is crucial to the performance of Linux servers, especially Web servers. By adjusting swap, the system performance bottleneck is sometimes crossed to save system upgrade costs.

This article includes:

Swap Principle

128 M swap limit exceeded

Impact of swap configuration on Performance

Swap performance monitoring

System commands for swap operations
Swap Principle

The principle of swap is a complicated problem, which requires a lot of space. Here, we will only give a brief introduction. We will discuss in detail the implementation details of swap in future articles.

As we all know, modern operating systems have implemented the "virtual memory" technology, which not only breaks through the physical memory restrictions in terms of functionality, so that programs can manipulate space larger than the actual physical memory, more importantly, "Virtual Memory" isolates the security protection network of each process so that each process is not disturbed by other programs.

The role of the swap space is described as follows: when the system's physical memory is insufficient, a part of the physical memory needs to be released for use by the currently running program. The released space may come from some programs that haven't been operated for a long time. The released space is temporarily saved to the swap space and will be executed by those programs, then, the stored data is restored from SWAp to the memory. In this way, the system always performs swap switching when the physical memory is insufficient.

Computer users often encounter this phenomenon. For example, when you use a Windows system, you can run multiple programs at the same time. When you switch to a program that has not been noticed for a long time, you will hear the hard disk crash. This is because the memory of this program is stolen by frequently running programs and put in the SWAp area. Therefore, once the program is placed on the front end, it will retrieve its data from the swap area, put it into the memory, and then run.

It should be noted that not all data exchanged from the physical memory will be put into swap (if so, swap will be overwhelmed ), A considerable amount of data is directly exchanged to the file system. For example, some programs open some files and read and write the files (in fact, each program must open at least one file, that is, run the program itself ), when you need to swap out the memory space of these programs, there is no need to put the data in the file part into the swap space, and you can directly put it in the file. If it is a file read operation, the memory data is directly released and does not need to be exchanged, because it can be directly restored from the file system when needed next time; if it is a file write operation, you only need to save the changed data to the file for recovery. However, the data of objects generated using the malloc and new functions is different. They need swap space because they do not have the corresponding "reserve" file in the file system, therefore, it is called "anonymous" (anonymous) memory data. This type of data also includes some status and variable data in the stack. Therefore, the swap space is the swap space for "anonymous" data.

128 M swap limit exceeded

It is often seen that some Linux (Chinese version) Installation manuals have such instructions: swap space cannot exceed 128 MB. Why is there such a saying? Before explaining the history of the number "128 M", I would like to answer a question: there is no m limit at all! The current limit is 2 GB!

Swap space is paging. The size of each page is the same as that of the Memory Page, facilitating data exchange between swap space and memory. In the earlier version of Linux, when implementing the swap space, the first page of the swap space is used as a "Bit Map" (bit map) for all the swap space pages ). This means that each bit on the first page corresponds to a page of swap space. If this parameter is set to 1, swap is available on this page. If it is set to 0, this page is a bad block and cannot be used. In this case, the first swap ing bit should be 0, because the first page of swap is a ing page. In addition, the last 10 ing bits are also occupied to represent the swap version (the original version is swap_space, and the current version is swapspace2 ). If the size of one page is S, the swap implementation method can manage "8 * (S-10)-1" swap pages. For i386 systems, if S = 4096, the total space size is 133890048. If 1 MB = 2 ^ 20 byte, the size is exactly 128 M.

To manage the swap space in this way, it is necessary to prevent bad blocks in the swap space. If the system checks that swap has bad blocks, 0 is marked on the corresponding bit ing, indicating that this page is unavailable. In this way, the system generates errors instead of Bad blocks when using swap.

Currently, system designers believe that:

1. The hard disk is of good quality and there are few bad disks.

2. Even if there are not many, you only need to list the Bad blocks, instead of creating a ing for each page.

3. If there are many bad blocks, this hard disk should not be used as the swap space.

Therefore, in Linux, the bitmap method is removed, and the M limit is also removed. Direct access with address, limited to 2G.

Impact of swap configuration on Performance

If you allocate too much swap space, the disk space will be wasted, and the swap space is too small, the system will encounter an error.

If the system's physical memory is used up, the system will run slowly but still run. If the swap space is used up, the system will encounter an error. For example, a Web server can generate multiple service processes (or threads) based on the number of requests. If swap space is used up, the service process cannot be started, generally, the error "application is out of memory" may occur. In severe cases, the service process may be deadlocked. Therefore, swap space allocation is very important.

Generally, swap space should be greater than or equal to the size of physical memory, and the minimum should not be less than 64 M. Generally, the size of swap space should be 2-times that of physical memory. However, different applications should have different configurations: For a small desktop system, only a small swap space is required, A large server system requires different sizes of swap space depending on the situation. Especially for database servers and web servers, as the access traffic increases, the requirements for swap space will also increase. For detailed configuration, see the description of each server product.

In addition, the number of swap partitions has a great impact on the performance. Because swap operations are disk Io operations, if there are multiple swap areas, the swap space allocation will operate on all swap in turn, this will greatly balance the IO load and speed up swap switching. If there is only one swap area, all the swap operations will make the swap area very busy, so that the system is waiting for most of the time, the efficiency is very low. The performance monitoring tool will find that the CPU is not very busy at this time, but the system is slow. This shows that the bottleneck lies in I/O, and it cannot be solved by increasing the CPU speed.
System Performance Monitoring

The allocation of swap space is important, but the performance monitoring during system operation is more valuable. Through performance monitoring tools, you can check the performance indicators of the system and find the bottleneck of the system performance. This article only describes some swap-related commands and usage in Solaris.

The most common command is the vmstat command (which is available on most Unix platforms), which allows you to view most performance metrics.

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 0 0 10 2 19372 10 0 0 99
0 0 0 0 93880 3304 0 0 0 19372 8 0 0 109
0 0 0 0 93880 3304 0 0 0 19372 6 0 0 112
............

Command description:
Parameters after vmstat specify the time interval for performance indicator capture. 3 indicates capture every three seconds. The first line of data does not need to be viewed and has no value. It only reflects the average performance since the startup. Starting from the second line, the system performance indicators within three seconds are reflected. Swap-related performance indicators include the following:

W under procs
It indicates the number of processes that need to be released and swapped out in the current (within three seconds.

SWPD under memory
It indicates the size of the swap space used.

Si, so under swap
Si indicates the total number of swap memories exchanged per second (within three seconds), measured in Kbytes. So indicates the total number of swap memories exchanged per second (within three seconds, the Unit is Kbytes.
The larger the number of indicators, the more busy the system. The system busy level of these indicators depends on the specific configuration of the system. During normal system operation, the system administrator should write down the values of these indicators and compare them when a system problem occurs. Then, the system administrator will soon find the problem, standard indicators for normal operation of the system are formulated for performance monitoring.

In addition, you can use Swapon-s to easily view the current usage of swap resources. For example:
# Swapon-S
Filename type size used priority
/Dev/hda9 partition 361420 0 3

It is easy to see the size of swap space used and unused resources.

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

System commands for swap operations

To increase the swap space, take 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 the 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 restarts, it does not remember the previous steps. Therefore, you must record the file name and swap type in the/etc/fstab file, for example:
/Path/swapfile none swap SW, PRI = 3 0 0

5) check whether the swap file is added
/Usr/sbin/Swapon-S

Delete unnecessary swap space.
1) become a Super User

2) use the swapoff command to reclaim swap space.
#/Usr/sbin/swapoff swapfile

3) edit the/etc/fstab file and remove the entity of the swap file.

4) reclaim the 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 mount it to the original file system.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.