About Swap partition during Linux Installation

Source: Internet
Author: User
Tags aliyun

About Swap partition during Linux Installation

Swap partition, that is, Swap zone. The role of Swap space can be simply described as: when the physical memory of the system is insufficient, a part of the physical memory needs to be released, for 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. 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.

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.

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 been ignored for a long time, you will hear the hard disk burst. 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 the memory data 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 "M", I would like to give 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 earlier versions of Linux, the first page between Swap spaces is used as a Bit map for all 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.

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 configurations should be made for different applications: 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.

Create a blank file with continuous space

The physical memory of the server is 512 MB ~ I set the swap file to 1 GB.

 
 
  1. #root @aliyun :/srv# dd if=/dev/zero of=SWAPFILE bs=1024 count=1048576 1048576+0 records in 1048576+0 records out 1073741824 bytes (1.1 GB) copied, 59.7957 s, 18.0 MB/s  
  2. #root@aliyun :/srv# dd if=/dev/zero of=SWAPFILE bs=1024 count=1048576 
  3. 1048576+0 records in  
  4. 1048576+0 records out  
  5. 1073741824 bytes (1.1 GB) copied, 59.7957 s, 18.0 MB/s 
Use swap files

Use the swapon command to allow the system to use this file as the swap file. However, this file cannot be used directly; otherwise, an error is returned:

 
 
  1. root @aliyun :/srv# swapon swapfile swapon: /srv/swapfile: read swap header failed: Invalid argument  
  2. root@aliyun:/srv# swapon swapfile  
  3. swapon: /srv/swapfile: read swap header failed: Invalid argument  

You must first use mkswap to format the file into the swap format. I don't know why 4 kb is missing ):

 
 
  1. root @aliyun :/srv# mkswap SWAPFILE 1048576 Setting up swapspace version 1, size = 1048572 KiB no label, UUID=1aaed031-33ef-479b-a9a4-2f008a7bbb2f  
  2. root@aliyun:/srv# mkswap SWAPFILE 1048576  
  3. Setting up swapspace version 1, size = 1048572 KiB  
  4. no label, UUID=1aaed031-33ef-479b-a9a4-2f008a7bbb2f  

Use formatted files:

 
 
  1. root @aliyun :/srv# swapon SWAPFILE  
  2. root@aliyun:/srv# swapon SWAPFILE  

View File Usage:

 
 
  1. root@aliyun:/srv# swapon -s Filename Type Size Used Priority /srv/SWAPFILE file 1048572 95852 -1  
  2. root@aliyun:/srv# swapon -s  
  3. Filename                                Type            Size    Used    Priority  
  4. /srv/SWAPFILE                           file            1048572 95852   -1  
Add auto-enable

To prevent swapfile from taking effect after restart, you can add the swap-enabled code to the Startup File. For ubuntu server, edit the/etc/rc. local file and add the following content to the custom file path ):

 
 
  1. swapon /srv/SWAPFILE  
  2. swapon /srv/SWAPFILE  

Or

Modify the/etc/fstab file and add the following content:

 
 
  1. /srv/SWAPFILE swap swap defaults 0 0  
  2. /srv/SWAPFILE   swap    swap    defaults        0       0  

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.