Linux Swap partition summary, linuxswap Swap Partition

Source: Internet
Author: User
Tags andrew morton

Linux Swap partition summary, linuxswap Swap Partition

Swap partition Concept

What is Linux swap space? Let's take a look at the following two sections about Linux swap space in English:

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 shocould 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.

To improve read/write efficiency and speed, the Linux kernel caches files in the Memory. This part of the Memory is Cache Memory ). The Cache Memory will not be automatically released even after your program runs. This will cause you to frequently read and write files in Linux, and you will find that the available physical memory is reduced. When the system's physical memory is insufficient, you need to release a portion of the physical memory for use by 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 the Swap partition to the memory. In this way, the system always performs Swap switching when the physical memory is insufficient.

We have a lot of questions about Swap partitioning. If you can find out these questions, you will have a similar understanding of Swap. How do I view the Swap partition size? How should I set the Swap partition size? When will the system use Swap partitions? Can it be adjusted? How can I adjust the Swap partition size? What are the advantages and disadvantages of Swap partitioning? Is Swap partition necessary? Let me look at these questions one by one!

View the Swap partition size

Run the free command to check the size and usage of the Swap partition. The Swap size is 2015 MB. Currently, no Swap partition is used.

[root@DB-Server ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          1000        855        145          0         28        296
-/+ buffers/cache:        530        470
Swap:         2015          0       2015

In addition, we can also use the swapon command to view information about the current swap: for example, the swap space is swap partition, Swap size, usage, and other details.

[root@DB-Server ~]# swapon -s
Filename                                Type            Size    Used    Priority
/dev/sda3                               partition       2064344 0       -1
[root@DB-Server ~]# cat /proc/swaps
Filename                                Type            Size    Used    Priority
/dev/sda3                               partition       2064344 0       -1
[root@DB-Server ~]# 

Swap partition size settings

What is the optimal size of the system's Swap partition? For this question, we should say that there can be only one unified reference standard. We should also consider it based on the actual situation of the system and the load on the memory. The following settings are recommended in the official ORACLE documents, this is a reference based on the physical memory.

RAM

Swap Space

Up to 512 MB

2 times the size of RAM

Between 1024 MB and 2048 MB

The size of RAM 1.5 times

Between 2049 MB and 8192 MB

Equal to the size of RAM

More than 8192 MB 

The size of RAM 0.75 times

In addition, I can see the following recommendation settings in other blogs. Of course, I don't know how to get this standard. There is no way to check whether it is reasonable. It can be used as a reference.

For physical memory within 4 GB, SWAP is set to 2 times the memory.

4-8 GB physical memory. SWAP is equal to the memory size.

8-64 GB physical memory, SWAP is set to 8 GB.

-GB physical memory, SWAP is set to 16 GB.

The two standards are indeed at a loss. I once set up a large Swap partition on an ORACLE database server (64 gb ram) according to the official recommendation, but I found that this Swap is rarely used, it is actually a waste of disk space. Therefore, based on the actual situation of the system and the memory load, you should set it to 8 GB according to the second reference standard. Of course, this is just some personal cognition.

Release Swap partition Space

 

[root@testlnx ~]# free -m
             total       used       free     shared    buffers     cached
Mem:         64556      55368       9188          0        926      51405
-/+ buffers/cache:       3036      61520
Swap:        65535         13      65522
[root@testlnx ~]# swapon -s
Filename                                Type            Size    Used    Priority
/dev/mapper/VolGroup00-LogVol01         partition       67108856        14204   -1

Disable swap partition with swapoff

[Root @ testlnx ~] # Swapoff/dev/mapper/VolGroup00-LogVol01

Enable swap partition using swaponIn this case, check the usage of the SWAp partition and you will find that used is 0

[root@testlnx ~]# swapon /dev/mapper/VolGroup00-LogVol01
[root@testlnx ~]# free -m
             total       used       free     shared    buffers     cached
Mem:         64556      55385       9171          0        926      51406
-/+ buffers/cache:       3052      61504
Swap:        65535          0      65535
[root@testlnx ~]#

When to use Swap partition Space

Under what conditions or conditions will the system use the Swap partition space? In fact, Linux uses the swappiness parameter to control it. Complex algorithms are also involved.

This parameter can be set to 0-100 to control the usage of the system swap. High values give priority to system performance and actively convert processes into physical memory when they are not active. Low values give priority to interaction and avoid converting processes to physical memory and reduce response latency. The default value is 60. Note: This is only a weight value, not a percentage value, involving complex algorithms in the system kernel. For details about this parameter, refer to the [reprint] in this article to adjust the virtual memory. The following 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 aggressive. 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 100 may affect the overall performance, whereas setting it lower (even 0) may improve interactivity (by decreasing response latency.

There are two ways to temporarily modify the swappiness parameter, which will expire after the system is restarted.

Method 1:
[root@DB-Server ~]# more /proc/sys/vm/swappiness
60
[root@DB-Server ~]# echo 10 > /proc/sys/vm/swappiness
[root@DB-Server ~]# more /proc/sys/vm/swappiness
10
 
Method 2
[root@DB-Server ~]#sysctl vm.swappiness=10

To permanently modify the swappiness parameter, modify the vm. swappiness value in the configuration file/etc/sysctl. conf and restart the system.

echo 'vm.swappiness=10' >>/etc/sysctl.conf

If someone asks if the physical memory is used to a certain percentage before using the Swap space, it clearly tells you that it is not such an algorithm, as shown below, there is only 8 MB of physical memory in a timely manner, however, Swap space is still not used. In another example, there is still 19 GB of physical memory, and a little Swap space is used.

In addition, adjust the/proc/sys/vm/swappiness parameter. If you are not sure about it, do not adjust this Kernel Parameter. This parameter meets the optimal value in most cases.

Impact of Swap partition on Performance

We know that Linux can use a regular file or independent partition in the file system as the Swap space, which is relatively faster. However, compared with RAM, the performance of Swap partitions is still inferior to that of physical memory. Currently, RAM on the server is basically adequate. Can you consider discarding Swap partitions, do I not need to retain Swap partitions? This is actually one of my questions. In this article, What Is a Linux SWAP Partition, And What Does It Do? In the blog, the author gives the advantages and disadvantages of the swap space.

Advantages:

Disadvantages:

In fact, the swap partition can be summarized as follows:

First, when the physical memory is insufficient to support the operation of the system and applications (processes), the Swap partition can be used as a temporary storage page for memory with low usage, allocate free memory to applications (processes) that are in urgent need. It is similar to the UPS system in the data center. Although it is not required under normal circumstances, Swap partition will play a key role in exceptional circumstances.

Second, even if your server has enough physical memory, some programs will transfer the paging content that is rarely used during their initialization to the swap space, in this way, physical memory space is provided. For applications (processes) with a probability of Memory leakage, Swap partitions are even more important, because no one wants to see the system crash due to insufficient physical memory.

Finally, many individual users are running a Linux system on Linux, some even PC virtual machines, which may be commonly used to sleep (Hibernate ), in this case, we recommend dividing Swap partitions.

In fact, a small amount of Swap space using Swap will not affect the performance. Only when the RAM resources encounter bottlenecks or memory leaks, frequent and extensive use of Swap partitions will cause serious performance problems. In addition, the frequent use of Swap partitions may also cause the kswapd0 process (which is responsible for page change in virtual memory management) to consume a large amount of CPU resources, resulting in soaring CPU usage.

I think about the advantages and disadvantages of Swap partition and whether to discard it. I think about it with a little bad taste: two organs, the appendix and the tonsils of a person. It is also a debate whether to remove the appendix or tonsils. In addition, Linux can run normally without Swap partitions (this problem has been mentioned)

Adjust the Swap partition size

As shown in the following test case, the Swap partition size is 65535 mb. I want to adjust the Swap partition size to 8 GB now. Let's take a look at the specific operation.

1: View Swap usage and related information

[root@getlnx14uat ~]# swapon -s
Filename                                Type            Size    Used    Priority
/dev/mapper/VolGroup00-LogVol01         partition       67108856        878880  -1
[root@getlnx14uat ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          3957       3920         36          0         39       3055
-/+ buffers/cache:        825       3132
Swap:        65535        858      64677

2: Disable Swap Partition

[root@getlnx14uat ~]# swapoff /dev/mapper/VolGroup00-LogVol01
[root@getlnx14uat ~]# swapon -s
Filename                                Type            Size    Used    Priority

3: Reduce the Swap partition size. To increase the Swap partition size, you need to expand the logical volume of the swap partition in use. Here, you can 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 the swap Partition

[root@getlnx14uat ~]# mkswap /dev/mapper/VolGroup00-LogVol01
Setting up swapspace version 1, size = 8589930 kB

5: Start the swap partition and add it to/etc/fstab for automatic mounting.

[root@getlnx14uat ~]# swapon -s
Filename                                Type            Size    Used    Priority
[root@getlnx14uat ~]# swapon /dev/mapper/VolGroup00-LogVol01
[root@getlnx14uat ~]# swapon -s
Filename                                Type            Size    Used    Priority
/dev/mapper/VolGroup00-LogVol01         partition       8388600 0       -1

References:

Https://wiki.archlinux.org/index.php/swap

Http://blog.csdn.net/tianlesoftware/article/details/8741873

Http://www.makeuseof.com/tag/swap-partition/

Related Article

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.