Linux swap space, linuxswap swap

Source: Internet
Author: User

Linux swap space, linuxswap swap

Linux memory uses virtual memory to manage the entire memory. physical RAM physical memory and swap space are the total amount of virtual memory.

Use Cases of swap

Swap has two main functions:

1. When the system requires more memory space than the physical memory, the kernel will switch out the Memory Page swap with less memory, the physical memory is provided to the current application for fast operation.

2. Some memory pages that are initialized during application startup but are no longer used during subsequent application running, and the system will also switch these pages out to the swap space, to leave a physical memory page to cache other applications or disks.

 

This memory management policy in linux is mainly used to save physical memory and improve the execution speed of the current application. However, swap cannot be used as a means to expand memory, because swap reads and writes are disk io, which is much slower than physical memory io.

If the system frequently switches out memory pages to swap partitions, and then swap in swap partitions to the Memory Page, this indicates that the system is searching for idle memory to run multiple applications at the same time, that is to say, the current system task is busy, but the available memory is insufficient. At this time, the only way is to increase the physical memory.

Therefore, to determine whether the memory usage of a system has reached a bottleneck, we can evaluate it by observing the swap usage and the si so frequency.

 

Generate swap

Swap space has two forms: swap partition and swap file. In short, the read and write operations on it are disk operations.

1. Swap partitions

Swap partitions can be allocated when the operating system is installed, or you can enter the system and use fdisk to divide a swap partition.

For example, divide a/dev/sda5 partition on the disk and mark it as a swap partition.

Then run the mkswap command to create a swap partition File System on the partition:

Mkswap/dev/sda5

Finally, the swap partition is activated:

Swapon/dev/sda5

 

2. file exchange

The swap space can also be a file. You only need to use the dd command to create a file/home/swapfile with the allocated size on the disk.

Create a swap partition File System

Mkswap/home/swapfile

Finally, activate it.

Swapon/home/swapfile

 

Swap space allocation policy

There are different opinions on swap space allocation. Some say that the physical memory is twice the size, some say that the physical memory is the same size, and some say that the physical memory is half the size. These statements are correct in different scenarios. (Therefore, the examiner may not really understand swap either when taking an absolute statement as an interview question)

The following is a red hat distribution policy:

Swap shoshould equal 2x physical RAM for up to 2 GB of physical RAM, and then an additional 1x physical RAM for any amount above 2 GB, but never less than 32 MB.

 

So, if:

M = Amount of RAM in GB, and S = Amount of swap in GB, then

If M < 2S = M *2ElseS = M + 2


There is a pseudo-code of if else to calculate swap. The last sentence is as follows:

For systems with really large amounts of RAM (more than 32 GB) you can likely get away with a smaller swap partition (around 1x, or less, of physical RAM ).

Therefore, how to score the score depends on the specific application scenario. As long as the score is not outrageous, it is correct.

There is also a note for the Red Hat series:

Important

File systems and LVM2 volumes assigned as swap spaceCannotBe in use when being modified. For example, no system processes can be assigned the swap space, as well as no amount of swap shoshould be allocated and used by the kernel. UsefreeAndcat /proc/swapsCommands to verify how much and where swap is in use.

The best way to achieve swap space modifications is to boot your system in rescue mode, and then follow the instructions (for each scenario) in the remainder of this chapter. refer to the Red Hat Enterprise Linux Installation Guide for instructions on booting into rescue mode. when prompted to mount the file system, selectSkip.

 

The following is another swap allocation policy, which is suitable for most systems and scenarios:

A rule of thumb is as follows:

1) for a desktop system, use a swap space of double system memory, as it will allow you to run a large number of applications (Response of which may be idle and easily swapped), making more RAM available for the active applications;

2) for a server, have a smaller amount of swap available (say half of physical memory) so that you have some flexibility for swapping when needed, but monitor the amount of swap space used and upgrade your RAM if necessary;

3) for older desktop machines (with say only 128 MB), use as much swap space as you can spare, even up to 1 GB.

 

Swap Optimization

In linux 2.6, a new parameter is added to manage swap, which is calledSwappiness, Swappiness can have 0 to 100 values. Setting this parameter to a lower value will reduce memory swap and improve the responsiveness of some systems.

The higher the value, the more pages in the memory, the more swap out to the swap space. The lower the value, the more applications use the physical memory space. Therefore, to maximize the use of physical memory, we should minimize the value of swappiness and even set it to 0.

In linux, the value is set to 60 by default, and its value is changed temporarily:

Echo 10>/proc/sys/vm/swappiness

You can change the value permanently in the/etc/sysctl. conf file.Vm. swappinessParameters

 

 

Reference link:

Https://www.linux.com/news/software/applications/8208-all-about-linux-swap-space

Https://www.centos.org/docs/5/html/5.1/Deployment_Guide/s1-swap-what-is.html

Https://wiki.archlinux.org/index.php/Swap_ (% E7 % AE % 80% E4 % BD % 93% E4 % B8 % AD % E6 % 96% 87)

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.