Home >System Tutorial >LINUX >Linux kernel memory recycling mechanism: in-depth understanding of memory management

Linux kernel memory recycling mechanism: in-depth understanding of memory management

王林
王林forward
2024-02-14 19:51:19813browse

Have you ever encountered various memory problems in Linux systems? Such as memory leaks, memory fragmentation, etc. These problems can be solved by in-depth understanding of the Linux kernel memory recycling mechanism.

No matter how much memory there is on the computer, it is not enough, so the Linux kernel needs to reclaim some rarely used memory pages to ensure that the system continues to use memory. There are three methods of page recycling: page writeback, page exchange and page discarding: If the backing storage of a rarely used page is a block device (such as a file mapping), the memory can be directly synchronized to the block device to free up the page. The page can be reused; if the page has no backing storage, it can be swapped to a specific swap partition, and then swapped back to the memory when it is accessed again; if the page's backing storage is a file, but the file content cannot be modified in the memory (such as executable file), it can be discarded directly if it is not currently needed.

1 Time to recycle

Linux kernel内存回收机制:深入理解内存管理

2 Which memory can be recycled

2.1 Recycling of page frames
LRU (Least Recently Used), the least recently used linked list, is arranged according to recent usage. The least used one exists at the end of the linked list, which can be seen through the following macro definition:
#define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
Each zone has 5 LRU linked lists to store various recently used pages.
enum lru_list {
LRU_INACTIVE_ANON = LRU_BASE,
LRU_ACTIVE_ANON = LRU_BASE LRU_ACTIVE,
LRU_INACTIVE_FILE = LRU_BASE LRU_FILE,
LRU_ACTIVE_FILE = LRU_BASE LRU_FILE LRU_ACTIVE,
LRU_UNEVICTABLE,
NR_LRU_LISTS
};
Among them, the pages in the four linked lists of INACTIVE_ANON, ACTIVE_ANON, INACTIVE_FILE, and ACTIVE_FILE can be recycled. ANON represents anonymous mapping, no backing storage; FILE represents file mapping.
When recycling pages, INACTIVE pages will be recycled first. Only when there are very few INACTIVE pages, ACTIVE pages will be considered for recycling.
In order to evaluate the activity of the page, the kernel introduces two flags, PG_referend and PG_active. Why do we need two bits? Assume that only one PG_active is used to identify whether the page is active. This bit is set when the page is accessed, but when is it clear? Doing this requires maintaining a large number of kernel timers, and this approach is doomed to failure.
A more elegant approach can be implemented using two flags. The core idea is: one indicating the current level of activity and one indicating whether it has been referenced recently. The following figure illustrates the basic algorithm.
Linux kernel内存回收机制:深入理解内存管理

Basically there are the following steps:
(1) If the page is active, set the PG_active bit and save it in the ACTIVE LRU list; otherwise, in INACTIVE;
(2) Each time the page is accessed, the PG_referenced bit is set. The mark_page_accessed function is responsible for this work;
(3) PG_referenced and the information provided by the reverse mapping are used to determine the degree of page activity. Each time this bit is cleared, the degree of page activity is detected. The page_referenced function implements this behavior;
(4) Enter mark_page_accessed again. If it is found that PG_referenced has been set, it means that page_referenced is not checked, so mark_page_accessed is called more frequently than page_referenced, which means that the page is frequently accessed. If the page is in the INACTIVE linked list, move it to ACTIVE. In addition, the PG_active flag will be set and PG_referenced will be cleared;
(5) Reverse transfer is also possible. When page activity decreases, page_referenced may be called twice in succession without mark_page_accessed in between.
If access to a memory page is stable, then calls to page_referenced and mark_page_accessed are balanced in nature, and the page remains in the current LRU list. This solution also ensures that memory pages will not jump quickly between ACTIVE and INACTIVE linked lists.
2.2 slab**** cache recycling
Slab cache recycling is relatively flexible, and all methods registered in shrinker_list will be executed.
The kernel registers the prune_super method for each file system by default. This function is used to recycle the dentry and inode cache that are no longer used in the file system;
Android's lowmemorykiller mechanism registers a method to selectively kill processes and reclaim the memory used by the process.
3****How ​​to recycle page frames
Linux kernel内存回收机制:深入理解内存管理

shrink_page_list is the process of actually recycling pages
Linux kernel内存回收机制:深入理解内存管理

4 Frequency of periodic recycling

4.1 kswapd
kswapd is a memory recycling thread created by the kernel for each memory node. Why does it need periodic recycling when there is a shortage recycling mechanism? Because some memory allocations are not allowed to block waiting for recovery, such as memory allocations in interrupt and exception handlers; some memory allocations are not allowed to activate I/O access. Only a few cases of memory shortage can completely execute the recycling process, so it is very necessary to use the system's idle time to reclaim memory.
This function records the allocation order used in the last balancing operation. If kswapd_max_order is greater than the last value, or classzone_idx is less than the last value, balance_pgdat is called to balance the memory domain again. Otherwise, a short sleep can be performed. The sleep time is HZ /10, for arm (HZ=100), the sleep time is 1ms.
balance_pgdat balances the operation until zone_wartermark_ok of the memory domain.
4.2 cache_reap
cache_reap is used to recycle idle objects in the slab. If the idle objects can be restored to a page, they are released back to the buddy system. Each time cache_reap is called, all slab_caches will be traversed and then sleep for 2*HZ. For arm (HZ=100), the cycle is 20ms.

In short, the Linux kernel memory recycling mechanism is a very important concept that can help you better understand memory management in Linux systems. If you want to know more about this concept, you can check out the resources provided in this article.

5 References

(1)《understanding the linux kernel》
(2)《professional linux kernel architecture》

The above is the detailed content of Linux kernel memory recycling mechanism: in-depth understanding of memory management. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lxlinux.net. If there is any infringement, please contact admin@php.cn delete