Swap swap space for Linux systems

Source: Internet
Author: User
Tags andrew morton

has been using Ubuntu for almost 1 years, Recently reload 16.04, every day to around 5 o'clock in the afternoon, will find swap space has hundreds of trillion write, system memory 8G, hard disk is SSD,I5 processor, configuration mid-range, also did not start what large software, is to use idea to do development, although no impact, but the spirit of a knowledge of the heart, Google, the first article is "All about Linux swap space", the tone is very big, direct translation.

Linux will randomly store RAM as a memory page. The switching technique is to copy a page of memory to the swap space on a pre-set hard disk, freeing the page to occupy memory. The sum of physical memory and swap space is the amount of virtual memory that can be provided.
There are two reasons to prove that switching technology is important. First, when the system requires more memory than physical memory, the system kernel can write less-used memory pages to swap space and use the free memory for the current application (process). Second, a memory page used by an application when it is started, may be used only at initialization time, and then no longer used, and the operating system can write this part of the memory page to swap space, and use the free memory for other applications or as a disk cache.
However, exchange technology also has a negative effect. Hard disk reads and writes slow relative to memory. The read and write speed of memory can be measured in nanoseconds, but the speed of the hard disk can only reach the millisecond level, and the speed of accessing the hard disk is thousands times slower than accessing the memory. The more exchanges that occur, the slower the system runs. Sometimes there will be excessive switching or memory page write-out jitter occurs frequently, because the system is to ensure that the application is running properly, but also to find free memory. In this case, it can only be solved by adding RAM.
Linux has two forms of swap space: Swap partitions and swap files. The swap partition is a standalone hard disk with no files or content. The swap file is a special file in the file system, independent of the system and data files.
You can use swapon -s the command to view the swap space, with the following output:

Filename  Type       Size       Used Priority/dev/sda5 partition  859436  0       -1

Each row is a swap space that the system is using. Here the ' Type ' field indicates that the swap space is a partition instead of a file, through ' Filename ' you can know that the swap partition is disk SDA5. The ' Size ' field disk size, in kilobytes, is the ' used ' field that indicates how much swap space is being used. The ' Priority ' field indicates the use of the swap space of the Linux system. There is an important feature that, if you mount two (or more) swap spaces with the same priority (preferably two different devices) in a Linux system, Linux will be used interchangeably to improve switching performance.

Swap partition

To add an additional swap partition to the system, first you need to prepare one. The first step is to make sure that the partition is marked as a swap partition, and the second step is to format the swap file system. To mark a partition as a swap partition, run with root privileges:

-l /dev/hdb

Replace '/dev/hdb ' with the disk for your swap partition. The output is similar to:

Device Boot    Start      End           Blocks  Id      System/dev/hdb1       2328    2434    859446  82      Linux swap / Solaris

If the partition is not marked as a swap partition, you need to declare it using the command fdisk and the parameter T. Be careful when working with partitions, you absolutely do not want to delete important partitions or change the identity of the system partition. Data on the swap partition is lost, so each change requires multiple confirmations. It is also important to note that Solaris uses the same ID as the Linux swap space, so be careful not to kill the Solaris partition.
If the partition is already marked as a swap partition, you need to run the command with root privileges mkswap :

mkswap /dev/hdb1

If you run without errors, your swap space will begin to be used. Activate now:

swapon /dev/hdb1

It swapon -s is possible to verify that it is running. In order to mount the swap space automatically when the system starts, you need to add some column configuration to the '/etc/fstab ' file, swap space is a special file system, and many parameters are not available. Like what:

/dev/hdb1       none    swap    sw      0       0

Check that your swap space is not restarted, you can run the swapoff -a command and then run swapon -a it through the swapon -s check.

Swap files

Like swap partitions, Linux also supports the use of swap files, which you can create, prepare, and mount in a swap partition. The benefit of exchanging files is that you do not need to find an empty partition or add additional swap partition disks.
ddcreate an empty file using the command. Create a 1G file, such as:

if=/dev/zeroof=/swapfile bs=1024 count=1048576

'/swapfile ' is the name of the swap file, ' count ' of 1048576 is the file size, in kilobytes.
Prepare the swap file using mkswap the command, similar to preparing the partition, but this time using the same swap file:

mkswap /swapfile

Similarly, mount the swap file using the swapon command:

swapon /swapfile

Enter the following in '/etc/fstab ':

/swapfile       none    swap    sw      0       0
Size of swap space

If you have a lot of memory, there may be no swap space and the system can run well. But if the physical memory consumes light, the system crashes because it has no other mitigation, so it is better to provide a swap space, not to mention that the disk is much cheaper than memory.
The key question is how big is the memory space? The older UNIX-like operating system requires that the swap space be two to three times times the physical memory. There's no need for extensions like Linux today, but if you configure them, they'll use them. The important principles are as follows:

    1. For desktop systems, using twice times the swap space of system memory, you will be able to run a large number of applications (which may have a lot of idle) and make more RAM available for the main application;
    2. For servers, use a small amount of swap space (usually half of the physical memory), so you can monitor the size of the swap space to see if you need to increase RAM;
    3. For older desktops, use as much swap space as possible

A new kernel parameter ' swappiness ' is added to the Linux 2.6 kernel, which allows administrators to modify the Linux switching mode. The parameter values are from 0 to 100. In essence, the larger the value, the more memory pages will be exchanged; the smaller the value, the more applications reside in memory, and the swap space is idle. Kernel maintainer Andrew Morton said that he set the Swappiness value in his desktop to 100, saying: "My point is that it is wrong to reduce the swap through kernel parameters." You don't need hundreds of trillion of useless apps to occupy memory. Put it on the disk and leave the memory to something useful. ”
Morton's idea has a loophole, if the memory exchange is too fast, the application response will drop, because when the application window is clicked, the application is reading into memory from the swap space, it will feel very slow to run.
The default ' swappiness ' value is 60. You can use the root command to adjust the parameters (action to restart):

50/proc/sys/vm/swappiness

If you need to make the parameters permanent, you need to modify the ' vm.swappiness ' parameter in '/etc/sysctl.conf '.

Conclusion

Managing swap space is an important aspect of system management. With good planning and reasonable use of exchange technology can have many benefits. Don't be afraid to experiment, and always monitor your system to make sure you get the results you need.

Written in the last

For now, both the memory and SSD are starting to price down, and basically it's easy to save the machine to 8G (RAM) +120g (SSD), so that the role of swap space is greatly weakened in terms of the individual user's desktop system, but as stated above, if there is no swap space, memory consumption is The machine is dead. Because SSDs do not recommend dividing multiple partitions, it is better to use swap file, and you can build several swap file files to improve switching performance.

Original link: All About Linux swap space
Translation: Eitherpotentially flying
Link: Swap swap space for Linux systems

Swap swap space for Linux systems

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.