Home System Tutorial LINUX [Linux System Optimization] Liberate your memory space—swap and buffer optimization guide

[Linux System Optimization] Liberate your memory space—swap and buffer optimization guide

Feb 13, 2024 pm 12:24 PM
linux system

Have you ever encountered a Linux system that runs slowly or has insufficient memory? It may be because the swap and buffer in the system are not well configured. This article will give you an in-depth understanding of swap and buffer, and how to optimize them to take your system performance to the next level.

[Linux System Optimization] Liberate your memory space—swap and buffer optimization guide

What is the memory mechanism of Linux?

We know that reading and writing data directly from physical memory is much faster than reading and writing data from the hard disk. Therefore, we hope that all data reading and writing are completed in memory, and memory is limited, so Introduced the concepts of physical memory and virtual memory.

Physical memory is the memory size provided by the system hardware. It is real memory. Compared with physical memory, there is a concept of virtual memory under Linux. Virtual memory is a strategy proposed to meet the shortage of physical memory. It is A piece of logical memory is virtualized using disk space. The disk space used as virtual memory is called swap space.

As an extension of physical memory, Linux will use the virtual memory of the swap partition when the physical memory is insufficient. More specifically, the kernel will write the temporarily unused memory block information to the swap space. In this way, the physical memory will be Once released, this memory can be used for other purposes. When the original content is needed, the information will be read from the swap space into physical memory again.

Linux's memory management adopts a paging access mechanism. In order to ensure that physical memory can be fully utilized, the kernel will automatically swap infrequently used data blocks in physical memory into virtual memory at the appropriate time, and will Frequently used information is retained to physical memory.

To have an in-depth understanding of the Linux memory operating mechanism, you need to know the following aspects:

The Linux system will perform page swap operations from time to time to maintain as much free physical memory as possible. Even if there is nothing that requires memory, Linux will swap out temporarily unused memory pages. This avoids the time required to wait for the exchange.

Linux page swapping is conditional. Not all pages are swapped to virtual memory when not in use. The Linux kernel only swaps some infrequently used page files to virtual memory based on the "most recently used" algorithm. Sometimes We will see this phenomenon: Linux still has a lot of physical memory, but a lot of swap space is also used. In fact, this is not surprising. For example, when a process that takes up a lot of memory needs to consume a lot of memory resources when running, some uncommon page files will be swapped into virtual memory, but later this process that takes up a lot of memory resources will be swapped into virtual memory. When the process ends and a lot of memory is released, the page file that was just swapped out will not be automatically swapped into the physical memory. Unless this is necessary, the system's physical memory will be much free at this moment, and the swap space is also being used. The phenomenon just mentioned occurred. There's nothing to worry about at this point, as long as you know what's going on.

The pages in the swap space will first be swapped to physical memory when used. If there is not enough physical memory to accommodate these pages at this time, they will be swapped out immediately. As a result, there may not be enough space in the virtual memory to Storing these swap pages will eventually cause problems such as false crashes and service abnormalities in Linux. Although Linux can recover by itself within a period of time, the recovered system is basically unusable.

Therefore, it is very important to properly plan and design the use of Linux memory.

In the Linux operating system, when an application needs to read data in a file, the operating system first allocates some memory, reads the data from the disk into these memories, and then distributes the data to the application; when needed When writing data to a file, the operating system first allocates memory to receive user data, and then writes the data from memory to disk. However, if there is a large amount of data that needs to be read from the disk to the memory or written from the memory to the disk, the read and write performance of the system becomes very low, because whether it is reading data from the disk or writing data to the disk, it is a very long process. A process that consumes time and resources. In this case, Linux introduced the buffers and cached mechanisms.

Buffers and cached are both memory operations, used to save files and file attribute information that have been opened by the system. In this way, when the operating system needs to read certain files, it will first search in the buffers and cached memory areas. If found, Directly read out and send it to the application. If the required data is not found, it will be read from the disk. This is the caching mechanism of the operating system. Through caching, the performance of the operating system is greatly improved. But the contents of buffers and cached buffers are different.

Buffers is used to buffer block devices. It only records the metadata of the file system and tracking in-flight pages, while cached is used to buffer files. To put it more simply: buffers are mainly used to store the contents of the directory, file attributes and permissions, etc. And cached is directly used to remember the files and programs we have opened.

In order to verify whether our conclusion is correct, we can open a very large file through vi to see the changes in cache, and then vi the file again to feel the similarities and differences in the speed of opening it twice, and whether it is the second time it is opened. The speed is significantly faster than the first time? Then execute the following command:

find / -name .conf to see if the value of buffers changes, and then execute the find command repeatedly to see the difference in display speed between the two times.

When did Linux start using virtual memory (swap)?

[root@wenwen ~]# cat /proc/sys/vm/swappiness  
60
Copy after login

The 60 above means that swap will be used when 40% of the physical memory is used (refer to network information: when the remaining physical memory is less than 40% (40=100-60), the swap space will be used) swappiness=0 When swappiness = 100, it means actively using the swap partition and moving the data on the memory to the swap space in a timely manner.

The larger the value, the more likely it is to use swap. It can be set to 0, which does not prohibit the use of swap, but only minimizes the possibility of using swap.

Normally: the swap partition setting is recommended to be twice the memory (when the memory is less than or equal to 4G). If the memory is greater than 4G, the swap only needs to be larger than the memory. In addition, try to lower swappiness as much as possible, so that the system performance will be better.

B. Modify swappiness parameter

#临时性修改:  
[root@wenwen ~]# sysctl vm.swappiness=10  
vm.swappiness = 10  
[root@wenwen ~]# cat /proc/sys/vm/swappiness  
10  
#永久性修改:  
[root@wenwen ~]# vim /etc/sysctl.conf  
加入参数:  
vm.swappiness = 35 
然后在直接:  
[root@wenwen ~]# sysctl -p /etc/sysctl.conf  
#查看是否生效:  
cat /proc/sys/vm/swappiness  
35 
Copy after login

It takes effect immediately and can also take effect after restarting.

How to release memory?

General systems do not automatically release the key configuration file /proc/sys/vm/drop_caches of memory. This file records the parameters of cache release. The default value is 0, which means the cache is not released. Its value can be any number between 0 and 3, representing different meanings:

  • 0 – Do not release
  • 1 – Release page cache
  • 2 – Release dentries and inodes
  • 3 – Release all cache

Practical operation:

[Linux System Optimization] Liberate your memory space—swap and buffer optimization guide

Obviously there is a lot of free memory

How to release swap?

Premise: First of all, make sure that the remaining memory is greater than or equal to the swap usage, otherwise it will crash! According to the memory mechanism, once the swap partition is released, all files stored in the swap partition will be transferred to physical memory. Releasing swap is usually accomplished by remounting the swap partition.

a. Check where the current swap partition is mounted? b. Shut down this partition c. Check the status: d. Check whether the swap partition is shut down. The bottom line shows all e. Mount swap to /dev/sda5 f. Check whether the mounting is successful

[Linux System Optimization] Liberate your memory space—swap and buffer optimization guide

Through the introduction of this article, you have already understood the basic concepts and uses of swap and buffer in Linux systems and how to check their usage. At the same time, we have shared some practical optimization suggestions to help you further improve system performance and memory utilization. Hope this article helps you!

The above is the detailed content of [Linux System Optimization] Liberate your memory space—swap and buffer optimization guide. 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

Video Face Swap

Video Face Swap

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

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)

Using Task Manager in Linux Using Task Manager in Linux Aug 15, 2024 am 07:30 AM

There are many questions that Linux beginners often ask, "Does Linux have a Task Manager?", "How to open the Task Manager on Linux?" Users from Windows know that the Task Manager is very useful. You can open the Task Manager by pressing Ctrl+Alt+Del in Windows. This task manager shows you all the running processes and the memory they consume, and you can select and kill a process from the task manager program. When you first use Linux, you will also look for something that is equivalent to a task manager in Linux. A Linux expert prefers to use the command line to find processes, memory consumption, etc., but you don't have to

Solve the problem of garbled display of graphs and charts on Zabbix Chinese monitoring server Solve the problem of garbled display of graphs and charts on Zabbix Chinese monitoring server Jul 31, 2024 pm 02:10 PM

Zabbix's support for Chinese is not very good, but sometimes we still choose Chinese for management purposes. In the web interface monitored by Zabbix, the Chinese under the graphic icon will display small squares. This is incorrect and requires downloading fonts. For example, "Microsoft Yahei", "Microsoft Yahei.ttf" is named "msyh.ttf", upload the downloaded font to /zabbix/fonts/fonts and modify the two characters in the /zabbix/include/defines.inc.php file at define('ZBX_GRAPH_FONT_NAME','DejaVuSans');define('ZBX_FONT_NAME'

7 ways to help you check the registration date of Linux users 7 ways to help you check the registration date of Linux users Aug 24, 2024 am 07:31 AM

Did you know, how to check the creation date of an account on a Linux system? If you know, what can you do? Did you succeed? If yes, how to do it? Basically Linux systems don't track this information, so what are the alternative ways to get this information? You may ask why am I checking this? Yes, there are situations where you may need to review this information and it will be helpful to you at that time. You can use the following 7 methods to verify. Use /var/log/secure Use aureport tool Use .bash_logout Use chage command Use useradd command Use passwd command Use last command Method 1: Use /var/l

Teach you how to add fonts to Fedora in 5 minutes Teach you how to add fonts to Fedora in 5 minutes Jul 23, 2024 am 09:45 AM

System-wide installation If you install a font system-wide, it will be available to all users. The best way to do this is to use RPM packages from the official software repositories. Before starting, open the "Software" tool in Fedora Workstation, or other tools using the official repository. Select the "Add-ons" category in the selection bar. Then select "Fonts" within the category. You'll see the available fonts similar to the ones in the screenshot below: When you select a font, some details will appear. Depending on several scenarios, you may be able to preview some sample text for the font. Click the "Install" button to add it to your system. Depending on system speed and network bandwidth, this process may take some time to complete

What should I do if the WPS missing fonts under the Linux system causes the file to be garbled? What should I do if the WPS missing fonts under the Linux system causes the file to be garbled? Jul 31, 2024 am 12:41 AM

1. Find the fonts wingdings, wingdings2, wingdings3, Webdings, and MTExtra from the Internet. 2. Enter the main folder, press Ctrl+h (show hidden files), and check if there is a .fonts folder. If not, create one. 3. Copy the downloaded fonts such as wingdings, wingdings2, wingdings3, Webdings, and MTExtra to the .fonts folder in the main folder. Then start wps to see if there is still a "System missing font..." reminder dialog box. If not, just Success! Notes: wingdings, wingdin

Centos 7 installation and configuration NTP network time synchronization server Centos 7 installation and configuration NTP network time synchronization server Aug 05, 2024 pm 10:35 PM

Experimental environment: OS: LinuxCentos7.4x86_641. View the current server time zone & list the time zone and set the time zone (if it is already the correct time zone, please skip it): #timedatectl#timedatectllist-timezones#timedatectlset-timezoneAsia/Shanghai2. Understanding of time zone concepts: GMT, UTC, CST, DSTUTC: The entire earth is divided into twenty-four time zones. Each time zone has its own local time. In international radio communication situations, for the sake of unification, a unified time is used, called Universal Coordinated Time (UTC). :UniversalTim

How to connect two Ubuntu hosts to the Internet using one network cable How to connect two Ubuntu hosts to the Internet using one network cable Aug 07, 2024 pm 01:39 PM

How to use one network cable to connect two ubuntu hosts to the Internet 1. Prepare host A: ubuntu16.04 and host B: ubuntu16.042. Host A has two network cards, one is connected to the external network and the other is connected to host B. Use the iwconfig command to view all network cards on the host. As shown above, the network cards on the author's A host (laptop) are: wlp2s0: This is a wireless network card. enp1s0: Wired network card, the network card connected to host B. The rest has nothing to do with us, no need to care. 3. Configure the static IP of A. Edit the file #vim/etc/network/interfaces to configure a static IP address for interface enp1s0, as shown below (where #==========

toss! Running DOS on Raspberry Pi toss! Running DOS on Raspberry Pi Jul 19, 2024 pm 05:23 PM

Different CPU architectures mean that running DOS on the Raspberry Pi is not easy, but it is not much trouble. FreeDOS may be familiar to everyone. It is a complete, free and well-compatible operating system for DOS. It can run some older DOS games or commercial software, and can also develop embedded applications. As long as the program can run on MS-DOS, it can run on FreeDOS. As the initiator and project coordinator of FreeDOS, many users will ask me questions as an insider. The question I get asked most often is: "Can FreeDOS run on a Raspberry Pi?" This question is not surprising. After all, Linux runs very well on the Raspberry Pi

See all articles