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

Example Analysis of Linux memory Management Mechanism

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly shows you the "example analysis of Linux memory management mechanism", the content is simple and easy to understand, clear organization, I hope to help you solve doubts, let Xiaobian lead you to study and learn the "example analysis of Linux memory management mechanism" article.

Here's how Linux memory management works:

Physical memory and virtual memory

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

Physical memory is the memory size provided by the system hardware, is the real memory, relative to physical memory, in Linux there is a concept of virtual memory, virtual memory is to meet the shortage of physical memory and the proposed strategy, it is the use of disk space virtual out of a piece of logical memory, used as virtual memory disk space is called swap space (Swap Space).

As an extension of physical memory, Linux will use virtual memory in swap partitions when physical memory is insufficient. More specifically, the kernel will write temporarily unused memory block information to swap space, so that physical memory is released and this memory can be used for other purposes. When the original content is needed, this information will be re-read from swap space into physical memory.

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

To understand how Linux memory works, you need to know the following:

Linux systems perform page swapping operations from time to time to keep as much free physical memory as possible, and Linux swaps out temporarily unused memory pages even if nothing else requires memory. This avoids the time required to wait for an exchange.

Linux page swap is conditional, not all pages are swapped to virtual memory when not in use, Linux kernel according to the "most recently used" algorithm, only some infrequently used page files are swapped 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 runs, it needs to consume a lot of memory resources. At this time, some infrequently used page files will be exchanged into virtual memory. However, when the process that takes up a lot of memory resources ends and releases a lot of memory, the page files that have just been exchanged will not be automatically exchanged into physical memory. Unless this is necessary, the physical memory of the system will be idle at this moment. At the same time, the exchange space is also being used, and the phenomenon just mentioned appears. There was nothing to worry about, just knowing what was going on.

Swap space pages in use will first be swapped to physical memory, if there is not enough physical memory to accommodate these pages, they will be immediately swapped out, so there may not be enough space in virtual memory to store these swap pages, which will eventually lead to false crashes, service exceptions and other problems in Linux. Although Linux can recover itself for a period of time, the recovered system is basically unavailable.

Therefore, rational planning and design of Linux memory use is very important.

Second, memory monitoring

As a Linux system administrator, monitoring the use of memory is very important, through monitoring helps to understand the use of memory status, such as whether the memory occupation is normal, whether the memory is scarce, etc., monitoring the most commonly used memory commands are free, top, etc., the following is a system free output:

[root@linuxeye ~]# free total used free shared buffers cached Mem: 3894036 3473544 420492 0 72972 1332348 -/+ buffers/cache: 2068224 1825812 Swap: 4095992 906036 3189956

Meaning of each option:

First line:

total: Total size of physical memory

used: the amount of physical memory that has been used

free: free physical memory size

shared: the amount of memory shared by multiple processes

buffers/cached: Size of disk cache

Second line Mem: represents physical memory usage

The third line (-/+ buffers/cached) represents the disk cache usage status

Line 4: Swap indicates swap space memory usage status

The state of memory output by the free command can be viewed from two perspectives: one from the kernel perspective and one from the application layer perspective.

View the state of memory from a kernel perspective

That is, the kernel can be directly allocated to it at present without additional operation, that is, the value of the second line Mem item in the output of the free command above. It can be seen that the physical memory of this system has 3894036K, and the free memory is only 420492K, which is a little more than 40M. Let's do a calculation like this:

3894036 - 3473544 = 420492

In fact, it is the total physical memory minus the used physical memory to get the free physical memory size. Note that the available memory value 420492 does not include the memory size in buffers and cached states.

If you think this system has too little free memory, you are wrong. In fact, the kernel completely controls memory usage. Linux will change buffers and cached memory into free memory for system use when memory is needed or when the system is running gradually.

System memory usage from an application layer perspective

That is, the memory size that applications running on Linux can use, that is, the output of the third line of the free command-/+ buffers/cached, you can see that the memory used by this system is only 2068224K, while the free memory reaches 1825812K. Continue to do such a calculation:

420492+(72972+1332348)=1825812

From this equation, we can see that the physical memory value available to the application is the sum of the free value of the Mem item plus the buffers and cached values. That is, this free value includes the buffers and cached item sizes. For the application, the memory occupied by buffers/cached is available because buffers/cached is to improve file reading performance. When the application needs memory, buffers/cached will be quickly reclaimed for use by the application.

Differences between buffers and cached

In the Linux operating system, when an application needs to read data from a file, the operating system allocates some memory, reads the data from disk into that memory, and then distributes the data to the application; when it needs to write data to a file, the operating system allocates memory to receive user data, and then writes the data from memory to disk. However, if there is a large amount of data to read from disk to memory or write from memory to disk, the system's read and write performance becomes very low, because both reading data from disk and writing data to disk are a very time and resource consuming process. In this case, Linux introduces buffers and cached mechanisms.

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

Buffers are used to buffer block devices, which only record file system metadata and tracking in-flight pages, while cached is used to buffer files. More popularly, buffers are mainly used to store what is in the directory, file attributes and permissions, etc. Cached is used directly to remember files and programs we have opened.

To verify whether our conclusion is correct, we can open a very large file through vi, look at the changes in cached, and then vi this file again, feel the difference between the two open speeds, is the second open speed significantly faster than the first time?

Then execute the following command:

find /* -name *.conf

Check to see if the buffers value changes, and then repeat the find command to see if the display speed differs twice.

Linux operating system memory operation principle, to a large extent, according to the requirements of the server to design, for example, the system buffer mechanism will be frequently used files and data cache in cached, linux always strive to cache more data and information, so that when the data is needed again, it can be directly taken from memory, without a long disk operation, this design idea improves the overall performance of the system.

The above is "Linux memory management mechanism sample analysis" all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

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