


Detailed explanation of the role of swap partition under Linux
This article mainly introduces a detailed explanation of the role of swap partition under Linux. The editor thinks it is quite good and has certain reference value. Friends in need can refer to it. I hope it can help everyone.
This article mainly studies the related contents of swap partition under Linux. The specific introduction is as follows.
swap partition introduction
Embedded Linux Chinese site news, the Swap partition of the Linux system, that is, the swap area, the role of the Swap space can be simply described as: when the physical memory of the system is not enough , it is necessary to release a part of the space in the physical memory for use by the currently running program. The freed space may come from some programs that have not operated for a long time. The freed space is temporarily saved in the Swap space. When those programs are about to run, the saved data is restored from Swap to the memory. In this way, the system always performs Swap when the physical memory is insufficient. In fact, Swap adjustment is crucial to the performance of Linux servers, especially Web servers. By adjusting Swap, system performance bottlenecks can sometimes be overcome and system upgrade costs can be saved.
As we all know, modern operating systems have implemented the "virtual memory" technology, which not only breaks through the limitations of physical memory in terms of functionality, allowing programs to manipulate space larger than the actual physical memory, More importantly, "virtual memory" is a safety protection net that isolates each process so that each process is not interfered by other programs.
Maybe computer users often encounter this phenomenon. For example, when using a Windows system, you can run multiple programs at the same time. When you switch to a program that you have not paid attention to for a long time, you will hear the hard disk beeping. This is because the memory of this program has been "stolen" by frequently running programs and placed in the Swap area. Therefore, once this program is placed on the front end, it will retrieve its data from the Swap area, put it into memory, and then continue running.
In addition, not all data exchanged from physical memory will be put into Swap (if so, Swap will be overwhelmed), a considerable part of the data will be swapped directly to the file system . For example, some programs will open some files and read and write files (in fact, every program must open at least one file, which is the running program itself). When the memory space of these programs needs to be swapped out, there is no need to The data in the file part is placed in the Swap space, but it can be placed directly into the file. If it is a file reading operation, the memory data is released directly and does not need to be swapped out, because it can be restored directly from the file system when needed next time; if it is a file writing operation, only the changed data needs to be saved to the file for recovery. But the data of objects generated by malloc and new functions are different. They require Swap space because they do not have corresponding "reserve" files in the file system, so they are called "anonymous" memory data. This type of data also includes some status and variable data in the stack. Therefore, Swap space is an exchange space for "anonymous" data.
Breaking through the 128M Swap limit
Some Linux (domestic Chinese version) installation manuals have such instructions: Swap space cannot exceed 128M. Why is this said? Before explaining the origin of the number "128M", let me first give an answer to the question: There is no 128M limit at all now! The current limit is 2G!
The Swap space in the Linux system is paged, and the size of each page is the same as the size of the memory page, which facilitates data exchange between the Swap space and the memory. When old versions of Linux implemented Swap space, they used the first page of Swap space as a "bit map" for all Swap space pages. This means that every bit on the first page corresponds to a page of Swap space. If this bit is 1, it means that Swap of this page is available; if it is 0, it means that this page is a bad block and cannot be used. In this way, the first Swap mapping bit should be 0, because the first Swap page is a mapping page. In addition, the last 10 mapping bits are also occupied to indicate the Swap version (the original version was Swap_space, the current version is swapspace2). Then, if the size of one page is s, this Swap implementation method can manage a total of "8 * (s - 10) - 1" Swap pages. For the i386 system, s=4096, the total space size is 133890048. If 1 MB=2^20 Byte is considered, the size is exactly 128M.
This way of managing Swap space is to prevent bad blocks in Swap space. If the system detects that there are bad blocks in Swap, it marks 0 on the corresponding bit map, indicating that this page is unavailable. In this way, when using Swap, bad blocks will not be used and cause errors in the system.
Current system designers believe:
1. The quality of hard disks is now very good, with very few bad blocks.
2. Even if there are, there are not many. You only need to list the bad blocks, and there is no need to create a mapping for each page.
3. If there are many bad blocks, this hard disk should not be used as Swap space.
So, now Linux has canceled the bit mapping method, and also canceled the 128M limit. Access directly by address, limited to 2G.
The impact of Swap configuration on performance
Allocating too much Swap space will waste disk space, and if there is too little Swap space, system errors will occur.
When the system's physical memory is used up, the system will run very slowly, but it can still run; if the Swap space is used up, an error will occur in the system. For example, a web server can spawn multiple service processes (or threads) according to different number of requests. If the Swap space is used up, the service process cannot be started, and an "application is out of memory" error will usually occur. In severe cases, it will cause Deadlock of service process. Therefore, the allocation of Swap space is very important.
Generally, the Swap space should be greater than or equal to the size of the physical memory, and the minimum should not be less than 64M. Usually the size of the Swap space should be 2-2.5 times the physical memory. However, according to different applications, there should be different configurations: if it is a small desktop system, only a smaller Swap space is required, while a large server system requires different sizes of Swap space depending on the situation. Especially for database servers and web servers, as the number of visits increases, the requirements for Swap space will also increase. For specific configurations, please refer to the instructions of each server product.
In addition, the number of Swap partitions also has a great impact on performance. Because the Swap operation is a disk IO operation, if there are multiple Swap areas, the allocation of Swap space will be performed on all Swaps in turn, which will greatly balance the IO load and speed up the Swap exchange. If there is only one swap area, all swap operations will make the swap area very busy, leaving the system in a waiting state most of the time, which is very inefficient. Using performance monitoring tools, you will find that the CPU is not very busy at this time, but the system is slow. This shows that the bottleneck is IO, and the problem cannot be solved by increasing the speed of the CPU.
System performance monitoring
Allocation of Swap space is certainly important, but performance monitoring while the system is running is even more valuable. Through performance monitoring tools, you can check various performance indicators of the system and find system performance bottlenecks. This article only introduces some commands and uses related to Swap under Solaris.
The most commonly used is the Vmstat command (there are such commands on most Unix platforms). This command can view most performance indicators. 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 following vmstat specify the time interval for performance indicator capture. 3 means capture every three seconds. There is no need to read the first row of data, it is of no value. It only reflects the average performance since startup. Starting from the second line, system performance indicators are reflected every three seconds. Among these performance indicators, those related to Swap include the following:
w under procs
It indicates that the memory and swap need to be released currently (within three seconds) The number of processes going out.
swpd under memory
It indicates the size of the Swap space used.
si under Swap, so
si represents the current (within three seconds) total amount of memory swapped back (Swap in) per second, in kbytes ; so represents the current (within three seconds) total amount of memory swapped out (Swap out) per second, in kbytes.
The greater the number of the above indicators, the busier the system is. The system busyness represented by these indicators is related to the specific configuration of the system. System administrators should write down the values of these indicators when the system is running normally. When a problem occurs with the system, they can compare it and quickly find the problem, and formulate standard indicator values for the normal operation of the system to provide performance. Monitor usage.
In addition, you can also easily check the current usage of Swap resources using Swapon-s. For example:
swapon -s
Filename Type Size Used Priority
/dev/hda9 partition 361420 0 3
can be convenient You can clearly see the size of used and unused resources in Swap space.
The Swap load should be kept below 30% to ensure good system performance.
To increase Swap space, follow 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 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 is restarted, the previous steps will not be remembered. Therefore, the name of the file and the Swap type should be recorded in the /etc/fstab file, such as:
/path/swapfile none Swap sw,pri=3 0 0
5) Check whether the Swap file is added with
/usr/sbin/swapon -s
to delete excess Swap space.
1) Become a super user
2) Use the Swapoff command to reclaim the Swap space.
#/usr/sbin/swapoff swapfile
3) Edit the /etc/fstab file and remove the entity of this Swap file.
4) Recycle 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 .
Related recommendations:
Oracle swap 100% case analysis
How to prevent Oracle from using Linux swap partition
How MySQL avoids using Linux swap partitions and improves read and write performance
The above is the detailed content of Detailed explanation of the role of swap partition under Linux. For more information, please follow other related articles on the PHP Chinese website!

Linux maintenance mode can be entered through the GRUB menu. The specific steps are: 1) Select the kernel in the GRUB menu and press 'e' to edit, 2) Add 'single' or '1' at the end of the 'linux' line, 3) Press Ctrl X to start. Maintenance mode provides a secure environment for tasks such as system repair, password reset and system upgrade.

The steps to enter Linux recovery mode are: 1. Restart the system and press the specific key to enter the GRUB menu; 2. Select the option with (recoverymode); 3. Select the operation in the recovery mode menu, such as fsck or root. Recovery mode allows you to start the system in single-user mode, perform file system checks and repairs, edit configuration files, and other operations to help solve system problems.

The core components of Linux include the kernel, file system, shell and common tools. 1. The kernel manages hardware resources and provides basic services. 2. The file system organizes and stores data. 3. Shell is the interface for users to interact with the system. 4. Common tools help complete daily tasks.

The basic structure of Linux includes the kernel, file system, and shell. 1) Kernel management hardware resources and use uname-r to view the version. 2) The EXT4 file system supports large files and logs and is created using mkfs.ext4. 3) Shell provides command line interaction such as Bash, and lists files using ls-l.

The key steps in Linux system management and maintenance include: 1) Master the basic knowledge, such as file system structure and user management; 2) Carry out system monitoring and resource management, use top, htop and other tools; 3) Use system logs to troubleshoot, use journalctl and other tools; 4) Write automated scripts and task scheduling, use cron tools; 5) implement security management and protection, configure firewalls through iptables; 6) Carry out performance optimization and best practices, adjust kernel parameters and develop good habits.

Linux maintenance mode is entered by adding init=/bin/bash or single parameters at startup. 1. Enter maintenance mode: Edit the GRUB menu and add startup parameters. 2. Remount the file system to read and write mode: mount-oremount,rw/. 3. Repair the file system: Use the fsck command, such as fsck/dev/sda1. 4. Back up the data and operate with caution to avoid data loss.

This article discusses how to improve Hadoop data processing efficiency on Debian systems. Optimization strategies cover hardware upgrades, operating system parameter adjustments, Hadoop configuration modifications, and the use of efficient algorithms and tools. 1. Hardware resource strengthening ensures that all nodes have consistent hardware configurations, especially paying attention to CPU, memory and network equipment performance. Choosing high-performance hardware components is essential to improve overall processing speed. 2. Operating system tunes file descriptors and network connections: Modify the /etc/security/limits.conf file to increase the upper limit of file descriptors and network connections allowed to be opened at the same time by the system. JVM parameter adjustment: Adjust in hadoop-env.sh file

This guide will guide you to learn how to use Syslog in Debian systems. Syslog is a key service in Linux systems for logging system and application log messages. It helps administrators monitor and analyze system activity to quickly identify and resolve problems. 1. Basic knowledge of Syslog The core functions of Syslog include: centrally collecting and managing log messages; supporting multiple log output formats and target locations (such as files or networks); providing real-time log viewing and filtering functions. 2. Install and configure Syslog (using Rsyslog) The Debian system uses Rsyslog by default. You can install it with the following command: sudoaptupdatesud


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)