Explanation of swap partition in Linux system installation

Source: Internet
Author: User
Tags aliyun

The role of swap partitions, or swap spaces, can be simply described as: when the physical memory of the system is not enough, it is necessary to release some of the space in physical memory for use by the currently running program. The freed space may come from programs that have not been manipulated for a long time, and the freed space is temporarily saved in the swap space until those programs run, recovering the saved data from the swap into memory. In this way, the system always does swap swap when physical memory is not enough. In fact, swap adjustments are critical to the performance of Linux servers, especially Web servers. By adjusting swap, you can sometimes bypass system performance bottlenecks and save on system upgrade costs.

As we all know, the modern operating system has realized the "virtual memory" this technology, not only in the function of the physical memory limit, so that the program can manipulate more than the actual physical memory space, more importantly, "virtual memory" is to isolate each process of the security network, so that each process is not affected by other programs interference.

Computer users often encounter this phenomenon. For example, when using a Windows system, you can run multiple programs at the same time, and when you switch to a program that has been ignored for a long time, you hear the hard drive "rushing" straight. This is because the memory of the program is "stolen" by those programs that are running frequently and placed in the swap area. Therefore, once the program is placed on the front end, it will retrieve its own data from the swap area, put it in memory, and then run.

It is important to note that not all data exchanged from physical memory will be put into swap (if so, the swap will be overwhelmed), and a significant portion of the data is exchanged directly to the file system. For example, some programs will open some files to read and write to the file (in fact, each program to open at least one file, that is, running the program itself), when the need to swap out the memory space of these programs, there is no need to put the file part of the data into the swap space, and can directly put it into the file. If it is a read file operation, then the memory data is released directly, do not need to swap out, because the next time you need to recover from the file system directly, if you are writing files, only need to save the changed data to a file in order to recover. However, the data for objects generated with malloc and the new function are different, they require swap space because they do not have a corresponding "reserve" file in the file system and are therefore referred to as "anonymous" (Anonymous) memory data. This type of data also includes some state and variable data in the stack. So the swap space is the swap space for "anonymous" data.

breakthrough 128M Swap Limit

Often see some Linux (domestic Chinese version) installation manual has this description: Swap space can not exceed 128M. Why is there such a claim? Before explaining the origin of the figure "128M", give the question a reply: There is no 128M limit! Now the limit is 2g!

The swap space is paged, and the size of each page is the same as the size of the memory page, facilitating exchange of data between swap space and memory. When an older version of Linux implements swap space, the first page of the swap space is used as a bit map for all swap space pages. This means that every bit of the first page corresponds to a single page of swap space. If this is 1, the page swap is available, or 0 indicates that the page is a bad block and cannot be used. So the first swap mapping bit should be 0, because the first page swap is the mapping page. In addition, the last 10 mapping bits are also used to represent the Swap version (the original version is swap_space and the current version is Swapspace2). So, if the size of a page is S, this method of swap can manage "8 * (S-10)-1" swap pages altogether. For i386 systems s=4096, the size of the space is 133890048, if you think 1 mb=2^20 byte, the size is exactly 128M.

impact of swap configuration on performance

Allocating too much swap space wastes disk space, and there is too little swap space for the system to get an error.

If the system is running out of physical memory, the system will run slowly, but still be able to run, and if the swap space is exhausted, the system will have an error. For example, the Web server can derive multiple service processes (or threads) depending on the number of requests, and if the swap space is exhausted, the service process cannot start, and the "application is out of memory" error usually occurs, causing the deadlock of the service process to be severe. Therefore, the allocation of swap space is very important.

Typically, the swap space should be greater than or equal to the size of the physical memory, the minimum should not be less than 64M, usually the size of the swap space should be 2-2 of physical memory. 5 times times. However, depending on the application, there should be different configurations: if it is a small desktop system, you only need a small swap space, while the large server system requires different sizes of swap space depending on the situation. In particular, the database server and Web server, with the increase in traffic, the swap space requirements will also increase, the specific configuration see the description of the server products.

In addition, the number of swap partitions has a significant impact on performance. Because swap operations are disk IO operations, if there are multiple swap zones, the swap space is allocated in a rotating manner to all swaps, which greatly balances the IO load and speeds up swap. If there is only one swap area, all switching operations will make the swap area very busy, leaving the system most of the time in a waiting state and inefficient. With the performance monitoring tool you will find that the CPU is not very busy at this time, but the system is slow. This shows that the bottleneck on the IO, relying on the speed of the CPU can not solve the problem.

Create a blank file with contiguous space

The physical memory of the server is 512MB, and I set the swap file to 1GB according to the 1.5~2 times principle.

#root @aliyun:/srv# dd if=/dev/zero of=swapfile bs=1024 count=1048576 1048576+0 Records in 1048576+0 Records out 10737418 Bytes (1.1 GB) copied, 59.7957 s, 18.0 MB/s
1 2 3 4 #root @aliyun:/srv# dd if=/dev/zero of=swapfile bs=1024 count=1048576 1048576 + 0 Records in 1048576 + 0 Records out 1073741824 bytes ( c18> 1.1 GB ) copied , 59.7957 s , 18.0 MB / s
Using swap files

Use the Swapon command to have the system use this file as a swap file. However, this file can not be used directly, otherwise it will be error:

Root @aliyun:/srv# swapon swapfile swapon:/srv/swapfile:read swap header failed:invalid argument
0 S Root @ Aliyun : / SRV # swapon Swapfile Swapon : / srv / swapfile : Read swap Header failed : Invalid argument

You must first format the file into swap format using Mkswap (you do not know why 4KB is missing):

Root @aliyun:/srv# mkswap swapfile 1048576 Setting up swapspace version 1, size = 1048572 KiB no label, uuid=1aaed031-33e f-479b-a9a4-2f008a7bbb2f
1 2 3 Root @ Aliyun : / SRV # Mkswap Swapfile 1048576 Setting up Swapspace version 1 , size = 1048572 20> KiB no label , UUID = 1aaed031 - 33ef - 479b - a9a4 - C37>2F008A7BBB2F

To use a formatted file:

Root @aliyun:/srv# swapon swapfile
1 Root @ Aliyun : / SRV # swapon Swapfile

To view file usage:

[Email protected]:/srv# swapon-s Filename Type Size used priority/srv/swapfile file 1048572 95852-1
1 2 3 root @ aliyun : / srv # Swapon-s filename                                   type             size     used     priority / srv / swapfile                            file              1048572 95852    - 1
Join Auto Enable

To avoid swapfile after a reboot, you can add swap-enabled code to the startup file, and for Ubuntu Server, edit the/etc/rc.local file to include the following (specific file path customization):

Swapon/srv/swapfile
1 Swapon / srv / swapfile

or

Modify the/etc/fstab file to include the following:

/srv/swapfile swap swap defaults 0 0
1 / 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.