Linux Memory Management

Source: Internet
Author: User

In Linux it is often found that spare memory is very small, and it seems that all of the memory is consumed by the system. The surface feeling is not enough memory, in fact, otherwise. This is an excellent feature of Linux memory management, which differs from the memory management of Windows. The main feature is that no matter how large the physical memory is. Linux is fully utilized. Read some program-called hard disk data into memory, and use the fast feature of memory read and write to improve the data access performance of Linux system. Windows allocates memory for the application only when it needs memory, and does not take full advantage of large memory space. Other words. With each additional physical memory, Linux will be fully leveraged. With the benefits of hardware investment, Windows just makes it a device, even if it adds 8GB or more.

This feature of Linux. The main use is the physical memory of spare. To improve data access performance by dividing a portion of space into caches and buffers.

The page fast cache (cache) is a primary disk cache implemented by the Linux kernel. It is primarily used to reduce I/O operations on disks. In detail, it is by caching the data in the disk into physical memory. Turn the access to the disk into a physical memory access question.

The value of the disk cache is two: first, access to the disk is much slower than the speed of access to the memory, so. Access to data from memory is faster than access from disk. Second, once the data has been interviewed, it is very likely to be visited again in the near future.

To understand the Linux memory management mechanism:

One, physical memory and virtual memory

We know. Reading and writing data directly from physical memory is much faster than reading and writing data from a hard disk. Therefore, we want all the 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 the system hardware. Is the real memory, relative to the physical memory. Under Linux, another virtual memory concept, virtual memory is to meet the lack of physical memory of the proposed strategy, it is the use of disk space virtual out of a piece of logical memory. The disk space used as virtual memory is called swap space.

As an extension of physical memory. Linux uses the virtual memory of the swap partition when physical memory is low. More specifically. Is that the kernel writes the memory block information that is temporarily unused to the swap space. Since then, physical memory has been freed and this memory can be used for other purposes. When the original content needs to be used, the information is read into the physical memory again from the swap space.

Linux memory management is a paging access mechanism, in order to ensure that the physical memory can be fully utilized, the kernel will be in the appropriate time, the physical memory is not used in the active exchange of data blocks themselves into virtual memory. Instead, the information that is often used is persisted to physical memory.

Learn more about the Linux memory execution mechanism. There are several areas that need to be known:

  1. The Linux system will occasionally perform a page exchange operation. To keep as much spare physical memory as possible, even if there is nothing to do with memory, Linux will swap out temporary memory pages. This avoids the time required to wait for the exchange.

  2. Linux is conditional on page swapping, not all pages are swapped to virtual memory when not in use, and the Linux kernel is based on the "most recently used" algorithm. Just swapping some of the less frequently used paging files to virtual memory, sometimes we see a phenomenon like this: there is a lot of Linux physical memory, but the swap space is also very much used. In fact. This is not surprising, for example, when a process that occupies very large memory consumes a lot of memory resources, and there are some less frequently used paging files to be swapped into virtual memory, but later the process that consumes very much memory resources ends and frees up a lot of memory. The paging file that has just been swapped out does not actively swap into physical memory, and unless it is necessary, the system's physical memory will be spare very much at the moment, and the swap space is being used at the same time. There was a phenomenon that had just been said. There's nothing to worry about, just know what's going on.
  3. The pages of the swap space are first swapped to physical memory when they are used, assuming there is not enough physical memory to accommodate the pages at this time. They will be immediately swapped out, so that virtual memory may not have enough space to store these exchange pages, and eventually lead to Linux fake crashes, service anomalies and other issues, Linux, although the ability to restore itself over a period of time, but the restored system is basically unusable.

So. It is important to plan and design the use of Linux memory properly.

Second, the memory monitoring

As a Linux system administrator. It is important to monitor the state of memory usage. Monitoring helps you understand the state of memory usage. For example, memory consumption is normal. Memory is scarce and so on. The most commonly used commands for monitoring memory are free, top, and so on. The following is the output of a system free:

  
 
  1. [[email protected] ~]# free
  2. -/+ buffers/cache:2068224 1825812
  3. swap:4095992 906036 3189956

Meaning of each option:

First line:

Total: Overall size of physical memory

Used: The amount of physical memory already in use

Free: spare physical memory size

Shared: The amount of memory that multiple processes share

buffers/cached: Size of the disk cache

Second line of Mem: representing physical memory usage

Third row (-/+ buffers/cached): Represents the disk cache usage status

Line four: Swap indicates swap space memory usage state

The memory state of the free command output can be viewed in two angles: one from the kernel point of view. One is from the point of view of the application layer.

View the state of memory from the perspective of the kernel

Is that the kernel can now be directly assigned to, do not need additional operations, that is, the above free command output of the second line of mem value, can be seen. This system physical memory 3894036K, spare memory only 420492K, that is 40M a little more, we do a calculation:

3894036–3473544 = 420492

In fact, the total physical memory minus the physical memory already used is the spare physical memory size, note that the available memory value of 420492 does not include the memory size in the buffers and cached states.

Suppose you think this system spare memory too small. Then you are wrong. As a matter of fact. The kernel completely controls the use of memory. Linux will change the buffers and cached state of memory into free memory for system use when memory is required, or when the system is stepping forward.

Use state of system memory from the perspective of the application layer

That is the amount of memory that an application executing on Linux can use, that is, the output of the third line of the free command,-/+ buffers/cached, to see that the memory used by this system is 2068224K, and spare memory reaches 1825812K, and continues to do such a calculation:

420492+ (72972+1332348) =1825812

By this equation, the physical memory value available to the application is the sum of the free values of the mem items plus the buffers and cached values. That is, this free value contains the size of the buffers and cached items. for Applications, buffers/cached occupies memory is available, because buffers/cached is to improve the performance of the file read, when the application needs to use memory, buffers/cached will be very fast to be recycled. For use by the application.

Similarities and differences of buffers and cached

In the Linux operating system. When the application needs to read the data in the file, the operating system allocates some memory first. Reads data from the disk into these memory. The data is then distributed to the application, and when the data needs to be written to the file. The operating system allocates memory to receive user data before it writes data from memory to disk. However, assuming that there is a large amount of data that needs to be read from disk to memory or written to disk by memory, the system's read and write performance becomes very low. Since both reading data from disk and writing data to disk are a very time-consuming and resource-intensive process, Linux introduces the buffers and cached mechanisms.

Buffers and cached are memory operations used to save files and file attribute information that the system has previously opened, so that when the operating system needs to read some files. Will first look in the buffers and cached memory area, assuming that the direct read out to the application, assuming no need to find the data to read from the disk, which is the operating system caching mechanism, through the cache. Greatly improves the performance of the operating system. But the content of buffers and cached buffer is different.

the buffers is used to buffer block devices. It simply records the file system's metadata (metadata) and tracking in-flight pages, and cached is used to buffer the file . More commonly said: buffers is mainly used to store the contents of the folder, file attributes and permissions and so on. and cached is used directly to memorize the files and programs we have opened.

In order to verify our conclusion is correct, can open a very large file through VI, see the change of cached, and then again VI this file, feel how the speed of the two times to open the similarities and differences, is not the second opening speed significantly faster than the first time?

Then run the following command:

 
   
  

See if the value of buffers changes. Then run the find command repeatedly to see how the two times the display speed is different.

The memory execution principle of the Linux operating system is largely based on the needs of the server. For example, the system buffer mechanism will be used to cache files and data in the cached, Linux is always trying to cache a lot of other data and information, so that the need for this data can be directly from the memory, without the need for a lengthy disk operation. This design approach improves the overall performance of the system.

Linux Memory Management

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.