How to Set Linux swap partitions? Method tutorial, linuxswap
How to set the Linux swap partition many friends asked about the swap partition in linux, collected a good article and shared it.
What is Swap?
Swap, that is, in the Swap zone, how many people care about it except when installing Linux? In fact, Swap adjustment is crucial to the performance of Linux servers, especially Web servers. By adjusting Swap, the system performance bottleneck is sometimes crossed to save system upgrade costs.
The principle of Swap is a complicated problem, which requires a lot of space. Here, we will only give a brief introduction. We will discuss in detail the implementation details of Swap in future articles.
As we all know, modern operating systems have implemented the "virtual memory" technology, which not only breaks through the physical memory restrictions in terms of functionality, so that programs can manipulate space larger than the actual physical memory, more importantly, "Virtual Memory" isolates the security protection network of each process so that each process is not disturbed by other programs.
The role of the Swap space is described as follows: when the system's physical memory is insufficient, a part of the physical memory needs to be released 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 Swap to the memory. In this way, the system always performs Swap switching when the physical memory is insufficient.
Computer users often encounter this phenomenon. For example, when you use a Windows system, you can run multiple programs at the same time. When you switch to a program that has been ignored for a long time, you will hear the hard disk burst. This is because the memory of this program is "stolen" by frequently running programs and put in the Swap area. Therefore, once the program is placed on the front end, it will retrieve its data from the Swap area, put it into the memory, and then run.
It should be noted that not all data exchanged from the physical memory will be put into Swap (if so, Swap will be overwhelmed ), A considerable amount of data is directly exchanged to the file system. For example, some programs open some files and read and write the files (in fact, each program must open at least one file, that is, run the program itself ), when you need to Swap out the memory space of these programs, there is no need to put the data in the file part into the Swap space, and you can directly put it in the file. If it is a file read operation, the memory data is directly released and does not need to be exchanged, because it can be directly restored from the file system when needed next time; if it is a file write operation, you only need to save the changed data to the file for recovery. However, the data of objects generated using the malloc and new functions is different. They need Swap space because they do not have the corresponding "reserve" file in the file system, therefore, it is called "Anonymous" (Anonymous) memory data. This type of data also includes some status and variable data in the stack. Therefore, the Swap space is the Swap space for "anonymous" data.
How to set the Swap partition size
I remember someone once commented on the Swap partition size setting: "The larger the hard disk is, the better, because the Linux kernel will not change swap until the physical memory is used up"
However, based on my experience, this may not be the case! A too large swap space will cause the kernel to think that there is a huge memory space and unlimitedly want to capture data into the memory, so that the kernel has been doing memory swap, slow system response time.
To be honest, if 1g ram does not run p2p or other things, it will be enough to set up 256 MB; if there is 2G, do not even set up.
In addition, if it is true that a software requires a huge amount of memory space to operate, it has to be started on swap, but it is best to distribute the performance across multiple physical hard disks (equivalent to raid effect )!
In fact, how to set the size of Swap partitions is the best way to check a Linux System Administrator's Level Test. How should Swap be set? I think so: first, we need to know which programs are running on this server and the memory size they occupy. After a definite check, the size of the Swap partition can be determined as follows:
(Memory size + Swap partition size) * 80% or 70% = total memory occupied by the program
Swap partitions are also useful during program testing. For example, administrators can monitor the usage of Swap partitions to detect system memory leakage, at the same time, applications such as Web projects can also provide a better traffic peak buffering effect. A Linux system administrator should be able to properly evaluate the system and program by monitoring the usage of Swap partitions. (Www.jbxue.com script School)
System Performance Monitoring
The allocation of Swap space is important, but the performance monitoring during system operation is more valuable. Through performance monitoring tools, you can check the performance indicators of the system and find the bottleneck of the system performance. This article only describes some Swap-related commands and usage in Solaris.
The most common command is the Vmstat command (which is available on most Unix platforms), which allows you to view most 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 0 0 10 2 19372 10 0 0 99
0 0 0 0 93880 3304 0 0 0 19372 8 0 0 109
0 0 0 0 93880 3304 0 0 0 19372 6 0 0 112
............
Command description:
Parameters after vmstat specify the time interval for performance indicator capture. 3 indicates capture every three seconds. The first line of data does not need to be viewed and has no value. It only reflects the average performance since the startup. Starting from the second line, the system performance indicators within three seconds are reflected. Swap-related performance indicators include the following:
W under procs
It indicates the number of processes that need to be released and swapped out in the current (within three seconds.
Swpd under memory
It indicates the size of the Swap space used.
Si, so under Swap
Si indicates the total number of Swap memories exchanged per second (within three seconds), measured in kbytes. so indicates the total number of Swap memories exchanged per second (within three seconds, the Unit is kbytes.
The larger the number of indicators, the more busy the system. The system busy level of these indicators depends on the specific configuration of the system. During normal system operation, the system administrator should write down the values of these indicators and compare them when a system problem occurs. Then, the system administrator will soon find the problem, standard indicators for normal operation of the system are formulated for performance monitoring.
Additional reading:
- What if linux swap has insufficient swap space?
- How to manually create Swap partitions in Linux
- Check which processes are using the swap partition script in linux.
- Linux uses fdisk to create swap partitions
- How to automatically load swap partitions at startup
In addition, you can use Swapon-s to easily view the current usage of Swap resources. For example:
# Swapon-s
Filename Type Size Used Priority
/Dev/hda9 partition 361420 0 3
It is easy to see the size of Swap space used and unused resources.
Swap load should be kept below 30% to ensure good system performance.
System commands for Swap operations
To increase the Swap space, take the following steps:
1) become a Super User
$ Su-root
2) create a Swap file
# Dd if =/dev/zero of = swapfile bs = 1024 count = 65536
Create a swap file with continuous space.
3) activate the Swap file
#/Usr/sbin/swapon swapfile
Swapfile refers to the swap file created in the previous step. 4) now the newly added Swap file has taken effect, but after the system restarts, it does not remember the previous steps. Therefore, you must record the file name and Swap type in the/etc/fstab file, for example:
/Path/swapfile none Swap sw, pri = 3 0 0
5) check whether the Swap file is added
/Usr/sbin/swapon-s
Delete unnecessary Swap space.
1) become a Super User
2) use the Swapoff command to reclaim Swap space.
#/Usr/sbin/swapoff swapfile
3) edit the/etc/fstab file and remove the entity of the Swap file.
4) reclaim the 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 mount it to the original file system.
In Linux, how does one set the role of SWAP partitions and the size of SWAP partitions?
When the system temporarily requires a large amount of memory and cannot meet the task execution requirements, move some inactive memory content to a cache area on the hard disk, this dedicated reserved area is the swap partition (equivalent to a Windows page file ). When the memory is sufficient, if the sleep function is not used, the swap partition can be smaller or even absent. If the sleep function is used, the swap partition should not be smaller than the memory.
How many partition settings are suitable for installing linux swap ??
9.0 M for linux 128 is enough.