Creation and tuning of Linux swap partitions

Source: Internet
Author: User

Overview

This article provides a brief introduction to swap and how to set up or adjust the swap partition after the system has been installed (in operation).

The demo environment in this article is:

650) this.width=650; "style=" WIDTH:500PX;HEIGHT:119PX; "title=" "src=" Http://i.imgur.com/fiBlXW8.png "alt=" Fiblxw8.png "border=" 0 "height=" 119 "hspace=" 0 "vspace=" 0 "width="/> "


What is swap space?

Swap partition, or swap space. It divides a section of disk space independently, when the physical memory is not enough, the system first put the temporary unused data in the physical memory into the swap space, freeing up the memory space to ensure the operation of the program, when the swap stored in the page is accessed, the system will reload it into physical memory to run. There is a concept of swap partitioning in Windows, Unix, and Linux systems.


How do I allocate swap space?

As a complement to the physical memory of the system, the operation of the swap partition is the operation of disk I/O, and we should partition the interactive partitions as much as possible with higher I/O drives. The priority is solid-state drives, and for a mechanical hard drive, the front disk is faster than the back, so priority is given to dividing the swap by the front sector.

In addition, the number of swap partitions has a significant impact on performance. If there are multiple swap zones, the swap space is allocated in a rotating manner to all swaps, which greatly balances the IO load and speeds up swap. If there is only one swap area, all switching operations will make the swap area very busy, leaving the system most of the time in a waiting state and inefficient.


How much swap space is allocated?

Allocating too much swap space wastes disk space, and there is too little swap space for the system to get an error. If the system is running out of physical memory, the system will run slowly, but still be able to run, and if the swap space is exhausted, the system will have an error. For example, the Web server can derive multiple service processes (or threads) depending on the number of requests, and if the swap space is exhausted, the service process cannot start, and the "application is out of memory" error usually occurs, causing the deadlock of the service process to be severe. Therefore, the allocation of swap space is very important.

through the search Red Hat Enterprise Linux 7 Installation Guide , see Red Hat's official recommended distribution scheme:

650) this.width=650; "style=" width:700px;height:225px; "src=" http://s3.51cto.com/wyfs02/M01/86/97/ Wkiom1fe39rsc2meaadyutbjzsw690.png "title=" Image 20160829104022.png "alt=" Wkiom1fe39rsc2meaadyutbjzsw690.png " Border= "0" height= "225" hspace= "0" vspace= "0" width= "/>"

Hibernation (hibernation) should not be turned on in the production environment, so there is no need to consider the third column, namely:

    • When memory is less than 2GB, the recommended swap size is twice times memory;

    • Memory 2 ~ 8GB, the recommended swap size is equal to the memory size;

    • When memory 8 ~64GB, the recommended swap size is 0.5 of memory;

    • When the memory is greater than 64GB, the server's workload configuration is considered.

If your system resources allow, increasing the swap space may result in better performance.


How do I view swap space?

Linux, the system provides three ways to view the partition of the system's swap:



      • Cat/proc/swaps

      • Swapon-s

      • Free

650) this.width=650; "style=" width:700px;height:216px; "src=" http://s2.51cto.com/wyfs02/M01/86/97/ Wkiom1fe5g7bhxc5aaa45flrdz8062.png "title=" Image 20160830094846.png "alt=" Wkiom1fe5g7bhxc5aaa45flrdz8062.png " Border= "0" height= "216" hspace= "0" vspace= "0" width= "/>"


How do I divide and increase swap space?

We all know how to allocate a swap partition when installing a Linux system, and how to set up or adjust the swap partition when the system is installed (in operation).

After the Linux system is installed, the basic settings for the swap partition include:

    • Create a swap partition or file

    • Write a special signature using Mkswap

    • Add the appropriate entry in the/etc/fstab file

    • Activating swap space with swapon-a

The operation of the swap partition must be rooted, and the procedure should be handled with caution.

To avoid unnecessary problems, we stop all swap partitions first :

[Email protected] ~]# >>swapoff-a #停止所有的swap分区

650) this.width=650; "style=" width:700px;height:121px; "src=" http://s2.51cto.com/wyfs02/M02/86/98/ Wkiom1fe9algbccxaaajf_qjxuq455.png "title=" Image 20160830104744.png "alt=" Wkiom1fe9algbccxaaajf_qjxuq455.png " Border= "0" height= "121" hspace= "0" vspace= "0" width= "/>"

1. Create a swap partition or file

There are two ways to create a swap: A. Create a new disk partition; B. Use a file as a swap partition.

A. Create a new disk partition:

With the Fdisk command (example: # Fdisk/dev/sdb), create a new size 2GB partition, use the "T" command in Fdisk to adjust the partition type to "a" (Linux swap type), and finally W to save the Fdisk operation.

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/86/99/wKioL1fFDXqB0uvFAACFp1IYgGY455.png "title=" Image 20160830123606.png "alt=" Wkiol1ffdxqb0uvfaacfp1iyggy455.png "/>B. Use a file as a swap partition:

Create a partition file with the DD command, and the file size can be set by specifying options. (bs*count= file size)

[Email protected] ~]# >> dd If=/dev/zero of=/root/swap bs=1m count=2048

650) this.width=650; "style=" width:700px;height:121px; "src=" http://s3.51cto.com/wyfs02/M02/86/98/ Wkiom1fe-blxrax0aaafdk7dnt4775.png "title=" Image 20160830111229.png "alt=" Wkiom1fe-blxrax0aaafdk7dnt4775.png " Border= "0" height= "121" hspace= "0" vspace= "0" width= "/>"

2. Write a special signature using MKSWAP (format swap partition)

Mkswap is a command specifically designed to set up a Linux swap partition, and the command format is mkswap [options] device [size]

650) this.width=650; "style=" width:700px;height:140px; "src=" http://s1.51cto.com/wyfs02/M01/86/98/wKiom1fE-qvgBf_ Raaapwlpcigi168.png "title=" Image 20160830111633.png "alt=" wkiom1fe-qvgbf_raaapwlpcigi168.png "border=" 0 "height=" "hspace=" 0 "vspace=" 0 "width="/>

3. Add the appropriate entry in the/etc/fstab file

650) this.width=650; "style=" width:700px;height:224px; "src=" http://s1.51cto.com/wyfs02/M00/86/98/wKiom1fE_ Gnbtzcqaabfleyetju131.png "title=" Image 20160830112920.png "alt=" wkiom1fe_gnbtzcqaabfleyetju131.png "border=" 0 " height= "224" hspace= "0" vspace= "0" width= "/>"

4. Activating swap space with Swapon-a

650) this.width=650; "style=" width:700px;height:185px; "src=" http://s3.51cto.com/wyfs02/M02/86/98/ Wkiol1ffbszwqwehaaa1emyhkm8665.png "title=" Image 20160830120120.png "alt=" Wkiol1ffbszwqwehaaa1emyhkm8665.png " Border= "0" height= "185" hspace= "0" vspace= "0" width= "/>"

Multiple swap partitions are prioritized (priority), with large values, which are preferred. We can adjust the priority of swap with the-p option of swapon to optimize swap performance: Distributed storage, high performance disk storage.

It is important to note that after the modification, you need to swapoff disable and then swapon to mount again.


Concluding remarks

Although the swap partition can be used as "virtual" memory, it is much slower than physical memory, so if the need for faster speed, and can not expect to swap, the best way is still to increase physical memory. The swap partition is only a temporary solution.


This article is from the "Zhaoqi Learn to Linux" blog, so be sure to keep this source http://zhaoqifly.blog.51cto.com/3500118/1844248

Creation and tuning of Linux swap partitions

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.