Table of Contents
1 Time to recycle" >1 Time to recycle
2 Which memory can be recycled" >2 Which memory can be recycled
4 Frequency of periodic recycling" >4 Frequency of periodic recycling
5 References" >5 References
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

Feb 14, 2024 pm 07:51 PM
linux linux tutorial linux system linux command shell script arrangement embeddedlinux Getting started with linux linux learning

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

vscode cannot install extension vscode cannot install extension Apr 15, 2025 pm 07:18 PM

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

Can vscode be used for mac Can vscode be used for mac Apr 15, 2025 pm 07:36 PM

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

What is vscode What is vscode for? What is vscode What is vscode for? Apr 15, 2025 pm 06:45 PM

VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages ​​and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.

How to use VSCode How to use VSCode Apr 15, 2025 pm 11:21 PM

Visual Studio Code (VSCode) is a cross-platform, open source and free code editor developed by Microsoft. It is known for its lightweight, scalability and support for a wide range of programming languages. To install VSCode, please visit the official website to download and run the installer. When using VSCode, you can create new projects, edit code, debug code, navigate projects, expand VSCode, and manage settings. VSCode is available for Windows, macOS, and Linux, supports multiple programming languages ​​and provides various extensions through Marketplace. Its advantages include lightweight, scalability, extensive language support, rich features and version

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

See all articles