Table of Contents
swap partition introduction
vmstat 3
swapon -s
Home Operation and Maintenance Linux Operation and Maintenance Detailed explanation of the role of swap partition under Linux

Detailed explanation of the role of swap partition under Linux

Jan 24, 2018 pm 01:22 PM
linux swap Detailed explanation

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 
…………
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

deepseek web version entrance deepseek official website entrance deepseek web version entrance deepseek official website entrance Feb 19, 2025 pm 04:54 PM

DeepSeek is a powerful intelligent search and analysis tool that provides two access methods: web version and official website. The web version is convenient and efficient, and can be used without installation; the official website provides comprehensive product information, download resources and support services. Whether individuals or corporate users, they can easily obtain and analyze massive data through DeepSeek to improve work efficiency, assist decision-making and promote innovation.

How to install deepseek How to install deepseek Feb 19, 2025 pm 05:48 PM

There are many ways to install DeepSeek, including: compile from source (for experienced developers) using precompiled packages (for Windows users) using Docker containers (for most convenient, no need to worry about compatibility) No matter which method you choose, Please read the official documents carefully and prepare them fully to avoid unnecessary trouble.

BITGet official website installation (2025 beginner's guide) BITGet official website installation (2025 beginner's guide) Feb 21, 2025 pm 08:42 PM

BITGet is a cryptocurrency exchange that provides a variety of trading services including spot trading, contract trading and derivatives. Founded in 2018, the exchange is headquartered in Singapore and is committed to providing users with a safe and reliable trading platform. BITGet offers a variety of trading pairs, including BTC/USDT, ETH/USDT and XRP/USDT. Additionally, the exchange has a reputation for security and liquidity and offers a variety of features such as premium order types, leveraged trading and 24/7 customer support.

Ouyi okx installation package is directly included Ouyi okx installation package is directly included Feb 21, 2025 pm 08:00 PM

Ouyi OKX, the world's leading digital asset exchange, has now launched an official installation package to provide a safe and convenient trading experience. The OKX installation package of Ouyi does not need to be accessed through a browser. It can directly install independent applications on the device, creating a stable and efficient trading platform for users. The installation process is simple and easy to understand. Users only need to download the latest version of the installation package and follow the prompts to complete the installation step by step.

Get the gate.io installation package for free Get the gate.io installation package for free Feb 21, 2025 pm 08:21 PM

Gate.io is a popular cryptocurrency exchange that users can use by downloading its installation package and installing it on their devices. The steps to obtain the installation package are as follows: Visit the official website of Gate.io, click "Download", select the corresponding operating system (Windows, Mac or Linux), and download the installation package to your computer. It is recommended to temporarily disable antivirus software or firewall during installation to ensure smooth installation. After completion, the user needs to create a Gate.io account to start using it.

Ouyi Exchange Download Official Portal Ouyi Exchange Download Official Portal Feb 21, 2025 pm 07:51 PM

Ouyi, also known as OKX, is a world-leading cryptocurrency trading platform. The article provides a download portal for Ouyi's official installation package, which facilitates users to install Ouyi client on different devices. This installation package supports Windows, Mac, Android and iOS systems. Users can choose the corresponding version to download according to their device type. After the installation is completed, users can register or log in to the Ouyi account, start trading cryptocurrencies and enjoy other services provided by the platform.

gate.io official website registration installation package link gate.io official website registration installation package link Feb 21, 2025 pm 08:15 PM

Gate.io is a highly acclaimed cryptocurrency trading platform known for its extensive token selection, low transaction fees and a user-friendly interface. With its advanced security features and excellent customer service, Gate.io provides traders with a reliable and convenient cryptocurrency trading environment. If you want to join Gate.io, please click the link provided to download the official registration installation package to start your cryptocurrency trading journey.

How to Install phpMyAdmin with Nginx on Ubuntu? How to Install phpMyAdmin with Nginx on Ubuntu? Feb 07, 2025 am 11:12 AM

This tutorial guides you through installing and configuring Nginx and phpMyAdmin on an Ubuntu system, potentially alongside an existing Apache server. We'll cover setting up Nginx, resolving potential port conflicts with Apache, installing MariaDB (

See all articles