Memory recovery method and device, electronic equipment and readable storage medium

By using kernel threads in electronic devices to traverse and recycle the virtual address space of the process, the problem of low memory recycling efficiency when the process's virtual address space is large is solved, and more efficient memory recycling performance is achieved.

CN120179407APending Publication Date: 2025-06-20VIVO MOBILE COMM CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510343006.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-03-21
Publication Date
2025-06-20

AI Technical Summary

Technical Problem

In electronic devices, when the virtual address space of the process is large, the memory recovery efficiency is low, resulting in a degradation of device performance.

Method used

When the first process applies for memory, it obtains the amount of free memory of the electronic device, and when the amount of free memory is less than or equal to the first threshold, a kernel thread is used to perform traversal operations on the virtual address space of the process, separates the hot page and the cold page, and performs memory recycling operations based on these two types of memory pages.

Benefits of technology

The memory recycling tasks are executed in parallel through kernel threads, which avoids the main process being blocked due to the long memory recycling time, and improves the memory recycling efficiency of electronic devices.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120179407A_ABST
    Figure CN120179407A_ABST
Patent Text Reader

Abstract

The invention discloses a memory recovery method and device, electronic equipment and a readable storage medium, and belongs to the field of memory management. The method comprises the steps that under the condition that a first process applies for a memory, the idle memory amount of the electronic equipment is acquired; the first process is a process of a preset category; under the condition that the free memory amount is smaller than or equal to a first threshold value, executing traversal operation on a virtual address space of a process through a kernel thread to obtain at least two types of memory pages; the access popularity of the at least two types of memory pages is different; and executing memory recovery operation according to the at least two types of memory pages through the first process.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application belongs to the technical field of memory management, and particularly relates to a memory recycling method, device, electronic device, and readable storage medium. Background Art

[0002] With the development of electronic device technology, electronic devices play an increasingly important role in users' daily lives, and the performance of electronic devices directly affects users' usage experiences. During the operation of an electronic device, processes frequently apply for memory. When the available memory is insufficient, the process needs to recycle memory first before it can continue to apply for memory.

[0003] In related technologies, when an electronic device performs memory recycling, it is usually completed by the process that applies for memory. Specifically, when recycling memory, the process needs to traverse the virtual address space to find memory pages that can be recycled. However, when the virtual address space of the process is large, the process takes a long time to traverse the virtual address space, resulting in poor memory recycling efficiency of the electronic device. Summary of the Invention

[0004] The purpose of the embodiments of this application is to provide a memory recycling method, device, electronic device, and readable storage medium, which can improve the memory recycling efficiency of the electronic device.

[0005] In a first aspect, the embodiments of this application provide a memory recycling method, which includes: when a first process applies for memory, obtaining the available memory amount of the electronic device; the first process is a process of a preset category; when the available memory amount is less than or equal to a first threshold, through a kernel thread, performing a traversal operation on the virtual address space of the process to obtain at least two types of memory pages; the access hotness of the at least two types of memory pages is different; through the first process, performing a memory recycling operation according to the at least two types of memory pages.

[0006] In a second aspect, the embodiments of this application provide a memory recycling device, which includes: a processing module; the processing module is used to obtain the available memory amount of the electronic device when a first process applies for memory; the first process is a process of a preset category; the processing module is used to perform a traversal operation on the virtual address space of the process through a kernel thread to obtain at least two types of memory pages when the available memory amount is less than or equal to a first threshold; the access hotness of the at least two types of memory pages is different; the processing module is used to perform a memory recycling operation according to the at least two types of memory pages through the first process.

[0007] In a third aspect, an embodiment of the present application provides an electronic device, which includes a processor and a memory. The memory stores a program or instruction that can run on the processor. When the program or instruction is executed by the processor, the steps of the method described in the first aspect are implemented.

[0008] In a fourth aspect, an embodiment of the present application provides a readable storage medium, on which a program or instruction is stored. When the program or instruction is executed by a processor, the steps of the method described in the first aspect are implemented.

[0009] In a fifth aspect, an embodiment of the present application provides a chip, which includes a processor and a communication interface. The communication interface is coupled to the processor, and the processor is used to run a program or instruction to implement the method described in the first aspect.

[0010] In a sixth aspect, an embodiment of the present application provides a computer program product, which is stored in a storage medium and is executed by at least one processor to implement the method described in the first aspect.

[0011] In the embodiment of the present application, when a first process applies for memory, the free memory amount of the electronic device is obtained; the first process is a process of a preset category; when the free memory amount is less than or equal to a first threshold, a kernel thread performs a traversal operation on the virtual address space of the process to obtain at least two types of memory pages; the access heat of the at least two types of memory pages is different; through the first process, a memory recovery operation is performed according to the at least two types of memory pages. Through this method, the electronic device can perform a traversal operation on the virtual address space of a process of a preset category through a kernel thread, and the main process and the kernel thread execute the memory recovery task in parallel, so as to improve the performance of memory recovery for the process of the preset category, and further improve the memory recovery efficiency of the electronic device. BRIEF DESCRIPTION OF THE DRAWINGS

[0012] Figure 1 is a schematic flowchart of the memory recovery method provided by the embodiment of the present application;

[0013] Figure 2 is a schematic diagram of the memory recovery method provided by some embodiments of the present application;

[0014] Figure 3 is a schematic flowchart of the memory recovery method provided by some embodiments of the present application;

[0015] Figure 4 is a schematic flowchart of the memory recovery method provided by some embodiments of the present application;

[0016] Figure 5It is a schematic flowchart of a memory recycling method provided by some embodiments of the present application;

[0017] Figure 6 It is a schematic diagram of a memory recycling method provided by some embodiments of the present application;

[0018] Figure 7 It is a schematic flowchart of a memory recycling method provided by some embodiments of the present application;

[0019] Figure 8 It is a schematic diagram of a memory recycling method provided by some embodiments of the present application;

[0020] Figure 9 It is a schematic flowchart of a memory recycling method provided by some embodiments of the present application;

[0021] Figure 10 It is a schematic structural diagram of a memory recycling device provided by some embodiments of the present application;

[0022] Figure 11 It is a schematic structural diagram of an electronic device provided by some embodiments of the present application;

[0023] Figure 12 It is a schematic hardware structure diagram of an electronic device provided by some embodiments of the present application. Detailed implementation manners

[0024] Next, the technical solutions in the embodiments of the present application will be clearly described in conjunction with the accompanying drawings in the embodiments of the present application. Obviously, the described embodiments are some, rather than all, embodiments of the present application. Based on the embodiments in the present application, all other embodiments obtained by those of ordinary skill in the art belong to the scope of protection of the present application.

[0025] The terms "first", "second", etc. in the specification and claims of the present application are used to distinguish similar objects, rather than to describe a specific order or sequence. It should be understood that such terms can be interchanged under appropriate circumstances so that the embodiments of the present application can be implemented in an order different from those illustrated or described herein, and the objects distinguished by "first", "second", etc. generally belong to the same category, and the number of objects is not limited. For example, the first object can be one or more. In addition, "and / or" in the specification and claims means at least one of the connected objects, and the character " / " generally indicates an "or" relationship between the associated objects before and after.

[0026] The terms "at least one (item)", "at least one of", etc. in the description and claims of this application refer to any one, any two or more combinations of the objects they contain. For example, at least one (item) of a, b, and c can represent: "a", "b", "c", "a and b", "a and c", "b and c", and "a, b, and c", where a, b, and c can be single or multiple. Similarly, "at least two (items)" means two or more, and its meaning is similar to that of "at least one (item)".

[0027] The memory recycling method provided by the embodiment of this application can be applied to the scenario where the User Interface (UI) process is running on an electronic device and memory recycling is required.

[0028] When the UI process is running on an electronic device and memory recycling is required, first, when the UI process (i.e., the first process) applies for memory, the electronic device first obtains the currently available free memory amount of the system by calling the system memory management module; when the free memory amount of the electronic device is less than or equal to the first threshold, the kernel thread asynchronously and in parallel performs a traversal operation on the virtual address space of the process, and divides the memory pages corresponding to the process address space into hot pages and cold pages (i.e., the access heat of at least two types of memory pages is different); finally, the UI process performs a memory recycling operation according to the hot pages and cold pages. In this way, the electronic device can asynchronously execute the memory recycling task through the kernel thread, thereby avoiding the first process from being blocked due to the long time-consuming memory recycling, and further improving the fluency of the electronic device when running the preset type of process.

[0029] The execution entity of the memory recycling method provided by the embodiment of this application can be a memory recycling device, and this memory recycling device can be an electronic device, or a functional module or functional entity in the electronic device. The following takes the electronic device executing the memory recycling method as an example to illustrate the technical solution provided by the embodiment of this application.

[0030] The following combines the drawings to explain in detail the memory recycling method provided by the embodiment of this application through specific embodiments and their application scenarios.

[0031] Figure 1 It is a schematic flowchart of the memory recycling method provided by the embodiment of this application, as Figure 1 shown, this memory recycling method can include the following steps 201 to step 203:

[0032] Step 201: When the first process applies for memory, the electronic device obtains the free memory amount of the electronic device.

[0033] Wherein, the above-mentioned first process is a preset type of process.

[0034] In some embodiments of the present application, the above-mentioned processes of the preset category may include processes directly related to the user experience, such as the interactive interface category, wallpaper category, game category, etc., or processes with high real-time requirements. Such processes usually have high requirements for the timeliness of memory application. If the process is blocked, it will cause frame drops and lags, affecting the user experience.

[0035] Exemplarily, the UI process (such as the main thread of the mobile phone desktop or application interface) needs to frequently apply for memory to update the interface content during operation, and the rendering process (such as the graphics rendering or video decoding process) requires a large amount of memory to process image and video data.

[0036] In some embodiments of the present application, the above-mentioned free memory may include the total amount of available physical memory remaining in the electronic device; or the allocable memory value obtained after being calculated by the memory management module, and this value reflects the size of the memory resources that the electronic device can currently allocate.

[0037] Exemplarily, the free memory of the electronic device may be 1MB.

[0038] Exemplarily, when the electronic device runs the UI process and needs to apply for memory to update the display content, the electronic device obtains the current remaining free memory amount, such as 10MB.

[0039] Step 202: When the free memory amount is less than or equal to the first threshold, the electronic device performs a traversal operation on the virtual address space of the process through the kernel thread to obtain at least two types of memory pages.

[0040] Among them, the access heat of the above-mentioned at least two types of memory pages is different.

[0041] In some embodiments of the present application, the above-mentioned first threshold may be a preset memory water level value.

[0042] It should be noted that the above-mentioned first threshold is used to trigger the memory recycling operation, and the value of the first threshold can be determined or adjusted according to the memory capacity, design requirements, and user experience requirements of the electronic device.

[0043] Exemplarily, if the total memory size of the electronic device is 4GB, the first threshold can be set to 256MB. Then, when there is a process applying for memory and the electronic device obtains that the free memory amount is less than or equal to 256MB, the memory recycling operation will be triggered so that the process can apply for memory.

[0044] In some embodiments of the present application, the above-mentioned kernel thread may include a kernel thread dedicated to the memory recycling task.

[0045] In some embodiments of the present application, this thread can be executed asynchronously and in parallel with the first process to avoid the memory recycling work from blocking the first process.

[0046] In some embodiments of the present application, the above process address space traversal operation may include a kernel thread scanning the virtual address space of a process to identify the memory page access status.

[0047] In some embodiments of the present application, the virtual address space of a process may be a continuous virtual memory space allocated by the operating system to each process, including a kernel space, a user space, a shared memory area, and a system reserved area. Among them, the kernel space is the storage area for the operating system kernel code and data; the user space is the storage area for user process code and data; the shared memory area is the storage area for sharing data among multiple processes; the system reserved area is the storage area for the operating system internal data structure and cache.

[0048] It should be noted that the above virtual address space of the process may also be referred to as mm (mm_struct, mm), and the above process address space traversal operation may also be referred to as mm walk.

[0049] In some embodiments of the present application, the above at least two types of memory pages may include young generation memory pages and old generation memory pages; or, the above at least two types of memory pages may include young generation memory pages, second young generation memory pages, second old generation memory pages, and old generation memory pages.

[0050] In some embodiments of the present application, the above memory pages may include at least one of file pages and anonymous pages.

[0051] In some embodiments of the present application, the above memory pages may include the smallest unit for the operating system of the electronic device to manage memory. Each mapped memory page has a corresponding page table entry (PTE) for recording information such as the physical address, access permission, and access status of the page.

[0052] Exemplarily, the size of each memory page may be 4KB.

[0053] In some embodiments of the present application, the above access heat may include a metric standard for distinguishing hot pages and cold pages based on classification criteria such as page access frequency or recent access time.

[0054] Exemplarily, pages with a high access frequency or recently accessed have a higher access heat and are considered hot pages, while pages with a low access frequency or not accessed for a long time have a lower access heat and are considered cold pages.

[0055] Exemplarily, if the first threshold is 256 MB, when the first process applies for memory and the electronic device obtains that the current free memory is less than or equal to 256 MB, the kernel thread traverses the virtual address space of the process to obtain two types of memory pages, namely hot pages and cold pages.

[0056] Step 203: The electronic device performs a memory recycling operation through the first process according to at least two types of memory pages.

[0057] In some embodiments of the present application, the above memory recycling may include recycling cold pages to release memory space, and the recycled cold pages can be reallocated by the electronic device to the process that applies for memory (such as the first process).

[0058] Exemplarily, after the kernel thread completes the address space traversal, the first process reclaims the identified cold pages from the memory and adds the reclaimed memory pages to the free memory.

[0059] Again exemplarily, in the operating system of the electronic device, the lru (Least Recently Used) algorithm is used for memory management, and the least recently used memory pages are recycled by distinguishing the cold and hot degrees of the memory pages.

[0060] It should be noted that a hot page refers to a page that has been recently accessed and is likely to be accessed again recently according to the locality principle, so it cannot be recycled immediately and is placed in the active lru list. A cold page refers to a memory page that remains after distinguishing hot pages and is placed in the inactive lru list. Then, when performing memory recycling, starting from the tail of the inactive lru list, each memory page is traversed again to determine whether it is a hot page. If the memory page becomes a hot page, it is placed in the active lru list. If it is still a cold page, it can be recycled immediately.

[0061] In this way, through this method, the electronic device can perform a virtual address space traversal operation on the virtual address space of a preset type of process through the kernel thread, and the main process and the kernel thread execute the memory recycling task in parallel, so as to improve the performance of memory recycling for the preset type of process, and further improve the memory recycling efficiency of the electronic device.

[0062] In some embodiments of the present application, the process of obtaining at least two types of memory pages by traversing the virtual address space of the process through the kernel thread in the above step 202 may include the following steps 202a to 202c:

[0063] Step 202a: The electronic device traverses the virtual address space linked list corresponding to the first process group through the kernel thread.

[0064] Among them, the above virtual address space linked list is used to indicate the virtual address spaces corresponding to at least one process in the above first process group.

[0065] Step 202b: The electronic device determines page table entries corresponding to at least one process according to the virtual address space.

[0066] Step 202c: The electronic device classifies memory pages of at least one process according to the access flag bits of the page table entries corresponding to at least one process, and obtains at least two types of memory pages.

[0067] In some embodiments of the present application, the above first process group may be a set including multiple processes, and the electronic device may control the use of memory resources of a group of processes by managing the process group.

[0068] Exemplarily, the first process group may be a set including multiple background processes, which is used to manage background tasks of the electronic device.

[0069] It should be noted that the above process group may be represented by memcg (Memory Control Group, memcg).

[0070] In some embodiments of the present application, the above virtual address space linked list may include a virtual address space linked list corresponding to a memcg, and the address spaces of all processes under the same memcg are strung in a linked list.

[0071] It should be noted that the above virtual address space linked list may also be referred to as mm list (mm_struct lrulist, mm list).

[0072] In some embodiments of the present application, the above page table entry may include a structure for describing the page state in the virtual address space of a process, and each page table entry (Page Table Entry, PTE) stores the physical address, access permission, access flag, and other status information related to page management of the page.

[0073] In some embodiments of the present application, the above AF (Access Flag, AF) flag bit may include an access flag of a memory page, and the AF flag bit is used to mark whether the page has been accessed recently.

[0074] Exemplarily, the AF flag bit may be set, and setting it means it is a hot page, indicating that the memory page has been accessed recently.

[0075] Exemplarily, the electronic device can determine whether the AF flag bit in the PTE corresponding to the memory page is set by using the Reverse Mapping (RMAP) algorithm. Since the memory page may correspond to the physical memory shared by entities, that is, mapped by the virtual addresses of multiple processes, there are multiple PTE mappings. The RMAP algorithm can find all PTE mappings of the memory page through reverse mapping, clear the AF flag from them, and cool down the memory page. When processing the same memory page again, if the AF flag bit is not set, that is, the memory page has not been accessed again, it means that the memory page has cooled down and can be recycled.

[0076] It should be noted that when using the RMAP algorithm to process memory pages with a large number of PTE mappings, if each PTE is to be traversed completely, it takes a long time and may cause frame drops and lags on the electronic device. In addition, when using the RMAP algorithm, there are many accessing processes, and only a very small part of the address space corresponding to these processes is accessed, so the power consumption is high, especially in embedded systems. Using the mglru (Multi-Generational lru, mglru) algorithm can optimize the above problems. Its main principle is to divide the lru into generations and levels, more finely distinguish between hot and cold pages, and at the same time introduce mm walk to reduce the time-consuming of reverse mapping during the memory recycling process.

[0077] The following combines Figure 2 to give an exemplary description of the mglru algorithm.

[0078] Exemplarily, as Figure 2 shown in (A) of [reference], the vertical axis coordinate on the left represents the memory size, and there are four columns with horizontal stripes, diagonal stripes, vertical stripes, and lattice patterns distributed from left to right, which are used to represent 4 generations of lru linked lists arranged from high to low according to heat.

[0079] It should be noted that the above generations can also be referred to as Gen (Generations, Gen). In some cases, there can be at most 4 generations (MAX_NR_GENS) and at least 2 generations (MIN_NR_GENS). The gen information of the memory page is stored in bit[53:51] in the memory page flag bit.

[0080] Exemplarily, as Figure 2 shown in (B) of [reference], Figure 2 in (B) of [reference], there are 4 generations of lru, namely the young generation, the sub-young generation, the sub-old generation, and the old generation, distributed from top to bottom. There are two lrus in each generation, one for storing file pages and the other for storing anonymous pages. A rectangular grid on each lru represents a memory page, and the pattern of the rectangular grid represents the level of the memory page, that is, the level of the lattice pattern is 0, the level of the vertical stripe is 1, the level of the diagonal stripe is 2, and the level of the horizontal stripe is 3.

[0081] It should be noted that for the memory pages in each generation, not only are file pages and anonymous pages distinguished, but different levels are also distinguished. The levels are divided according to the number of times the memory pages are accessed. The more times a memory page is accessed, the higher the level, and the levels can more finely distinguish between hot and cold pages. In addition, the number of times a memory page is accessed is stored in bit[50:49] in the flag bit of each memory page.

[0082] The following combines Figure 3 to exemplarily illustrate the principle and process of the mglru algorithm.

[0083] Exemplarily, in the mglru algorithm, the default setting is a maximum of 4 generations and a minimum of 2 generations. The iterative process between generations polls like a clock, and the recycling pointer points to the old generation. When recycling, the pages in the old generation are traversed. Cold pages will be recycled, and hot pages will be placed in the young generation. When the memory pages in the old generation pointed to by the recycling pointer are completely recycled, the recycling pointer will rotate clockwise, and the generation pointed to after rotation will be used as the new old generation. When the number of generations between the old generation and the young generation is 0, that is, only the old generation and the young generation remain, an aging operation needs to be performed.

[0084] Exemplarily, the aging process first performs an mm walk operation. The following combines Figure 4 to exemplarily illustrate the steps and process of performing an mm walk on the mms in the mm list corresponding to memcg.

[0085] As Figure 4 shown, in some examples, the process of mm walk may include the following steps:

[0086] Step 41: Whether to enable the mm walk function.

[0087] Exemplarily, mm walk can timely distinguish between hot and cold pages, reduce reverse mapping operations, and improve memory recycling efficiency. However, in the case where there are relatively few hot pages in the process, mm walk will consume more computing resources. Therefore, the mm walk function can be selectively enabled.

[0088] Step 42: Traverse the mm_list from the beginning to obtain the process address space mm.

[0089] Exemplarily, all mms under the same memcg are strung in an mm list linked list. The mm list structure is as Figure 5 shown, Figure 5 where 501 in it represents the mms that have been scanned in the current round of iteration; Figure 5 where 502 in it represents the mms to be scanned in the current round of iteration, but this mm has not been scheduled after the previous scan and does not need to be scanned; Figure 5The 505 in it indicates the mm to be scanned in this round of iteration. This mm was scheduled again after the previous scan and needs to be scanned again; Figure 5 The 504 in it indicates the mm to be scanned in this round of iteration, and it is a newly created process that has just been newly added to the mm list, but has not been scheduled to execute yet and does not need to be scanned; Figure 5 The 505 in it indicates the mm to be scanned in this round of iteration. It is a newly created process that has just been newly added to the mm list and has just been scheduled to execute and needs to be scanned.

[0090] Step 43: Obtain the mm->mmap_lock lock. Traverse the virtual address space of the process from next_addr to ULONG_MAX to obtain each vma.

[0091] Exemplarily, after obtaining a process address space mm that needs to be scanned, the mm->mmap_lock lock needs to be obtained first to protect the list of the process's virtual memory areas (vm_area_struct, vma), ensuring thread safety when traversing and modifying the virtual address space. This lock can prevent multiple threads from modifying the vma simultaneously, thus providing mutual exclusion protection for the vma and solving the data inconsistency problem. Then traverse the entire virtual address space of the process to find each vma. next_addr is a dynamic variable used to record the starting address of the current traversed process virtual address space, and ULONG_MAX represents the upper limit of the address space. Using ULONG_MAX as the upper limit can avoid address overflow problems and ensure the security of the traversal operation.

[0092] Step 44: This vma needs to be skipped.

[0093] Exemplarily, not all of the found vmas need to be traversed because not all the memory of the vmas can be reclaimed, and the memory pages that cannot be reclaimed do not need to be distinguished between hot and cold. For example, the image memory applied for by the direct memory access buffer dma-buf (Direct Memory Access Buffer, dma-buf) cannot be reclaimed by the system and can only be actively released by the user space. In addition, if there is no swap partition, the anonymous pages cannot be reclaimed either. For the vmas in the above situations, they need to be skipped and no traversal operation is performed.

[0094] Step 45: Traverse the vma address space to obtain each PMD.

[0095] Exemplarily, after obtaining a valid vma, traverse the address space of the vma to obtain all the page middle directories (PMDs). However, not all memory pages have PMDs. Only when there is a mapping relationship between the virtual address and the physical address will a page table entry (PTE) be generated. Only when there is a PTE will the corresponding PMD be generated.

[0096] Step 46:!force_scan && the PMD is not set in the Bloom filter.

[0097] It should be noted that the Bloom filter is used to store the possibly valid PMDs. Since the virtual address space of the process is very sparse, if all the access states of the PTEs are traversed, it will take a long time. With the help of the Bloom filter, it can provide guidance for which PMDs to traverse when traversing the process address space later. As Figure 6 shown, the data structure of the Bloom filter is a two-dimensional bitmap. Each bit is used to identify the state of the PMD. When the bit is set, it means that there are more hot pages in the PMD. The two dimensions represent two generations. Each time an aging operation is performed and the aging pointer is rotated, the Bloom filter also needs to be iterated.

[0098] Exemplarily, for a newly created process that has been scheduled for execution, the mm of this process needs to be forced to scan without referring to the Bloom filter. For a process that is not newly created, the process has a new execution opportunity between two scans, that is, such a process may have accessed new memory pages, and the memory pages accessed by such a process may become hot pages and need to be scanned. However, it is not necessary to scan the entire address space. Only the PMDs filtered out by the Bloom filter need to be scanned.

[0099] Step 47: Traverse all the PTEs under the PMD. If the PTE exists and the AF flag is set, clear the flag, obtain the memory page corresponding to the PTE, set bit[53:51] of the flag bit of the memory page to the young generation, and increase the statistical count.

[0100] Exemplarily, for a system with a single memory page size of 4KB, if a three-level page table is adopted, one PMD manages 512 PTEs. Detect each valid PTE under this PMD (only PTEs with physical memory mapping are valid). There is an AF flag bit in the PTE. If the AF flag bit is set, it indicates a hot page that has just been accessed. For such PTEs, first clear its AF flag, obtain the memory page corresponding to this PTE, set bit[53:51] of the page flag bit to the young generation (this means that when this memory page is encountered in the recycling process, there is no need to find the PTE through reverse mapping to determine its cold or hot state, and this hot page can be immediately put into the young generation), and increase the statistical count.

[0101] Step 48: Check if the increased statistical count exceeds 4096.

[0102] Step 49: Briefly release the mm->mmap_lock lock to avoid starvation of other threads in the same process group.

[0103] Exemplarily, steps 48 and 49 are the number of PTEs allowed to be effectively processed in step 47. If the statistical count is greater than 4096, the mm->mmap_lock lock needs to be briefly released. The mm->mmap_lock lock is responsible for managing the entire process's address space. Page faults and memory mapping both need to hold this lock. Therefore, the performance of this lock will also affect the overall system performance and cannot be held for a long time to avoid starvation of other threads in the same process because they cannot obtain this lock (if these threads cannot obtain the lock for a long time, they will be in a waiting state for a long time and cannot continue to execute. This situation is called "starvation").

[0104] Step 410: If the number of PTEs with the AF flag set in this PMD reaches a certain quantity, set the corresponding valid bit of this PMD in the Bloom filter to 1.

[0105] Exemplarily, when the number of PTEs with the AF flag set in this PMD reaches a certain quantity, that is, when the number of hot pages corresponding to 512 PTEs reaches a certain amount, the PMD is considered hot. For example, if the certain quantity limit is set within [1 / 8, 1 / 2], the specific setting is related to the size of the cache line. At this time, for 512 valid PTEs, at least 64 hot pages are required to consider the PMD hot. Such PMDs are the PMDs filtered by the Bloom filter in step 46 above, that is, the valid bits are set in the Bloom filter filtering.

[0106] Step 411: All mms have been iterated through.

[0107] Exemplarily, it is determined whether the mm list scanning is completed. If not, the next mm needs to be selected for continued scanning. If the scanning is completed, the state of the mm list at this time is as Figure 7 shown. Both the head node and the tail node point to the head node at this time. If a new process is created, it is added to the tail of the linked list and pointed to by the tail node.

[0108] Step 412: Whether the total number of generations has reached the maximum number of generations 4.

[0109] Step 413: Transfer the memory pages in the old generation to the second-oldest generation, and rotate the reclaim pointer clockwise to point to the second-oldest generation as the new old generation.

[0110] Step 414: Rotate the aging pointer clockwise to point to the empty space vacated by the previous old generation, and update the statistical count.

[0111] Exemplarily, for steps 412 to 414, if the mm list has completed one round of scanning, aging needs to be continued. If the total number of generations has reached the maximum number of generations 4 at this time, rotate the aging pointer clockwise, as Figure 3 shown. At this time, the aging pointer points to the young generation. If rotated clockwise, it will coincide with the reclaim pointer (which also points to the old generation at this time). The purpose of the aging operation is to promote hot pages from the old generation to the young generation to avoid incorrect reclamation of hot pages. If the reclaim pointer and the aging pointer coincide, it will cause the aging operation and the reclaim operation to occur simultaneously on the same generation, and the management logic of hot pages and cold pages will conflict, which will further lead to incorrect reclamation of hot pages, thus affecting the system performance. To avoid such a situation, at this time, all the memory pages in the old generation need to be transferred to the second-oldest generation before rotating the aging pointer, as Figure 3 shown, and the memory pages in the old generation are transferred to the second-oldest generation.

[0112] Exemplarily, after the above mm walk operation, continue with the aging operation and rotate the aging pointer clockwise, as Figure 3 shown. The aging pointer will point to the old generation. At this time, in the old generation, cold pages are reclaimed and hot pages are put into the young generation. Or, when the total number of generations is 4, all the memory pages in the old generation are transferred to the second-oldest generation. That is to say, there are no memory pages in the old generation at this time, and it is an empty generation. And the aging pointer points to this empty old generation, indicating that this empty old generation has become the new young generation, and subsequent hot pages will be added to this new young generation. From the above steps, it can be seen that hot pages keep transferring to the young generation, and cold pages will naturally settle.

[0113] When performing memory recycling, the Proportional-Integral-Derivative Control Algorithm (PID control algorithm) can be used. The main purpose of the PID control algorithm is to reasonably select a type and a level for recycling from the types (anonymous pages or file pages) and levels (levels 0 to 3) of the lru, so as to reduce memory thrashing (memory thrashing occurs when a memory page needs to be accessed immediately after being recycled). Taking the calculated value of the proportional-integral unit (the derivative unit is not used) of level 0 of the anonymous page as the reference value, and the calculated value of the proportional-integral unit of level 0 of the file page as the process variable, compare the reference value and the process variable, and recycle the type with the smaller calculated value. The same principle applies when selecting the recycling level after determining the recycling type, that is, by comparing the calculated values of the proportional-integral units of different levels, select the level with the smaller calculated value for recycling.

[0114] In this way, the electronic device can accurately identify hot pages and cold pages according to the access flag bits of the page table entries corresponding to the processes, reduce the consumption of computing resources during the memory recycling process, thereby improving the performance of memory recycling for processes of a preset category, and further improving the memory recycling efficiency of the electronic device.

[0115] In some embodiments of the present application, before the above step 202a, the memory recycling method provided by the present application may further include the following steps 204 to 205:

[0116] Step 204: The electronic device obtains a process group linked list.

[0117] Wherein, the above process group linked list is used to indicate at least one process group.

[0118] Step 205: The electronic device determines a first process group according to the process group linked list.

[0119] In some embodiments of the present application, the above process group linked list may be represented as memcg lru, that is, the least recently used linked list based on the process group.

[0120] Exemplarily, the process group linked list may be a global memcg lru linked list, where each node represents a memcg unit.

[0121] Exemplarily again, the electronic device can obtain the memcg lru linked list through the memory management system, and determine a memcg unit from the memcg lru linked list as the first process group.

[0122] In this way, the electronic device can determine the first process group according to the process group linked list, so as to determine the process group that is most likely to require memory recycling, improve the performance of memory recycling for processes of a preset category, and further improve the memory recycling efficiency of the electronic device.

[0123] In some embodiments of the present application, before step 202 above, the memory recycling method provided by the present application may further include the following steps 206 to 207:

[0124] Step 206: The electronic device obtains context parameters.

[0125] Among them, the above context parameters include relevant parameters for performing a traversal operation on the virtual address space of the process;

[0126] Step 207: The electronic device adds the context parameters to the global linked list.

[0127] In some embodiments of the present application, the above global linked list is used to save context parameters.

[0128] In some embodiments of the present application, the above context parameters may include a pointer to the mm list, a serial number for distinguishing the young generation in the current round, whether to use a Bloom filter, that is, a flag indicating whether forced scanning is required, and other parameters for instructing the kernel thread to perform an mm walk operation.

[0129] It should be noted that the above mm list pointer is used to indicate the object processed by the kernel thread, that is, to indicate the target mmlist.

[0130] In some embodiments of the present application, the above global linked list may include a doubly linked list for storing all context parameters to be processed, so that the kernel thread can obtain and execute an mm walk operation from it.

[0131] In some embodiments of the present application, the electronic device may generate a linked list node object after integrating the context parameters according to the context parameters, and add the linked list node object to the global linked list.

[0132] In this way, the electronic device can add the context parameters to the global linked list, so as to provide necessary information and context support for the kernel thread to asynchronously execute an mm walk, prepare for implementing the memory recycling tasks in parallel by the main process and the kernel thread later, and further improve the memory recycling efficiency of the electronic device.

[0133] In some embodiments of the present application, step 207 may further include the following steps 207a and 207b:

[0134] Step 207a: The electronic device determines a linked list node object according to the context parameters.

[0135] Step 207b: The electronic device adds the linked list node object to the global linked list.

[0136] In some embodiments of the present application, the above-linked list node object may include a data structure for storing context parameters and other information related to the mm walk operation.

[0137] Exemplarily, the linked list node object may include the following fields:

[0138] struct list_head list; used to hang the node into the global linked list;

[0139] struct mm_walk_entry*entry; pointing to the structure for storing context parameters;

[0140] spinlock_t lock; used to protect the access to the linked list node to ensure thread safety.

[0141] In this way, the electronic device can generate a linked list node object according to the context parameters and add the linked list node object to the global linked list, so as to provide necessary information and context support for the kernel thread to asynchronously execute mm walk, prepare for the subsequent parallel execution of the memory reclaiming task by the main process and the kernel thread, and further improve the memory reclaiming efficiency of the electronic device.

[0142] In some embodiments of the present application, in the above step 202, the kernel thread performs a traversal operation on the virtual address space of the process to obtain at least two types of memory pages; the following steps 202d and 202e may also be included:

[0143] Step 202d: The electronic device obtains context parameters from the global linked list through the kernel thread.

[0144] Step 202e: Through the kernel thread, perform a traversal operation on the virtual address space of the process according to the context parameters to obtain at least two types of memory pages.

[0145] In some embodiments of the present application, the electronic device may obtain a linked list node object from the global linked list through the kernel thread, parse the context parameters from the linked list node object, and perform mm walk on the mm list pointed to by the mm list according to the operation parameters indicated by the context parameters to obtain at least two types of memory pages with different degrees of heat.

[0146] In this way, the electronic device can obtain context parameters from the global linked list, enabling the kernel thread to independently complete the mm walk operation, implementing parallel execution of the memory reclaiming task by the main process and the kernel thread, thereby improving the speed of memory reclaiming for the preset category of processes, and further improving the memory reclaiming efficiency of the electronic device.

[0147] In some embodiments of the present application, step 203 may further include the following steps 203a and 203b:

[0148] Step 203a: The electronic device determines the memory pages to be reclaimed from at least two types of memory pages through a first process based on the memory reclaiming parameters.

[0149] Step 203b: The electronic device performs memory reclaiming on the memory pages to be reclaimed.

[0150] In some embodiments of the present application, the above memory reclaiming parameters include at least one of the following:

[0151] The type of memory pages to be reclaimed;

[0152] The page level of the memory pages to be reclaimed.

[0153] In some embodiments of the present application, the above memory pages to be reclaimed may include memory pages with low access heat, i.e., cold pages.

[0154] Exemplarily, the memory pages to be reclaimed may be pages with low access heat and not marked as hot pages screened from the old generation.

[0155] In some embodiments of the present application, the above type of memory pages to be reclaimed may include anonymous pages and file pages.

[0156] Exemplarily, an anonymous page refers to a memory page not mapped to a specific file, usually used for the heap and stack spaces of a process; a file page refers to a memory page mapped to a disk file, such as cached file data. When reclaiming memory, anonymous pages or file pages can be preferentially reclaimed according to system requirements.

[0157] In some embodiments of the present application, the above page level of the memory pages to be reclaimed may include dividing the memory pages into a total of 4 levels from level 0 to level 3 according to the access times of the memory pages.

[0158] Exemplarily, as Figure 8 shown, Figure 8 in (A) represents the memory reclaiming process of the conventional mglru algorithm, where both the mm walk operation and the reclaiming of memory pages need to be completed by the main process, while Figure 8In (B), it represents the memory recycling method provided by the embodiments of the present application. For processes of a preset category, the main process completes the work of recycling memory pages, and the kernel thread completes the mm walk operation.

[0159] Exemplarily, when the first process performs recycling, it will recycle the memory pages in the old generation. The specific process is as follows: First, use the PID algorithm to select the type of memory page to be recycled and the page level of the memory page; after the type and level are selected, traverse the memory pages in the lru linked list (there are two lru linked lists in one generation, which store anonymous pages and file pages respectively). If the memory page is a hot page, put it into the young generation; if the memory page is not a hot page, but its page level is higher than the currently selected level to be recycled, put it into the previous generation, that is, the second-oldest generation for protection. The remaining other memory pages are cold pages to be recycled. Isolate the memory pages to be recycled into a separate linked list, and then perform a recycling operation on each memory page according to this separate linked list.

[0160] The following combines Figure 9 to make an exemplary description of the steps and process of the memory recycling method provided by the embodiments of the present application.

[0161] Step 900: The process has a page fault or applies for memory through dma-buf.

[0162] Exemplarily, a page fault of a process means that when a process applies for memory, what it usually applies for is virtual memory. When the process accesses its corresponding physical memory through virtual memory, if there is no physical memory corresponding to the virtual memory, it can directly apply for memory through dma-buf. That is to say, both situations require applying for memory, that is, the situation where the first process applies for memory.

[0163] Step 901: The amount of free memory meets the minimum threshold.

[0164] Exemplarily, check whether the remaining free memory is lower than the min water level (that is, the first threshold). If it is lower than the min water level, then execute the next step.

[0165] Step 902: Traverse the process group linked list to obtain the process group to be recycled.

[0166] Exemplarily, the mglru algorithm will build a memcg lru. The main purpose of building a memcg lru is to be fair to each memcg during memory recycling, allowing the electronic device to traverse all memcgs in the memcg lru and perform operations on each memcg.

[0167] Step 903: Check whether aging is required.

[0168] Exemplarily, when there is no other generation between the young generation and the old generation, that is, when the total number of generations is 2, aging is required to generate a young generation.

[0169] Step 904: Is it a preset category process?

[0170] Exemplarily, it is determined whether the process currently performing memory recycling work is a preset category process. For example, it is set that the preset category process is a user-perceived process, such as an application main thread, a rendering thread, etc. If such a process is blocked, it will cause frame drops and lags in the electronic device.

[0171] Step 905: Save the context parameters to generate a linked list node object and add it to the global linked list.

[0172] Exemplarily, to implement the kernel thread to perform mm walk asynchronously, it is necessary to save some context parameters, as follows:

[0173]

[0174] It includes a data structure mm_walk_entry; a global doubly linked list mw_entry_head; and a spinlock mw_entry_lock that protects the linked list. Some parameters are saved in the mm_walk_entry data structure, as follows:

[0175] The list element is used to hang into the global doubly linked list mw_entry_head;

[0176] lruvec indicates which generation the aging operation is performed on;

[0177] Swappiness is used to determine whether anonymous pages can be recycled, and the ratio of anonymous pages to file pages during memory recycling.

[0178] max_seq is used to distinguish the serial number of the current round of young generation;

[0179] force_scan is used to indicate whether to enable the Bloom filter, that is, whether to forcefully traverse all address spaces of mm.

[0180] Then, insert this data structure into the head of a global doubly linked list mw_entry_head. Then, after waking up the kernel thread responsible for mm walk, the main process of the current process for memory recycling directly returns and starts traversing the next memcg.

[0181] Step 906: Wake up the kernel thread and obtain a linked list node object from the global linked list to perform mm walk operations.

[0182] Exemplarily, after the kernel thread responsible for mm walk is awakened, it will obtain an mm_walk_entry from the tail of the global linked list that stores mm_walk_entry to perform the mm walk operation.

[0183] Step 907: Obtain the linked list of virtual address spaces under the process group and traverse it, select one of the process address spaces, and perform an mm walk operation on it.

[0184] Exemplarily, in the case where the first process is not a preset category process, there is no need to wake up the kernel thread, and the first process performs the mm walk operation.

[0185] Step 908: Perform recycling.

[0186] Exemplarily, when the first process performs recycling, it will recycle the memory pages in the old generation. The specific process is as follows: First, use the PID algorithm to select the type of memory pages to be recycled and the page level of the memory pages; after the type and level are selected, traverse the memory pages in the lru linked list (there are two lru linked lists in a generation, which store anonymous pages and file pages respectively). If the memory page is a hot page, put it into the young generation; if the memory page is not a hot page, but its page level is higher than the currently selected level to be recycled, put it into the previous generation, that is, the second-oldest generation for protection, and the remaining other memory pages are the cold pages to be recycled. Isolate the memory pages to be recycled into a separate linked list, and then perform a recycling operation on each memory page according to the linked list.

[0187] Step 909: The memory allocation system allocates memory for the process.

[0188] Exemplarily, if the free memory in the system is higher than the min water level, the process directly applies for memory from the memory allocation system and then exits directly.

[0189] In this way, the electronic device can independently complete the mm walk operation through the kernel thread and complete the memory page recycling work through the main process, so that the memory recycling task can be executed in parallel by the main process and the kernel thread, improving the speed of memory recycling for the preset category process, and further improving the memory recycling efficiency of the electronic device.

[0190] Each of the above method embodiments, or various possible implementation manners in each method embodiment, can be executed alone, or, on the premise of no contradiction, can also be combined with each other for execution, which can be specifically determined according to actual usage requirements, and the embodiments of the present application do not limit this.

[0191] For the memory recycling method provided by the embodiments of the present application, the execution subject can be a memory recycling device. In the embodiments of the present application, taking the memory recycling device executing the memory recycling method as an example, the memory recycling device provided by the embodiments of the present application is described.

[0192] Figure 10 This is a schematic structural diagram of the memory recycling device provided by the embodiment of the present application. As Figure 10 shown, the memory recycling device 1000 may include an acquisition module 1001, a traversal module 1002, and a memory recycling module 1003, where: the above acquisition module is used to obtain the free memory amount of the electronic device when a first process applies for memory; the first process is a process of a preset category; the above traversal module is further used to perform a traversal operation on the virtual address space of the process through a kernel thread when the free memory amount is less than or equal to a first threshold, and obtain at least two types of memory pages; the access hotness of the at least two types of memory pages is different; the above memory recycling module is further used to perform a memory recycling operation according to the at least two types of memory pages through the first process.

[0193] In some embodiments of the present application, the above device further includes: a determination module and a processing module; the above traversal module is specifically used to traverse the virtual address space linked list corresponding to the first process group through the kernel thread, and the virtual address space linked list is used to indicate the virtual address space corresponding to at least one process in the first process group; the above determination module is specifically used to determine the page table entries corresponding to the at least one process according to the virtual address space; the above processing module is specifically used to classify the memory pages of the at least one process according to the access flag bits of the page table entries corresponding to the at least one process, and obtain at least two types of memory pages.

[0194] In some embodiments of the present application, the above acquisition module is further used to obtain a process group linked list before traversing the virtual address space linked list corresponding to the first process group through the kernel thread, and the process group linked list is used to indicate at least one process group; the above determination module is further used to determine the first process group according to the process group linked list.

[0195] In some embodiments of the present application, the above acquisition module is further used to obtain context parameters before performing a traversal operation on the virtual address space of the process through the kernel thread to obtain at least two types of memory pages, and the context parameters include relevant parameters for performing a traversal operation on the virtual address space of the process.

[0196] In some embodiments of the present application, the above determination module is specifically used to determine a linked list node object according to the context parameters; the above processing module is specifically used to add the linked list node object to the global linked list.

[0197] In some embodiments of the present application, the above-mentioned acquisition module is specifically configured to obtain the above-mentioned context parameters from the above-mentioned global linked list through the above-mentioned kernel thread; the above-mentioned processing module is specifically configured to perform a traversal operation on the virtual address space of the process according to the above-mentioned context parameters through the above-mentioned kernel thread to obtain at least two types of memory pages.

[0198] In some embodiments of the present application, the above-mentioned determination module is specifically configured to determine the memory pages to be recycled from the above-mentioned at least two types of memory pages based on the memory recycling parameters through the above-mentioned first process; the above-mentioned memory recycling module is specifically configured to perform memory recycling on the above-mentioned memory pages to be recycled.

[0199] In some embodiments of the present application, the above-mentioned memory recycling parameters include at least one of the following: the type of memory pages to be recycled; the page level of the memory pages to be recycled.

[0200] The memory recycling device provided by the embodiments of the present application obtains the amount of free memory of the electronic device when the first process applies for memory; the above-mentioned first process is a process of a preset category; when the above-mentioned amount of free memory is less than or equal to the first threshold, a traversal operation is performed on the virtual address space of the process through the kernel thread to obtain at least two types of memory pages; the access hotness of the above-mentioned at least two types of memory pages is different; the above-mentioned first process performs a memory recycling operation according to the above-mentioned at least two types of memory pages. Through this method, the electronic device can perform the memory recycling task in parallel through the main process and the kernel thread, thereby improving the speed of memory recycling of the process of the preset category, and further improving the memory recycling efficiency of the electronic device.

[0201] The memory recovery device in the embodiments of the present application can be an electronic device or a component in an electronic device, such as an integrated circuit or a chip. The electronic device can be a terminal or other devices other than terminals. Exemplarily, the electronic device can be a mobile phone, a tablet computer, a laptop computer, a handheld computer, a vehicle-mounted electronic device, a Mobile Internet Device (MID), an augmented reality (AR) / virtual reality (VR) device, a robot, a wearable device, an ultra-mobile personal computer (UMPC), a netbook, or a personal digital assistant (PDA), etc. It can also be a server, a Network Attached Storage (NAS), a personal computer (PC), a television (TV), a teller machine, or a self-service machine, etc. The embodiments of the present application do not make specific limitations.

[0202] The memory recovery device in the embodiments of the present application can be a device with an operating system. The operating system can be an Android operating system, an iOS operating system, or other possible operating systems. The embodiments of the present application do not make specific limitations.

[0203] The memory recovery device provided in the embodiments of the present application can implement each process implemented by the above memory recovery method embodiments. To avoid repetition, it will not be elaborated here.

[0204] Optionally, as Figure 11 shown, the embodiments of the present application further provide an electronic device 1100, including a processor 1101 and a memory 1102. A program or instruction that can run on the processor 1101 is stored on the memory 1102. When the program or instruction is executed by the processor 1101, it implements each step of the above memory recovery method embodiments and can achieve the same technical effect. To avoid repetition, it will not be elaborated here.

[0205] It should be noted that the electronic devices in the embodiments of the present application include the above-mentioned mobile electronic devices and non-mobile electronic devices.

[0206] Figure 12 Schematic diagram of the hardware structure of an electronic device for implementing the embodiments of the present application.

[0207] The electronic device 100 includes, but is not limited to, components such as a radio frequency unit 101, a network module 102, an audio output unit 103, an input unit 104, a sensor 105, a display unit 106, a user input unit 107, an interface unit 108, a memory 109, and a processor 110.

[0208] Those skilled in the art can understand that the electronic device 100 may further include a power source (such as a battery) for supplying power to each component. The power source can be logically connected to the processor 110 through a power management system, so as to implement functions such as management of charging, discharging, and power consumption management through the power management system. Figure 12 The structure of the electronic device shown does not limit the electronic device. The electronic device may include more or fewer components than shown, or combine certain components, or have different component arrangements, which will not be elaborated here.

[0209] Among them, the above-mentioned processor 110 is used to obtain the free memory amount of the electronic device when a first process applies for memory; the first process is a process of a preset category; the processor 110 is further used to perform a traversal operation on the virtual address space of the process through a kernel thread when the free memory amount is less than or equal to a first threshold, to obtain at least two types of memory pages; the access heat of the at least two types of memory pages is different; the processor 110 is further used to perform a memory recycling operation according to the at least two types of memory pages through the first process.

[0210] In some embodiments of the present application, the processor 110 is specifically used to traverse the virtual address space linked list corresponding to the first process group through the kernel thread, and the virtual address space linked list is used to indicate the virtual address space corresponding to at least one process in the first process group; the processor 110 is specifically used to determine the page table entries corresponding to the at least one process according to the virtual address space; the processor 110 is specifically used to classify the memory pages of the at least one process according to the access flag bits of the page table entries corresponding to the at least one process, to obtain at least two types of memory pages.

[0211] In some embodiments of the present application, the processor 110 is further used to obtain a process group linked list before traversing the virtual address space linked list corresponding to the first process group through the kernel thread, and the process group linked list is used to indicate at least one process group; the processor 110 is further used to determine the first process group according to the process group linked list.

[0212] In some embodiments of the present application, the above-mentioned processor 110 is further configured to obtain context parameters before performing a traversal operation on the virtual address space of the process through the kernel thread to obtain at least two types of memory pages. The context parameters include parameters related to performing a traversal operation on the virtual address space of the process.

[0213] In some embodiments of the present application, the above-mentioned processor 110 is specifically configured to determine a linked list node object according to the above-mentioned context parameters; the above-mentioned processor 110 is specifically configured to add the above-mentioned linked list node object to the global linked list.

[0214] In some embodiments of the present application, the above-mentioned processor 110 is specifically configured to obtain the above-mentioned context parameters from the above-mentioned global linked list through the above-mentioned kernel thread; the above-mentioned processor 110 is specifically configured to perform a traversal operation on the virtual address space of the process according to the above-mentioned context parameters through the above-mentioned kernel thread to obtain at least two types of memory pages.

[0215] In some embodiments of the present application, the above-mentioned processor 110 is specifically configured to determine memory pages to be recycled from the above-mentioned at least two types of memory pages based on memory recycling parameters through the above-mentioned first process; the above-mentioned processor 110 is specifically configured to perform memory recycling on the above-mentioned memory pages to be recycled.

[0216] In some embodiments of the present application, the above-mentioned memory recycling parameters include at least one of the following: the type of memory pages to be recycled; the page level of the memory pages to be recycled.

[0217] The electronic device provided by the embodiments of the present application, when the first process applies for memory, obtains the free memory amount of the electronic device; the above-mentioned first process is a process of a preset category; when the above-mentioned free memory amount is less than or equal to a first threshold, a traversal operation is performed on the virtual address space of the process through the kernel thread to obtain at least two types of memory pages; the access hotness of the above-mentioned at least two types of memory pages is different; through the above-mentioned first process, a memory recycling operation is performed according to the above-mentioned at least two types of memory pages. Through this method, the electronic device can perform memory recycling tasks in parallel through the main process and the kernel thread, thereby improving the speed of memory recycling of the process of the preset category, and further improving the memory recycling efficiency of the electronic device.

[0218] It should be understood that in the embodiments of the present application, the input unit 104 may include a Graphics Processing Unit (GPU) 1041 and a microphone 1042. The GPU 1041 processes the image data of static pictures or videos obtained by an image capturing device (such as a camera) in a video capturing mode or an image capturing mode. The display unit 106 may include a display panel 1061, and the display panel 1061 may be configured in the form of a liquid crystal display, an organic light emitting diode, etc. The user input unit 107 includes at least one of a touch panel 1071 and other input devices 1072. The touch panel 1071 is also referred to as a touch screen. The touch panel 1071 may include two parts: a touch detection device and a touch controller. The other input devices 1072 may include, but are not limited to, a physical keyboard, function keys (such as volume control keys, switch keys, etc.), a trackball, a mouse, and a joystick, which will not be elaborated here.

[0219] The memory 109 can be used to store software programs and various data. The memory 109 mainly includes a first storage area for storing programs or instructions and a second storage area for storing data. Among them, the first storage area can store an operating system, applications or instructions required for at least one function (such as a sound playback function, an image playback function, etc.). In addition, the memory 109 may include a volatile memory or a non-volatile memory, or the memory 109 may include both a volatile memory and a non-volatile memory. Among them, the non-volatile memory may be a Read-Only Memory (ROM), a Programmable ROM (PROM), an Erasable PROM (EPROM), an Electrically EPROM (EEPROM), or a flash memory. The volatile memory may be a Random Access Memory (RAM), a Static RAM (SRAM), a Dynamic RAM (DRAM), a Synchronous DRAM (SDRAM), a Double Data Rate SDRAM (DDR SDRAM), an Enhanced SDRAM (ESDRAM), a Synch link DRAM (SLDRAM), and a Direct Rambus RAM (DRRAM). The memory 109 in the embodiments of the present application includes, but is not limited to, these and any other suitable types of memories.

[0220] The processor 110 may include one or more processing units; optionally, the processor 110 integrates an application processor and a modem processor. Among them, the application processor mainly processes operations related to the operating system, user interface, application programs, etc., and the modem processor mainly processes wireless communication signals, such as a baseband processor. It can be understood that the above-mentioned modem processor may not be integrated into the processor 110 either.

[0221] The embodiment of the present application also provides a readable storage medium, on which a program or instruction is stored. When the program or instruction is executed by a processor, it implements each process of the above-mentioned embodiment of the memory recycling method and can achieve the same technical effect. To avoid repetition, it will not be elaborated here.

[0222] Among them, the processor is the processor in the electronic device described in the above embodiment. The readable storage medium includes computer-readable storage media, such as computer read-only memory ROM, random access memory RAM, magnetic disks, or optical discs, etc.

[0223] The embodiment of the present application further provides a chip, which includes a processor and a communication interface. The communication interface is coupled to the processor. The processor is used to run a program or instruction to implement each process of the above-mentioned embodiment of the memory recycling method and can achieve the same technical effect. To avoid repetition, it will not be elaborated here.

[0224] It should be understood that the chip mentioned in the embodiment of the present application may also be referred to as a system-on-chip, system chip, chip system, or system-on-chip, etc.

[0225] The embodiment of the present application provides a computer program product, which is stored in a storage medium. The program product is executed by at least one processor to implement each process of the above-mentioned embodiment of the memory recycling method and can achieve the same technical effect. To avoid repetition, it will not be elaborated here.

[0226] It should be noted that, in this document, the term "including", "comprising" or any other variant thereof is intended to cover non-exclusive inclusion, such that a process, method, article or device comprising a series of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such process, method, article or device. Without further limitation, an element defined by the statement "comprising an..." does not exclude the presence of additional identical elements in the process, method, article or device comprising that element. In addition, it should be pointed out that the scope of the methods and devices in the embodiments of the present application is not limited to performing functions in the order shown or discussed, and may also include performing functions in a substantially simultaneous manner or in the reverse order according to the functions involved. For example, the described methods may be performed in a different order than described, and various steps may be added, omitted, or combined. Additionally, the features described with reference to certain examples may be combined in other examples.

[0227] From the description of the above embodiments, those skilled in the art can clearly understand that the methods of the above embodiments can be implemented by means of software plus a necessary general hardware platform. Of course, they can also be implemented by hardware, but in many cases the former is a better implementation. Based on this understanding, the technical solution of the present application, in essence, or the part that contributes to the prior art, can be embodied in the form of a computer software product. The computer software product is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disc) and includes several instructions to enable a terminal (which can be a mobile phone, computer, server, or network device, etc.) to execute the methods described in the various embodiments of the present application.

[0228] The embodiments of the present application have been described above in conjunction with the accompanying drawings. However, the present application is not limited to the above specific embodiments. The above specific embodiments are merely illustrative and not restrictive. Under the inspiration of the present application, those of ordinary skill in the art can also make many forms without departing from the purpose of the present application and the scope protected by the claims, and all of them fall within the protection scope of the present application.

Claims

1. A memory recovery method, characterized in that: The method comprises: When a first process applies for memory, obtaining the amount of free memory of the electronic device; the first process is a process of a preset category; When the amount of free memory is less than or equal to the first threshold, performing a traversal operation on the virtual address space of the process through a kernel thread to obtain at least two types of memory pages; the at least two types of memory pages have different access heats; Through the first process, a memory reclaiming operation is performed according to the at least two types of memory pages.

2. The method according to claim 1, characterized in that The traversal operation is performed on the virtual address space of the process through the kernel thread to obtain at least two types of memory pages, including: Traversing, by means of the kernel thread, a virtual address space linked list corresponding to the first process group, wherein the virtual address space linked list is used to indicate a virtual address space corresponding to at least one process in the first process group; Determining, according to the virtual address space, a page table entry corresponding to the at least one process; According to the access flag bit of the page table entry corresponding to the at least one process, the memory pages of the at least one process are classified to obtain at least two categories of memory pages.

3. The method according to claim 2, characterized in that Before traversing the virtual address space linked list corresponding to the first process group through the kernel thread, the method further includes: Obtaining a process group linked list, where the process group linked list is used to indicate at least one process group; According to the process group linked list, the first process group is determined.

4. The method according to claim 1, characterized in that: Before performing a traversal operation on the virtual address space of the process through the kernel thread to obtain at least two types of memory pages, the method further includes: Obtaining context parameters, wherein the context parameters include parameters related to performing a traversal operation on a virtual address space of a process; Add the context parameters to the global linked list.

5. The method according to claim 4, characterized in that The adding the context parameters to the global linked list comprises: Determine a linked list node object according to the context parameter; Add the linked list node object to the global linked list.

6. The method according to claim 4, characterized in that The traversal operation is performed on the virtual address space of the process through the kernel thread to obtain at least two types of memory pages, including: Acquiring the context parameter from the global linked list through the kernel thread; Through the kernel thread, a traversal operation is performed on the virtual address space of the process according to the context parameters to obtain at least two types of memory pages.

7. The method according to claim 1, characterized in that The performing of the memory reclaiming operation according to the at least two types of memory pages through the first process includes: Determining, by the first process, memory pages to be reclaimed from the at least two types of memory pages based on memory reclaim parameters; Memory reclamation is performed on the memory page to be reclaimed.

8. The method according to claim 7, characterized in that The memory recycling parameter includes at least one of the following: The type of memory page to be reclaimed; The page level of the memory page to be reclaimed.

9. A memory recovery device, characterized in that: The device comprises: an acquisition module, a traversal module and a memory recovery module; The acquisition module is used to acquire the amount of free memory of the electronic device when a first process applies for memory; the first process is a process of a preset category; The traversal module is used to perform a traversal operation on the virtual address space of the process through a kernel thread when the amount of free memory is less than or equal to a first threshold, to obtain at least two types of memory pages; the access heat of the at least two types of memory pages is different; The memory reclaiming module is used to perform a memory reclaiming operation according to the at least two types of memory pages through the first process.

10. The device according to claim 9, characterized in that The device also includes: a determination module and a processing module; The traversal module is specifically used to traverse the virtual address space linked list corresponding to the first process group through the kernel thread, and the virtual address space linked list is used to indicate the virtual address space corresponding to at least one process in the first process group; The determination module is used to determine the page table entry corresponding to the at least one process according to the virtual address space; The processing module is used to classify the memory pages of the at least one process according to the access flag bits of the page table entries corresponding to the at least one process to obtain at least two categories of memory pages.

11. The device according to claim 10, characterized in that The acquisition module is further configured to acquire a process group linked list before traversing the virtual address space linked list corresponding to the first process group through the kernel thread, wherein the process group linked list is used to indicate at least one process group; The determination module is further used to determine the first process group according to the process group linked list.

12. The device according to claim 9, characterized in that The acquisition module is further used to acquire context parameters before performing the traversal operation on the virtual address space of the process through the kernel thread to obtain at least two types of memory pages, wherein the context parameters include relevant parameters for performing the traversal operation on the virtual address space of the process; The processing module is further used to add the context parameters to a global linked list.

13. The device according to claim 12, characterized in that The determination module is specifically used to determine the linked list node object according to the context parameter; The processing module is specifically used to add the linked list node object to the global linked list.

14. An electronic device, characterized in that: It comprises a processor and a memory, wherein the memory stores programs or instructions that can be run on the processor, and when the programs or instructions are executed by the processor, the steps of the memory recycling method as described in any one of claims 1 to 8 are implemented.

15. A readable storage medium, characterized in that: The readable storage medium stores a program or instruction, and when the program or instruction is executed by the processor, the steps of the memory recovery method as described in any one of claims 1-8 are implemented.