In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Reproduced from Xiaoxiang hermit
Original link: http://www.cnblogs.com/kerrycode/p/5246383.html
What is Linux swap space? Let's first take a look at the following two paragraphs of English introduction to Linux swap space:
Linux divides its physical RAM (random access memory) into chucks of memory called pages. Swapping is the process whereby a page of memory is copied to the preconfigured space on the hard disk, called swap space, to free up that page of memory. The combined sizes of the physical memory and the swap space is the amount of virtual memory available.
Swap space in Linux is used when the amount of physical memory (RAM) is full. If the system needs more memory resources and the RAM is full, inactive pages in memory are moved to the swap space. While swap space can help machines with a small amount of RAM, it should not be considered a replacement for more RAM. Swap space is located on hard drives, which have a slower access time than physical memory.Swap space can be a dedicated swap partition (recommended), a swap file, or a combination of swap partitions and swap files.
In order to improve the efficiency and speed of reading and writing, the Linux kernel will cache files in memory, which is called Cache Memory (cache memory). Even after your program is finished, Cache Memory will not be released automatically. This will cause you to find less physical memory available after frequently reading and writing files in your Linux system. When the physical memory of the system is insufficient, a portion of the physical memory needs to be released for use by the currently running program. The free space may come from programs that have been inactive for a long time, and the free space is temporarily saved in Swap space, and when those programs are about to run, the saved data is recovered from the Swap partition to memory. In this way, the system does not swap Swap until there is not enough physical memory.
In fact, we have a lot of questions about Swap partitions, and if you can figure them out, then your understanding of Swap is almost enough. How do I view the Swap partition size? How should the Swap partition size be set? When will the system use Swap partitions? Can it be adjusted? How do I resize the Swap partition? What are the advantages and disadvantages of Swap partition and what should be paid attention to? Is Swap partitioning necessary? Then let me take a look at these questions one by one.
View Swap partition siz
To check the size and usage of the Swap partition, you can generally use the free command. As shown below, the Swap size is 2015m. Currently, no Swap partition is used.
[root@DB-Server] # free-m total used free shared buffers cachedMem: 1000 855 145 0 28 296 buffers/cache: 530 470Swap: 2015 0 2015
In addition, we can also use the swapon command to view the current swap-related information: for example, the swap space is swap partition,Swap size, usage and other details
[root@DB-Server ~] # swapon-sFilename Type Size Used Priority/dev/sda3 partition 2064344 0-1 [root@DB-Server ~] # cat / proc/swapsFilename Type Size Used Priority/dev/sda3 Partition 2064344 0-1 [root@DB-Server ~] #
Swap partition size settin
What is the optimal Swap partition size setting for the system? On this issue, it should be said that there can only be a unified reference standard, and it should be considered comprehensively according to the actual situation of the system and the load of memory. For example, the official documents of ORACLE recommend the following settings, which are based on physical memory as a reference.
RAM
Swap Space
Up to 512 MB
2 times the size of RAM
Between 1024 MB and 2048 MB
1.5 times the size of RAM
Between 2049 MB and 8192 MB
Equal to the size of RAM
More than 8192 MB
0.75 times the size of RAM
In addition, I saw the following recommended setting in other blogs, but of course I don't know how it got this standard. There is no way to prove whether it is reasonable. It can be used as a reference.
For physical memory within 4G, SWAP is set to 2 times the amount of memory.
4-8 gigabytes of physical memory, SWAP equal to memory size.
8-64 GB of physical memory, SWAP set to 8 GB.
64-256g physical memory, SWAP set to 16G.
The upper and lower standards are indeed very confusing. I once set up a large Swap partition on an ORACLE database server (64G RAM) as officially recommended, but I found that this Swap was rarely used and a waste of disk space. Therefore, according to the actual situation of the system and the load of memory, it should be set to 8G according to the second reference standard. Of course, this is just some personal perception.
Free up Swap zone SPAC
[root@testlnx] # free-m total used free shared buffers cachedMem: 64556 55368 9188 0926 51405 buffers/cache + buffers/cache: 3036 61520Swap: 65535 13 65522 [root@testlnx] # swapon-sFilename Type Size Used Priority/dev/mapper/VolGroup00-LogVol01 partition 67108856 14204-1
Turn off the swap partition using swapoff
[root@testlnx ~] # swapoff / dev/mapper/VolGroup00-LogVol01
Use swapon to enable swap partition. Check the usage of swap partition at this time, and you will find that used is 0.
[root@testlnx ~] # swapon / dev/mapper/VolGroup00-LogVol01 [root@testlnx ~] # free-m total used free shared buffers cachedMem: 64556 55385 9171 0926 51406 Muhammad + buffers/cache: 3052 61504Swap: 65535 65535 [root@testlnx] #
When to use Swap Partition Space
Under what circumstances or conditions will the system use Swap partitioned space? In fact, Linux is controlled by a parameter swappiness. Of course, complex algorithms are also involved.
The value of this parameter can be 0-100, which controls the use of the system swap. High values can give priority to system performance and proactively convert a process out of physical memory when it is inactive. Low values give priority to interactivity and minimize the conversion of processes to physical memory and reduce response latency. The default value is 60. Note: this is only a weight, not a percentage value, involving complex algorithms in the kernel of the system. For this parameter, please refer to this article [reprint] to adjust virtual memory. I won't go into too much detail here. Here is some information about swappiness
The Linux 2.6 kernel added a new kernel parameter called swappiness to let administrators tweak the way Linux swaps. It is a number from 0 to 100. In essence, higher values lead to more pages being swapped, and lower values lead to more applications being kept in memory, even if they are idle. Kernel maintainer Andrew Morton has said that he runs his desktop machines with a swappiness of 100, stating that "My point is that decreasing the tendency of the kernel to swap stuff out is wrong. You really don't want hundreds of megabytes of BloatyApp's untouched memory floating about in the machine. Get it out on the disk, use the memory for something useful."
Swappiness is a property of the Linux kernel that changes the balance between swapping out runtime memory, as opposed to dropping pages from the system page cache. Swappiness can be set to values between 0 and 100 inclusive. A low value means the kernel will try to avoid swapping as much as possible where a higher value instead will make the kernel aggressively try to use swap space. The default value is 60, and for most desktop systems, setting it to 100may affect the overall performance, whereas setting it lower (even 0) may improve interactivity (by decreasing response latency.
There are two ways to modify swappiness parameters temporarily, and the system fails after reboot.
Method 1: [root@DB-Server ~] # more / proc/sys/vm/swappiness60 [root@DB-Server ~] # echo 10 > / proc/sys/vm/swappiness [root@DB-Server ~] # more / proc/sys/vm/swappiness10 method 2 [root@DB-Server ~] # sysctl vm.swappiness=10
The way to permanently modify the swappiness parameter is to change the value of vm.swappiness in the configuration file / etc/sysctl.conf, and then restart the system
Echo 'vm.swappiness=10' > > / etc/sysctl.conf
If someone asks whether Swap swap space will not be used until a certain percentage of physical memory is used, you can clearly tell you that it is not such an algorithm. As shown in the screenshot below, there is only 8m of physical memory left, but there is still no Swap swap space. In another example, 19GB of physical memory is left, which uses a little bit of Swap swap space.
In addition, adjust the / proc/sys/vm/swappiness parameter, if you are not absolutely sure, do not casually adjust this kernel parameter, this parameter conforms to an optimal value in most cases.
The effect of Swap switching Partition on performance
We know that Linux can use a regular file or a separate partition in the file system as the Swap swap space, which is relatively faster. However, compared with RAM, the performance of Swap swap partition is still not as good as physical memory. RAM on current servers is basically quite sufficient, so can you consider abandoning Swap swap partition and whether it is not necessary to keep Swap swap partition? This is actually one of my questions. In this What Is a Linux SWAP Partition, And What Does It Do? In the blog, the author gives the advantages and disadvantages of swap swap space.
Advantages:
Provides overflow space when your memory fills up completelyCan move rarely-needed items away from your high-speed memoryAllows you to hibernate
Disadvantages:
Takes up space on your hard drive as SWAP partitions do not resize dynamicallyCan increase wear and tear to your hard driveDoes not necessarily improve performance (see below)
In fact, the reserved swap partition can be summarized as follows:
First of all, when there is not enough physical memory to support the operation of the system and applications (processes), this Swap swap partition can be used to temporarily store underutilized memory pages, freeing up memory for much-needed applications (processes) to use. It is somewhat similar to the UPS system in the computer room, although it is not needed under normal circumstances, but under abnormal circumstances, Swap exchange partition will still play a key role.
Second, even if your server has enough physical memory, some programs will transfer the rarely used paging content of their initialization to swap space to make way for physical memory space. For applications (processes) that have a chance of memory leaks, Swap swapping is even more important because no one wants to see a system crash due to insufficient physical memory.
Finally, many individual users are running Linux systems on virtual machines using Linux and some even PC, which may be commonly used to Hibernate. In this case, it is also recommended to divide Swap swap partitions.
In fact, a small amount of Swap swap space will not affect performance. Only when RAM resources have bottlenecks or memory leaks and process anomalies lead to frequent and heavy use of swap partitions will cause serious performance problems. In addition, the frequent use of Swap to swap partitions will also cause kswapd0 processes (virtual memory management, paging) to consume a lot of CPU resources, causing CPU to soar.
About the pros and cons of Swap zoning and whether it should be abandoned, I thought of this with a bit of unsavory interest: the two organs of the human body, the appendix and tonsils. There is also a debate about whether to remove an appendix or tonsil. In addition, in fact, there is no need for Swap to swap partitions, Linux can also operate normally (this problem has been mentioned)
Resize the Swap partition
As shown in the following test case, the Swap partition size is 65535m. Now I want to adjust the Swap partition to 8G, so let's take a look at the specific operation.
1: view the usage of Swap and related information
[root@getlnx14uat] # swapon-sFilename Type Size Used Priority/dev/mapper/VolGroup00-LogVol01 partition 67108856 878880-1 [root@getlnx14uat ~] # free-m total used free shared buffers cachedMem: 3957 3920 36 0 39 3055 buffers/cache + buffers/cache: 825 3132Swap: 65535 858 64677
2: close the Swap swap partition
[root@getlnx14uat ~] # swapoff / dev/mapper/VolGroup00-LogVol01 [root@getlnx14uat ~] # swapon-sFilename Type Size Used Priority
3: here is to reduce the size of the Swap partition, if you are increasing the size of the Swap partition, then you need to expand the logical volume of the swap partition you are using, here use the lvreduce command to shrink the logical volume.
[root@getlnx14uat] # lvreduce-L 8G / dev/mapper/VolGroup00-LogVol01 WARNING: Reducing active logical volume to 8.00 GB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce LogVol01? [y/n]: y Reducing logical volume LogVol01 to 8.00 GB Logical volume LogVol01 successfully resized
4: format swap partition
[root@getlnx14uat ~] # mkswap / dev/mapper/VolGroup00-LogVol01Setting up swapspace version 1, size = 8589930 kB
5: start the swap partition and add it to / etc/fstab auto mount
[root@getlnx14uat] # swapon-sFilename Type Size Used Priority [root@getlnx14uat ~] # swapon / dev/mapper/VolGroup00-LogVol01 [root@getlnx14uat ~] # swapon-sFilename Type Size Used Priority/dev/mapper/VolGroup00-LogVol01 partition 8388600 0-1
Reference:
Https://wiki.archlinux.org/index.php/swap
Http://blog.csdn.net/tianlesoftware/article/details/8741873
Http://www.makeuseof.com/tag/swap-partition/
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.