Linux kernel Memory recovery mechanism

Source: Internet
Author: User
Tags memory usage

Ext.: http://www.wowotech.net/linux_kenrel/233.html

Linux kernel Memory recovery mechanism

Itrocker Released: 2015-11-12 20:37 Category: Memory management

No matter how much memory is available on the computer, Linux kernel needs to recycle some of the memory pages that are seldom used to ensure that the system continues to have memory usage. Page recycling has three ways of page writeback, page swapping, and page discards: If a backup memory for a page that is rarely used is a block device (such as a file map), the memory can be synced directly to the block device, and the vacated pages can be reused, and if the page has no fallback memory, it can be swapped to a specific swap partition. The memory is swapped back when it is accessed again, and if the backup memory of the page is a file, but the contents of the file cannot be modified (such as an executable file), it can be discarded directly if it is not currently required.

1 Timing of recycling

2 which memory can be recycled

2.1 Recycling of page boxes

LRU (Least recently used), the least recently used linked list, is in accordance with recent usage, the least used at the end of the list, with the following macro definition can be seen:

#define Lru_to_page (_head) (List_entry ((_head)->prev, struct page, LRU))

Each zone has 5 LRU linked lists for each of the most 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

};

The pages in Inactive_anon, Active_anon, Inactive_file, Active_file 4 linked lists can be recycled. The anon represents an anonymous mapping with no backup memory;

When a page is recycled, the inactive page is recycled, and the active page is considered recycled only if the inactive page is rare.

To assess the extent of the page's activity, kernel introduced the pg_referend and pg_active two flag bits. Why do we need two bits? Suppose you use only one pg_active to identify whether a page is active, and when the page is accessed, set that bit, but when is it clear? This method is destined to fail because of the need to maintain a large number of kernel timers.

Using two flags, you can achieve a more sophisticated approach, the core idea is: a representation of the current level of activity, a representation of the recent has been quoted, illustrates the basic algorithm.

Basically there are the following steps:

(1) If the page is active, set the pg_active bit and save it in the active LRU linked list; inactive;

(2) Each time the page is accessed, the pg_referenced bit is set and the mark_page_accessed function is responsible for the work;

(3) pg_referenced and the information provided by the reverse mapping is used to determine the level of page activity, each time the bit is cleared, the page activity is detected, the page_referenced function realizes the behavior;

(4) Enter mark_page_accessed again. If pg_referenced is found to be set, it means that page_referenced does not perform a check, so calls to mark_page_accessed are more frequent than page_referenced, which means that the page is often accessed. If the page is in the inactive linked list, move it to active, also set the pg_active flag bit, clear pg_referenced;

(5) The reverse transfer is also possible, when the page activity is reduced, may be called two times in a row page_referenced and there is no mark_page_accessed in the middle.

If the access to the memory pages is stable, then the calls to page_referenced and mark_page_accessed are inherently balanced, so that the page remains in the current LRU list. This approach also ensures that memory pages are no longer fast jumping between active and inactive linked lists.

2.2 Slab Cache Recycling

Slab cache Recycling is relatively flexible, and all methods that are registered to Shrinker_list will be executed.

The kernel defaults to each file system by registering the Prune_super method, which is used to reclaim Dentry and inode caches that are no longer used in the file system;

The Android Lowmemorykiller mechanism registers the selective kill process method that recycles the memory used by the process.

3 How to recycle a page box

Where Shrink_page_list is the process of actually recycling pages

4 frequency of periodic recycling

4.1 KSWAPD

KSWAPD is the memory recycle thread that the kernel creates for each memory node, why is there a shortage of recycling mechanisms that need to be recycled periodically? Some memory allocations do not allow blocking to be reclaimed, such as memory allocations in interrupts and exception handlers, and some memory allocations do not allow I/O access to be activated. Only a few cases of memory shortages can fully perform the recycling process, so it is necessary to use system idle time to reclaim memory.

This function records the allocation order used in the last equalization operation, if Kswapd_max_order is greater than the last value, or if CLASSZONE_IDX is less than the previous value, then call Balance_pgdat to balance the memory domain again, or you can hibernate for a short time. The time to sleep is HZ/10, and for Arm (HZ=100), the time to sleep is 1ms.

Balance_pgdat the balanced operation until the ZONE_WARTERMARK_OK of the memory domain.

4.2 Cache_reap

Cache_reap is used to reclaim idle objects in the slab, and if the free object can be restored to a page, it is released back to the buddy system. Each call to Cache_reap will iterate through all the slab_caches, then hibernate 2*hz, and for Arm (HZ=100), the period is 20ms.

5 Reference Documents

(1) "Understanding the Linux kernel"

(2) "Professional Linux kernel Architecture"

Linux kernel Memory recovery mechanism

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.