[Linux Kernel] Memory Management - Memory Reclamation Mechanism

Reprint please specify: https://www.cnblogs.com/Ethan-Code/p/16626560.html

The way of memory recovery

As mentioned above, the memory allocation method of malloc, malloc applies for virtual memory, only when the program accesses it, it will trigger a page fault exception to enter the kernel state, and establish a physical memory mapping in the page fault interrupt function.

If the physical memory is sufficient, the mapping between the page frame and the page is established directly. When the physical memory is insufficient, the kernel will reclaim physical memory. The main methods of memory reclaiming are:

  1. Background memory reclamation (kswapd)
  2. Direct memory recovery (direct reclaim)
  3. OOM mechanism (Out of Memory)

The three memory recovery methods are progressive according to the degree of memory shortage.

Background memory recovery - kswapd

The memory mentioned in this section is mainly for physical memory

kswapd is a kernel thread that is responsible for memory recovery in the background when memory is insufficient. This process occurs in the background, so it occurs asynchronously and will not block the process.

The kernel sets three thresholds on the capacity of the pair:

  • page minimum threshold (pages_min);
  • pages low threshold (pages_low);
  • page high threshold (pages_high);

When the memory is greater than pages_low , it means that the system memory is sufficient at this time, and memory recovery will not be performed.

When the memory is less than pages_low , it means that there is pressure on the memory at this time, and kswapd0 will be triggered to perform background memory recovery until pages_high .

When the memory is less than pages_min , it means that the user's memory is exhausted at this time, and direct memory recovery will be triggered, and the process will be blocked.

If you want to adjust the trigger timing of kswapd, you need to modify the value of pages_low , and the value of ** pages_low is calculated by pages_min , so you need to modify pages_min **.

Kernel options /proc/sys/vm/min_free_kbytescan set pages_min

Therefore, the memory water level threshold can be modified by the value of min_free_kbytes .

direct memory reclamation

When a process applies for and accesses memory, if the number of available memory pages is less than pages_min, it is not enough for memory allocation to establish a mapping relationship.

At this time, direct memory recovery will be triggered, and memory recovery will start while blocking the process, so this memory recovery method is synchronous.

Blocking the process will cause a long delay, the CPU utilization of the system will increase, and the system load will increase, so try to avoid direct memory recovery.

OOM——Out of Memory

If the remaining free memory of the system is not enough for memory allocation after direct memory reclamation, the OOM mechanism will be further triggered.

The OOM Killer mechanism will select and kill a process that occupies a high physical memory according to the algorithm in order to release memory resources. If the physical memory is still insufficient, the OOM Killer will continue to kill the process that occupies a high physical memory until enough memory locations are released. .

There is a function in the Linux kernel oom_badness(), which scans the processes that can be killed in the system, and scores each process, and the process with the highest score will be killed first.

A process' score is generally determined by the number of physical memory page frames it occupies.

Calculation method of function oom_badness(): points = process_pages + oom_score_adj*totalpages/1000

variable meaning
points scoring result
process_pages The number of physical memory pages used by the process
oom_score_adj The OOM calibration value of the process is generally 0
totalpages The total number of pages available in the system

/proc/[pid]/oom_score_adjOOM can be adjusted by setting the OOM calibration value, that is, the modified value, thereby changing the score result of this process and reducing the probability that the process is killed by OOM.

The setting range of oom_score_adj is [-1000,1000]

Performance impact of memory reclamation timing

sar -B 1The command can observe the memory recovery situation:

pgscank/s : The number of pages scanned by kswapd per second . pgscand/s: The number of pages directly scanned
by the application per second during the memory application process . pgsteal/s: The number of scanned pages recycled per second (pgscank+pgscand).

When the pages_low setting is unreasonable, the kswapd background reclaims memory not in a timely manner, applying for memory will often trigger direct memory reclamation , and the value of pgscand/s will be relatively large, which will cause system jitter in terms of performance . By appropriately increasing min_free_kbytes , the kswapd kernel thread can be triggered in advance to avoid direct memory reclamation.

The performance impact of modifying min_free_kbytes:

  • min_free_kbytes is relatively small, and the system reserves a small amount of free memory, so most of the memory is used by the process. However, it will cause kswapd recovery not in time, trigger direct memory recovery, and block the process.
  • If min_free_kbytes is relatively large, the system will reserve more free memory, which will reduce the available memory of the application process while improving performance, and reduce memory utilization, which is a way of exchanging space for time.
  • min_free_kbytes is very large, close to the total memory size in extreme cases, only a small amount of memory can be used, and OOM may be triggered frequently.

The specific process of memory recovery - file pages and anonymous pages

The two memory recycling methods mentioned above (kswapd and direct recycling) mainly reclaim file pages and anonymous pages in memory.

  • File-backed Page : Both the disk data (Buffer) cached by the kernel and the file data (Cache) cached by the kernel are called file pages.
  • Anonymous page (Anonymous Page) : This part of memory has no actual carrier, such as heap, stack data, etc., unlike the file cache that has a hard disk file.

The recycling of file pages and anonymous pages is based on the LRU algorithm (least recently used algorithm).

In the Linux kernel, the LRU algorithm maintains two doubly-linked lists: the active linked list and the inactive linked list . PG_activeThe bit status is used to determine whether it is active, and PG_referencedthe bit status is used to determine whether it has been accessed. The two page identifiers determine how to move pages between the two linked lists.

In fact, concurrent access to the linked list requires the use of spin locks to avoid conflicts. For this, the kernel also provides an LRU cache mechanism. Only when the pages to be inserted into the two linked lists reach a certain number will they be added to the corresponding linked list at one time. , thereby reducing lock competition and improving system performance.

When the memory is insufficient, the kernel will give priority to reclaiming the tail page of the inactive linked list.

  • File page recycling: If the file page is clean, the memory will be released directly without affecting system performance. If it is a dirty page (dirty), it needs to be written to the disk first, and then the memory is released. IO operations will occur during this process, which will affect system performance.
  • Anonymous page recycling: Because this part of memory may still be used, it cannot be released directly. If the swap mechanism is turned on, the memory will be stored in the disk first, and then read from the disk when needed. IO operations will occur during this process, which will affect system performance.

It can be seen that in addition to reclaiming clean file pages, other memory page reclaims will basically cause disk IO, which will affect the performance of the system.

Therefore, for memory reclamation, appropriate parameters should be set according to requirements, so as to adjust the memory reclamation timing of kswapd, adjust the frequency of direct memory reclamation, reduce the triggering of OOM mechanism, and improve the performance and stability of the system.


Good article recommendation: https://xiaolincoding.com/os/3_memory/mem_reclaim.html
About LRU, you can refer to: https://www.cnblogs.com/muahao/p/10109712.html

Guess you like

Origin blog.csdn.net/weixin_45636061/article/details/127184818