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

Example Analysis of Linux swap Partition

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

Share

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

Linux swap partition example analysis, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

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 is dormant, 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 the physical memory is limited, but you want to run a program that consumes 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 the 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:

* * when swap:** is configured, 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). * * when swap:** is not configured, the OOM killer of the kernel is triggered. In the default configuration, memory-consuming processes are given priority 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 swap space with the same size as the memory. When you need to sleep, it is recommended to configure twice the size of the physical memory, but the maximum value is not more than twice the memory size. When the physical memory is greater than 1G and there is no need for hibernation, the recommended size is round (sqrt (RAM)), where RAM is the physical memory size. When you need to sleep, the recommended size is RAM+round (sqrt (RAM)), but the maximum value is not more than twice the memory size. If the swap space of twice the physical memory size 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 256 512 512 512 1024 1024 1024 2048 2048 physical memory (GB) does not need hibernation need hibernation maximum 1 12 2 2 1 3 4 3 2 5 6 4 2 6 8 5 2 7 10 6 2 8 12 8 3 11 16 12 3 15 24 16 4 20 32 24 5 29 48 32 6 38 64 64 8 72 128 128 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-m total 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, here will be the path to the partition, if the swap type is a file, here will be the file path Type:swap type, partition represents this is a swap partition, file represents the size of a swap file Size:swap, in k, where 524284 represents almost the size of 512MUsed: the size that has been used, where 0 indicates that Priority: priority has not been used, high priority swap will be given priority Swap with the same priority will be used evenly (round-robin algorithm), and the priority can be set to view the swap in/out in the system through the "swapon-p" command.

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 2procsMurwyMauwapMeikui-r b swpd free buff cache si so bi bo in cs us sy id wa st 0 70232 75620 7940 000000 111180 0 1 99 00 00 70232 75620 7940 209476 00 00 116 186 11 99 00 00 70228 75620 7940 209476 20 20 120 193 11 98 100 0 70228 75620 7940 209476 00 00 117 186 00 100 00 00 70228 75620 7940 209476 00 00 113 184 0 1 99 00

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 has already created a partition with fdisk. In this case, 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 # View the swap that is now in use on the system In order to compare dev@dev:~$ swapon-sFilename Type Size Used Priority/dev/dm-1 partition 524284 0-1 # 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-2 # to ensure that our new swap partition will be loaded automatically after the system restart The / etc/fstab file dev@dev:~$ sudo sh-c 'echo "/ dev/sdb1 none swap sw 00" > > / etc/fstab'# needs to be modified to make sure the write is successful. The first item here is the swap partition of the original system. 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 0 that we just added.

Add a swap file

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 permissions of the file. To prevent other users from mismanipulating this file, dev@dev:~$ sudo chmod / mnt/512MiB.swap # is formatted as a swap file dev@dev:~$ sudo mkswap / mnt/512MiB.swap # add a new file 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, and 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-3 # as you can see from the output of the free command, adding swap partitions and files after the first two rounds # now the switching space of the system has become 3G (3144692K) dev@dev:~$ free total used free shared buff/cache availableMem: 500192 39112 9564 1996 451516 430820Swap: 3144692 24 3144668 # also to ensure that our new swap files will be loaded automatically after the system restart The / etc/fstab file dev@dev:~$ sudo sh-c 'echo "/ mnt/512MiB.swap none swap sw 00" > > / etc/fstab' needs to be modified

Note: not all file systems support the creation of swap files, such as btrfs. Creating swap files 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 all swapdev@dev:~$ sudo swapoff-a # swapon-s commands that are in use without any output. The free command shows that the swap space is 0. Indicates that swapoff is successful dev@dev:~$ swapon-sdev@dev:~$ free total 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 # and delete the three lines related to swap in / etc/fstab using your favorite editor (in this case, please adjust them 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 continuous physical disk space, and swap files may not put swap partitions and system partitions on different disks, so that they will not compete with the system disk for the same disk's Igamo bandwidth. If you have multiple disks, you can create a swap partition on each disk and set them to the same priority. In this way, the kernel will evenly access these swap partitions, with 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 dev@dev:~$ cat / proc/sys/vm/swappiness60 of swappiness in the current system

Modify 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 what is Linux system Linux is a free to use and freely spread UNIX-like operating system, is a POSIX-based multi-user, multi-task, multi-threaded and multi-CPU operating system, using Linux can run major Unix tools, applications and network protocols.

This is the answer to the sample analysis question on the Linux swap partition. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

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

Development

Wechat

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

12
Report