Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction and usage of swap under Linux

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "the introduction and usage of swap under Linux". In daily operation, I believe many people have doubts about the introduction and usage of swap under Linux. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "introduction and usage of swap under Linux"! Next, please follow the editor to study!

What is swap?

A swap space is an area on disk that can be a partition, a file, or a combination of them. To put it simply, when the system's physical memory is tight, Linux will save the infrequently accessed data in memory to swap, so that the system will have more physical memory to serve each process, and when the system needs to access the content stored on swap, it will load the data on swap into memory, which is what we often call swap out and swap in.

Why do I need swap?

To answer this question, we need to answer what benefits swap brings to us.

For some large applications (such as LibreOffice, video editor, etc.), a large amount of memory is used during startup, but this memory is often only used at startup, and this memory is rarely used later in the run process. With swap, the system can save this part of the memory data that is not used in this way to swap, thus releasing more physical memory for the system to use.

The hibernation function of many distributions (such as ubuntu) depends on the swap partition. When the system sleeps, the data in memory will be saved to the swap partition, and the data will be loaded into memory the next time the system starts, which can speed up the startup of the system. So if you want to use the hibernation function, you must configure the swap partition, and the size must be greater than or equal to the physical memory.

In some cases, what if you want to run a program that consumes memory but has limited physical memory? At this point, you can achieve your goal by configuring enough swap space, which is slower, but at least you can run.

Although in most cases, physical memory is sufficient, there are always some unexpected situations, such as a process needs more memory than expected, or a process has a memory leak. When there is not enough memory, the OOM killer of the kernel will be triggered. According to the configuration of OOM killer, some processes will be dropped by kill or restarted directly by the system (the default is to give priority to the one that consumes the most memory). However, with swap, we can use swap as memory, which is a bit slow, but at least gives us a chance to go to the debug, kill process or save the current work progress.

If you have looked at Linux memory management, you will know that the system will use as much free memory as possible for cache to speed up the system's ILinux O speed, so if you can move less commonly used memory data to swap, there will be more physical memory for cache, thus improving the overall performance of the system.

What are swap's shortcomings?

The advantages of swap are described above, but what about the disadvantages of swap? Swap is stored on the disk, and the speed of the disk is several orders of magnitude slower than memory. If you keep reading and writing swap, it will definitely affect the performance of the system. Especially when the system memory is very tight, the frequency of reading and writing swap space will be very high, causing the system to run very slowly. Like dead, adding physical memory is the only solution.

Because the system will automatically move uncommonly used memory data to swap, for desktop programs, it may cause a small card to be opened after minimizing a program, because the data on swap needs to be reloaded into memory.

Do you want swap or not?

The above describes what swap is and their advantages and disadvantages, so do you want to configure swap or not? The answer is: it depends.

The following discusses the choice of swap for servers and desktops in three cases: insufficient memory, barely enough memory, and plenty of memory.

There is not enough memory

Whether it's a desktop or a server, when you obviously don't have enough physical memory and want to run a program, adding swap is the only option, and it's better to slow down than not to work.

There is barely enough memory.

It is recommended to configure swap, so that the kernel will move less commonly used data from memory to swap, so that there will be more physical memory for system calls, improve system performance, and avoid abnormal process exit due to occasional lack of physical memory. Improve system stability, but for the server, it is necessary to limit or monitor the use of swap space when swap space is used more than expected or swap in/out is frequent. Measures should be taken in time, otherwise it will have a great impact on performance.

Ample memory

In theory, swap is useless if there is enough physical memory and does not need hibernation, but the key problem is that it is difficult to ensure that physical memory is sufficient under any circumstances, because there are always unexpected situations, such as some processes consume more memory than expected, server pressure exceeds expectations, memory leaks and so on.

In this case where there is plenty of memory, can swap help us if an exception occurs?

Desktop environment

Generally speaking, no monitoring function is enabled, so it is impossible to predict the abnormal memory usage in advance. When the memory is used up, there are two situations:

Configured with swap: when the system slows down, you can feel that you may still have a chance to kill some processes and save the current work progress, and of course you will want to smash the computer slowly, but when the disk is so cheap, it is worth wasting some disk space in exchange for such an opportunity.

No swap configured: the kernel's OOM killer is triggered and may not even have a chance to save work progress.

Server environment

The server will generally configure a monitor to alarm or automatically restart abnormal processes when memory usage reaches a threshold. But what if there's no surveillance? When memory is used up, there are two situations:

Swap configured: at this time, the server can still provide services, but the performance will be reduced by several levels until it is almost in a state of panic, and this process will last for a long time, which is a disaster for the server. So configuring swap can only keep the service alive for a while, followed by long-term service outages (for example, the server that used to process 1000 requests per second can only handle 50 requests per second due to frequent use of swap. From a system point of view, the process is still running, but from a business point of view, the service has been almost interrupted).

No swap is configured: the OOM killer of the kernel is triggered. In the default configuration, the memory-consuming process is given priority to kill. This process is usually our business process, and the daemon will restart the business process automatically (no daemon? Are you kidding me? this situation will only cause the service to be interrupted for a while (depending on the time it takes for the process to restart), and will not cause poor performance and continuous service interruption due to the configuration of swap. Even if OOM killer doesn't kill the expected process, we can find it through testing, and then configure OOM killer to restart the system, which is better than configuring swap to survive there.

As can be seen from the above, it seems better for the server not to configure swap, which allows the problematic process to restart as soon as possible and shorten the time for the business to be affected.

And, even if no monitor is configured, we still have a memory control module in cgroups that can control the maximum amount of memory that a group of processes can use. When this number is exceeded, the corresponding behavior can be triggered, such as restarting the process.

Generally speaking, for the desktop environment, memory is generally not as abundant as on the server side, and many different types of GUI windows are opened due to usage scenarios, but there is only one process in the foreground, most of which are on standby in the background, so it is necessary to configure swap to improve performance. For the server, the configuration of memory is relatively abundant, the processes started are also processes to work (otherwise should not be started), and there is no need for sleep, coupled with cgroups, you can more easily limit the memory use of processes, I think that the configuration of swap is basically unnecessary, look at coreos, there is no swap by default.

What is the appropriate swap size configuration?

Since configuring swap is helpful for desktop systems, what size of swap is appropriate to configure? Here are some suggestions from ubuntu:

When the physical memory is less than 1G and there is no need for hibernation, you can set the swap space as the same size as the memory. When you need to hibernate, it is recommended to configure twice the physical memory size, but the maximum value should not exceed twice the memory size.

The recommended size is round (sqrt (RAM)) when the physical memory is greater than 1G and does not need to sleep, where RAM is the physical memory size; when dormant is needed, the recommended size is RAM+round (sqrt (RAM)), but the maximum value is not more than twice the memory size

If swap space twice the size of physical memory is not enough, it is recommended to increase memory instead of swap

The following are detailed recommendations for different physical memory situations, the first column is the size of physical memory, the second and third columns are recommended for situations where there is no need for sleep and the need for sleep, and the fourth column is the maximum value not to be exceeded

Physical memory (MB) does not need hibernation need hibernation maximum 256 512 512512 1024 10241024 1024 2048 2048 physical memory (GB) do not need hibernation need hibernation maximum 11 2 22 13 43 25 64 2 85 2 7 28128 3 11 1612 15 2416 4 20 3224 5 29 4832 6 38 6464 8 72 128128 11 139 256 how to configure swap?

When we have determined how much swap space to configure, how should we configure it? Of course, it can be allocated during system installation, but if we are not satisfied with the size allocated during installation, we can adjust it later. I will not describe how to install it here, but only how to add more swap space to the system.

There are two types of swap spaces under Linux, swap partitions and swap files, which have their own characteristics:

Since there is no file system above the swap partition, it is equivalent to the kernel directly accessing continuous disk space, and the efficiency is relatively high. However, since the swap partition is generally allocated when the system is installed, it is very inconvenient to reduce space and expand capacity later.

Swap files are placed in the file system of the specified partition, so it may be affected by the performance of the file system, but it is said that the kernel after version 2.6 can directly access the physical disk address of the swap file, which is equivalent to skipping the file system to access the disk directly. however, if the physical location of the swap file on the disk is discontinuous, it will still adversely affect performance, but its advantage is flexibility. You can add and remove swap files at any time.

View the configured swapdxt@DXT:~/QiuZhao/SharedMem/ShareMem$ free-mtotal used free shared buff/cache availableMem in the system: 3845 2986 120 411 737 143Swap: 3904 1540 2364

Or:

Use the command swapon-s to view the swap in use on the system

Dev@dev:~$ swapon-sFilename Type Size Used Priority/dev/dm-1 partition 524284 0-1

If you configure multiple swap partitions or files, there will be multiple lines, each representing a swap partition or file that is being used by the system. Here is the meaning of each field:

Filename: if the swap type is a partition, this will be the path to the partition, and if the swap type is a file, this will be the path to the file

The type of Type:swap, partition represents a swap partition, and file represents an swap file

The size of the Size:swap, in k, where 524284 represents about 512m

Used: the size that has been used, where 0 means it has not been used yet

Priority: priority, high priority swap will be given priority, equal priority swap will be evenly used (round-robin algorithm), and the priority can be set by "swapon-p" command.

Check the status of swap in/out in the system

It is not that the performance of swap degrades when it takes up more space. The real impact on performance is the frequency of swap in and out. The higher the frequency, the greater the impact on system performance. We can check the frequency of swap in/out through the vmstat command.

# Parameter 2 means to count every two seconds The columns si and so are the number of swap in and out per second dev@ubuntu:~$ vmstat 2procs-memory-swap----io-system-cpu-r b swpd free buff cache si so bi bo in cs us sy id wa st0 0 70232 75620 7940 209476 00 111 180 0 1 99 00 0 70232 75620 7940 209476 00 0116 186 11 99 00 0 70228 75620 7940 209476 20 20 120 193 11 98 70228 75620 7940 209476 00 00 117186 00 100000 07022875620209476 00113184 0 1 9900 add swap partition

Before adding a swap partition, you must first have a free partition. If it is a new disk, you can use fdisk to create a new partition for swap.

Note: disk partitioning operation must be careful, otherwise it will result in data loss and system failure. Disk partitioning is not covered in this article, so I won't discuss how to use fdisk here.

# the test environment used in this article is a virtual machine / dev/sdb is a newly added hard drive and a partition has been created with fdisk. In this example, we will use the partition / dev/sdb1 dev@dev:~$ sudo fdisk-l / dev/sdbDevice Boot Start End Sectors Size Id Type/dev/sdb1 2048 4194303 4192256 2G 83 Linux# to create a swap partition dev@dev:~$ sudo mkswap / dev/sdb1Setting up swapspace version 1, size = 2 GiB (2146430976 bytes) no label, UUID=d69621de-618a-4bea-9a96-b8e8b0d0ea40# to view the swap that is now in use in the system In order to compare with the added dev@dev:~$ swapon-sFilename Type Size Used Priority/dev/dm-1 partition 524284 0-partition, add the new partition to the system dev@dev:~$ sudo swapon / dev/sdb1#, you can see that the new swap partition has been added to the system. And the priority is lower than the original dev@dev:~$ swapon-sFilename Type Size Used Priority/dev/dm-1 partition 524284 0-1/dev/sdb1 partition 2096124 0-partition to ensure that our new swap partition will be loaded automatically after the system restart, we need to modify the / etc/fstab file dev@dev:~$ sudo sh-c 'echo "/ dev/sdb1 none swap sw 00" > > / etc/fstab'# to make sure the write is successful. The first item here is the swap partition of the original system, and the second is the dev@dev:~$ grep swap / etc/fstab/dev/mapper/dev--vg-swap_1 none swap sw 0 0/dev/sdb1 none swap sw 00 added swap file that we just added.

Adding swap files is much easier and less risky than partitioning operations.

# first create a new 512m file, which can be used as a swap file. The file path can be arbitrarily # fallocate this command depends on the file system. Some old file systems do not support this command, such as ext2,#. In this case, you can use dd to achieve the same effect: the difference between # sudo dd if=/dev/zero of=/mnt/512MiB.swap bs=1024 count=524288#fallocate and dd is that # fallocate declares so much first. Then the file system allocates the real physical disk space only when it is specifically used, that is, allocating a little bit, # and dd actually writes 512m of data to the physical disk space from the very beginning. # so fallocate is more convenient as a test, because you don't have to write any data at first, but you have to dev@dev:~$ sudo fallocate-l 512m / mnt/512MiB.swap# to modify the permission of the file. To prevent other users from mismanipulating this file, dev@dev:~$ sudo chmod 600 / mnt/512MiB.swap# is formatted as swap file dev@dev:~$ sudo mkswap / mnt/512MiB.swap# to add new files to the system dev@dev:~$ sudo swapon / mnt/512MiB.swap#. When you can see that the new swap file has been added to the system, the type is file#. Here you can see that due to the highest priority The first swap partition / dev/dm-1 has been used with 24Kdevdevdevavo $swapon-sFilename Type Size Used Priority/dev/dm-1 partition 524284 24-1/dev/sdb1 partition 2096124 0-2/mnt/512MiB.swap file 524284 0-partition as you can see from the output of the free command, adding swap partitions and files after the first two rounds # now the swap space of the system has become 3G (3144692K) dev@dev:~$ freetotal used free shared buff/cache availableMem: 500192 39112 9564 1996 451516 430820Swap: 3144692 24 314466 430820Swap also to ensure that our new swap file will be automatically loaded after the system restart, we need to modify the / etc/fstab file dev@dev:~$ sudo sh-c 'echo "/ mnt/512MiB.swap none swap sw 00" > > / etc/fstab' Note: not all file systems support the creation of swap files, such as btrfs Creating a swap file in the btrfs partition will fail. Cancel all swap

If, after careful consideration, it is determined that swap is no longer needed, all swap partitions and files can be removed from the system, the opposite of the above

# stop the swapdev@dev:~$ sudo swapoff-a#swapon-s command that all systems are using without any output. The free command shows that the swap space is 0, which means that swapoff is successful dev@dev:~$ swapon-sdev@dev:~$ freetotal used free shared buff/cache availableMem: 500192 35924 348888 2004 115380 433924Swap: 00 "of course we still need to modify / etc/fstab, otherwise after the next restart The system will re-mount the corresponding swap partition and file # use your favorite editor to delete the three lines related to swap in / etc/fstab (in this case, three lines, please adjust according to the actual situation) how to optimize swap performance?

How can you configure swap to make it perform better?

Try to use swap partitions. Relative to swap files, partitions must be contiguous physical disk space, while swap files may not be

Put the swap partition and the partition of the system on a different disk, so that it will not compete for the same disk bandwidth as the system disk.

If you have multiple disks, you can create a swap partition on each disk and set them to the same priority, so that the kernel will access these swap partitions evenly, with a performance equivalent to N times the original (where N is the number of disks).

But then again, if you visit swap frequently, it is useless to optimize swap. It is still several orders of magnitude lower than memory, and the performance is still severely degraded. If you do not visit swap frequently, what is the point of optimizing swap? So in fact, optimizing the performance of swap is of little practical significance, just take a look at it here.

Configure swappiness

Sometimes our desktop environment is indeed configured with plenty of memory, and also configured with swap space, at this time we want to minimize the use of swap space to avoid affecting system performance, Linux has long helped us take into account this situation, in the 2.6 kernel, added a parameter called swappiness, used to configure the urgent need to move data that is not commonly used in memory to swap. The range of values for this parameter is 0: 0100. 0 tells the kernel not to move memory data to swap as much as possible, that is, to do so only if necessary, while 100 tells the kernel to move infrequently accessed data in memory to swap whenever possible.

The default configuration of Ubuntu's desktop and server is 60 (which may change with the version). For the desktop environment, the response speed of the interface is directly related to the fluency of the system. If there is plenty of memory, you can set this value a little smaller, so that you can keep the data in memory as much as possible, thus waking up the background interface program will be faster. Ubuntu desktop recommends setting this value to 10. Of course, you can adjust this parameter arbitrarily according to the actual use of swap space until you are satisfied with the level. For servers, the main performance measure is the overall processing capacity, rather than the response speed of a specific time, and it may be better to use more memory to do I _ cache, so Ubuntu server recommends keeping the default value of 60.

View the value of swappiness in the current system

Dev@dev:~$ cat / proc/sys/vm/swappiness60 modifies the value of swappiness in the current system dev@dev:~$ sudo sysctl vm.swappiness=10vm.swappiness = 10devdevdevviso $cat / proc/sys/vm/swappiness10

The swappiness value modified by sysctl above will expire after the system reboot. If you want to continue to take effect after reboot, you need to modify the configuration file / etc/sysctl.conf, and change the following line to 10. If you can't find this line in the file, you can add this line at the end of the file.

Vm.swappiness=10 at this point, the study of "introduction and usage of swap under Linux" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report