Talking about the memory management mechanism of Linux

Source: Internet
Author: User
Tags memory usage linux

One 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 the hard disk, so we want all of the 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 amount of memory provided by the system hardware, is the real memory, relative to physical memory, under Linux there is a virtual memory concept, virtual memory is to meet the lack of physical memory of the strategy proposed, it is the use of disk space virtual out of a piece of logical memory, The disk space used for virtual memory is called swap space.

As an extension of physical memory, Linux is not available when physical memory is low using the swap partition of virtual memory, in more detail, the kernel will be temporarily unused memory block information to write to swap space, so that the physical memory has been released, this memory can be used for other purposes, when the need to use the original content, This information is then read back into the physical memory from the swap space.

The memory management of Linux takes the paging access mechanism, in order to ensure that the physical memory can be fully utilized, the kernel will automatically exchange the infrequently used data blocks in physical memory into virtual memory when appropriate, and keep the frequently used information to physical memory.

To get an insight into the Linux memory runtime mechanism, you need to know the following aspects:

First of all, the Linux system will be a paging operation from time to moment to maintain as much free physical memory, even if there is no need for memory, Linux will also swap out the memory page temporarily unused. This avoids the time that is required to wait for the exchange.

Second, the Linux page Exchange is conditional, not all pages are switched to virtual memory when not in use, and the Linux kernel, based on the "most recently used" algorithm, simply swaps some infrequently used paging files to virtual memory, and sometimes we see a phenomenon where Linux has a lot of physical memory , but the swap space is also used a lot. In fact, it's not surprising that, for example, a process that takes up a lot of memory consumes a lot of memory resources, and then there are some infrequently-used paging files that are swapped into virtual memory, but then the process that consumes many memory resources ends and frees up a lot of memory, The paging file that was exchanged just now will not automatically exchange into the physical memory, unless it is necessary, then the system physical memory will be idle a lot, while the swap space is also being used, there are just said phenomenon. Don't worry about that, just know what's going on.

Finally, the Swap Space page is first switched to physical memory when it is used, if there is not enough physical memory at this time to accommodate these pages, they will be exchanged immediately, so there may not be enough space in the virtual memory to store the Exchange pages, which will eventually lead to a fake panic, service exceptions, and so on Linux , Linux can recover itself over a period of time, but the restored system is basically unusable.

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

Two monitoring of memory

As a Linux system administrator, monitoring the use of memory is very important, through monitoring to help understand the use of memory, such as the normal memory consumption, memory is scarce, and so on, monitoring memory most commonly used commands have free, top, etc., the following is a system free output:

[Haixigov@webserver ~]$ Free

Total used free shared buffers Cached

mem:16402432 16360492 41940 0 465404 12714880

-/+ buffers/cache:3180208 13222224

swap:8193108 264 8192844

We explain the meaning of each option in the output result:

First is the first line:

Total: The overall size of physical memory.

Used: How little physical memory is already in use.

Free: The idle physical memory value.

Shared: Memory values shared by multiple processes.

Buffers/cached: The size of the disk cache.

Second line mem: represents physical memory usage.

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

Line four: Swap represents the swap space memory usage state.

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

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.