Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Memory mechanism of Linux and manual release of swap and buffer and cache

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "the memory mechanism of Linux and the manual release of swap and buffer and cache". The explanation in the article is simple and clear and easy to learn and understand. please follow the editor's train of thought to study and learn "the memory mechanism of Linux and manually release swap and buffer and cache".

What is the memory mechanism of linux?

We know that it is much faster to read and write data directly from physical memory than from hard disk, so we want all data to be read and written in memory, and memory is limited. This leads to the concept of physical memory and virtual memory.

Physical memory is the amount of memory provided by system hardware, and it is the real memory. Compared with physical memory, there is also 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 virtualized by disk space. The disk space used as virtual memory is called swap space (Swap Space).

As an extension of the 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, so that the physical memory is released, and this memory can be used for other purposes. When the original content is needed, the information will be re-read from the swap space into the physical memory.

The memory management of Linux adopts the paging access mechanism. In order to ensure that the physical memory can be fully utilized, the kernel will automatically swap the data blocks that are not frequently used in the physical memory into the virtual memory at an appropriate time, and keep the frequently used information to the physical memory.

To gain an in-depth understanding of how linux memory works, you need to know the following aspects:

The Linux system will swap pages from time to time to keep as much free physical memory as possible, and Linux will swap out pages that are temporarily unused, even if nothing is needed. This avoids the time required to wait for the exchange.

Linux page exchange is conditional, not all pages are swapped to virtual memory when not in use, the linux kernel according to the "recently most frequently used" algorithm, only some infrequently used page files to virtual memory, sometimes we will see such a phenomenon: linux physical memory is still a lot, but swap space is also used a lot. In fact, this is not surprising. For example, when a process that takes up a lot of memory consumes a lot of memory resources, some infrequently used page files will be swapped into virtual memory. But later, when the process that takes up a lot of memory ends and releases a lot of memory, the page files that have just been swapped out will not be automatically swapped into physical memory unless it is necessary. Then the physical memory of the system will be a lot free at the moment, and the swap space is also being used, which leads to the phenomenon mentioned just now. Don't worry about this, as long as you know what's going on.

The pages of swap space will first be swapped to physical memory when they are used. If there is not enough physical memory to accommodate these pages, they will be swapped out immediately. Thus, there may not be enough space in virtual memory to store these swap pages, which will eventually lead to problems such as fake panic and service exceptions in linux. Although linux can recover itself within a period of time. But the restored system is basically unavailable.

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

In the Linux operating system, when the application needs to read the data in the file, the operating system first allocates some memory, reads the data from the disk into the memory, and then distributes the data to the application; when the data needs to be written to the file, the operating system first allocates memory to receive user data, and then writes the data from memory to disk. However, if a large amount of data needs to be read from disk to memory or written to disk by memory, the read-write ability of the system becomes very low, because whether reading data from disk or writing data to disk is a time-consuming and resource-consuming process. In this case, Linux introduces buffers and cached mechanisms.

Buffers and cached are memory operations, used to save files that the system has opened and file attribute information, so that when the operating system needs to read some files, it will first look up in the buffers and cached memory area, if found, read directly to the application, if not found the data needed, then read from the disk, this is the operating system cache mechanism, through caching, greatly improve the performance of the operating system. But the content of buffers and cached buffering is different.

Buffers is used to buffer block devices, it only records the file system metadata (metadata) and tracking in-flight pages, while cached is used to buffer files. To put it more popularly, buffers is mainly used to store what is in the directory, the properties of the file, permissions and so on. 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, see the changes in cached, and then vi the file again to feel the similarities and differences between the two openings, and whether the second time is obviously faster than the first time. Then execute the following command:

Find /-name .conf to see if the value of buffers changes, and then repeat the find command to see how the display speed is different twice.

When did linux start using virtual memory (swap)?

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

The above 60 means that swap is used only when 40% of physical memory is used (see network materials: swap space is used when the remaining physical memory is less than 40%). Swappiness=0 means maximum use of physical memory, followed by swap space. Swappiness=100 means to actively use swap partitions and move the data in memory to swap space in a timely manner.

A higher value indicates a greater tendency to use swap. It can be set to 0, which does not prohibit the use of swap, but minimizes the likelihood of using swap.

In general: the swap partition setting is recommended to be twice the amount of memory (when the memory is less than or equal to 4G), and if the memory is greater than 4G, as long as it is larger than the memory. In addition, try to lower the swappiness, so that the performance of the system will be better.

b. Modify swappiness parameters

Temporary modification: [root@wenwen ~] # sysctl vm.swappiness=10 vm.swappiness=10 [root@wenwen ~] # cat / proc/sys/vm/swappiness 10

Permanent modification:

[root@wenwen ~] # vim / etc/sysctl.conf add parameters: vm.swappiness = 35 and then check whether it is valid at: [root@wenwen ~] # sysctl-p / etc/sysctl.conf: cat / proc/sys/vm/swappiness 35

It takes effect immediately, and the restart can also take effect.

Third, how to release memory?

A general system is a key configuration file / proc/sys/vm/drop_caches that does not automatically free memory. The parameter of cache release is recorded in this file, and the default value is 0, that is, 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 caches

Real fuck:

Obviously, there's a lot of free memory left.

4. How to release swap?

Premise: first of all, make sure that the remaining memory is greater than or equal to swap usage, otherwise there will be downtime! 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 done by remounting the swap partition.

a. Check where the current swap partition is mounted? b. Shut down this partition c. View status: d. Check to see if the swap partition is turned off, and the bottom line shows full e. Mount swap to / dev/sda5 f. Check whether the mount is successful

Thank you for reading, the above is the "memory mechanism of Linux and manual release of swap and buffer and cache" content, after the study of this article, I believe you on the memory mechanism of Linux and manual release of swap and buffer and cache this problem has a deeper understanding, the specific use of the need for you to practice and verify. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report