How do I set up a Linux swap partition? Method Tutorials

Source: Internet
Author: User

What is swap?
Swap, the swap area, how many people cared about it, in addition to installing Linux? In fact, swap adjustments are critical to the performance of Linux servers, especially Web servers. By adjusting swap, you can sometimes bypass system performance bottlenecks and save on system upgrade costs.
The principle of swap is a more complex issue that needs a lot of space to illustrate. Here is a brief introduction to the details of the swap implementation in a future article.
As we all know, the modern operating system has realized the "virtual memory" this technology, not only in the function of the physical memory limit, so that the program can manipulate more than the actual physical memory space, more importantly, "virtual memory" is to isolate each process of the security network, so that each process is not affected by other programs interference.
The function of swap space can be simply described as: when the physical memory of the system is not enough, it is necessary to release some of the space in physical memory for use by the currently running program. The freed space may come from programs that have not been manipulated for a long time, and the freed space is temporarily saved in the swap space until those programs run, recovering the saved data from the swap into memory. In this way, the system always does swap swap when physical memory is not enough.
Computer users often encounter this phenomenon. For example, when using a Windows system, you can run multiple programs at the same time, and when you switch to a program that has been ignored for a long time, you hear the hard drive "rushing" straight. This is because the memory of the program is "stolen" by those programs that are running frequently and placed in the swap area. Therefore, once the program is placed on the front end, it will retrieve its own data from the swap area, put it in memory, and then run.
It is important to note that not all data exchanged from physical memory will be put into swap (if so, the swap will be overwhelmed), and a significant portion of the data is exchanged directly to the file system. For example, some programs will open some files to read and write to the file (in fact, each program to open at least one file, that is, running the program itself), when the need to swap out the memory space of these programs, there is no need to put the file part of the data into the swap space, and can directly put it into the file. If it is a read file operation, then the memory data is released directly, do not need to swap out, because the next time you need to recover from the file system directly, if you are writing files, only need to save the changed data to a file in order to recover. However, the data for objects generated with malloc and the new function are different, they require swap space because they do not have a corresponding "reserve" file in the file system and are therefore referred to as "anonymous" (Anonymous) memory data. This type of data also includes some state and variable data in the stack. So the swap space is the swap space for "anonymous" data.
How to set the swap partition size
I remember someone who commented on the size of the swap partition: "As long as you're not afraid to waste a hard disk, the bigger the better, because the Linux kernel won't move the swap until the physical memory is completely exhausted."
But according to my experience, this may not be the case. Too much swap space can cause kernel to think that there is a huge amount of memory space in which you want to capture data in memory, causing the kernel to always be doing memories swap and slowing down system response times.
To be honest, 1G RAM if do not run peer-to kind of things, that set a 256MB is enough, there are 2G words even set not set.
In addition, if it is true because a software does require a huge amount of memory space to operate, it has to be on the swap, but for the best performance scattered on multiple physical hard disk (equal to the raid effect)!
In fact, how to set the size of the swap partition is the most able to check the level of a Linux system administrator test, swap in the end how to set it? I think so: first of all we need to understand which programs the server is running, how much memory they occupy, and after the exact check, the size of the swap partition can be determined as follows:
(Memory size + swap partition size) * 80% or 70% = The total amount of memory that the program needs to occupy
Swap partitioning is also useful during program testing, such as the ability of an administrator to monitor system memory for leaks through the use of a swap partition, as well as providing a good traffic peak buffer for applications such as Web projects. A Linux system administrator should be able to monitor the use of the swap partition, the system, the program has a reasonable evaluation. (www.jbxue.com Script Academy finishing)
System performance Monitoring
While the allocation of swap space is important, performance monitoring is more valuable when the system is running. Through the Performance monitoring tool, you can check the performance indicators of the system to find the bottleneck of system performance. This article only describes some of the commands and uses that are related to swap under Solaris.
The most common is the Vmstat command (which has commands on most UNIX platforms), which allows you to see most of the performance metrics.
For example:
# Vmstat 3
Procs Memory Swap IO system CPU
R b w swpd free buff cache si so bi bo in CS us sy ID
0 0 0 0 93880 3304 19372 0 0 10 2 131 10 0 0 99
0 0 0 0 93880 3304 19372 0 0 0 0 109 8 0 0 100
0 0 0 0 93880 3304 19372 0 0 0 0 112 6 0 0 100
............
Command Description:
The parameters that follow the Vmstat specify the time interval at which the performance metrics are captured. 3 indicates a capture every three seconds. The first line of data does not have to look, it has no value, it only reflects the average performance since the boot. Starting from the second line, the system performance metrics are reflected every three seconds. Among these performance indicators, swap-related include the following:
Procs under the W
It represents the current (within three seconds) of the number of processes that need to free memory and swap out.
The SWPD under memory
It represents the size of the swap space used.
Si,so under Swap
Si represents the total amount of swap back memory (swap in) per second (within three seconds), in Kbytes;so representing the total amount of swap out memory (swap out) per second in the current (three seconds) unit of Kbytes.
The larger the number of indicators above indicates the busier the system. The performance of these indicators is related to the system's specific configuration. System administrators should be in normal operation of the system, note the values of these indicators, in the event of system problems, and then compare, will quickly find the problem, and set up the normal operation of the system standard indicator values for performance monitoring use.

Extended reading:
    • What about the lack of swap space for Linux swap?
    • How to manually create swap partition methods for Linux
    • to see which processes are taking up the swap partition script
    • Linux using Fdisk to create a swap partition
    • Implementation of automatic load on swap partition with boot

In addition, using Swapon-s is a simple way to see the current swap resource usage. Example:  
# swapon-s 
Filename Type Size used priority 
/dev/hda9 partition 361420 0 3 
It is easy to see the size of the used and unused resources in the swap space.   The
should keep the swap load below 30% to ensure good system performance.  
System command   for swap operations;
Add swap space, divided into the following steps:  
1) become Superuser  
$su-root 
2) Create swap file  
# dd If=/dev/zero of=swapfile bs=1024 count=65536 
creates a swap file with contiguous space.  
3) Activate swap file  
#/usr/sbin/swapon swapfile 
Swapfile refers to the interchange file created in the previous step. 4) Now that the new swap file has worked, the previous steps will not be remembered after the system restarts. Therefore to record the name of the file in the/etc/fstab file, and the swap type, such as:  
/path/swapfile None Swap sw,pri=3 0 0 
5) Verify that the Swap file is added  
/usr/sbin/swapon-s 
removes the extra swap space.  
1) become Superuser  
2) Use the Swapoff command to retract the swap space.  
#/usr/sbin/swapoff swapfile 
3) Edit the/etc/fstab file and remove the entity from this swap file.  
4) Reclaims this file from the file system.  
#rm swapfile 
5) Of course, if the swap space is not a file, but a partition, you need to create a new file system and then attach it to the original file system.

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.