Memory recovery method, device, equipment and storage medium
Patent Information
- Application Number
- CN202510193004.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-02-20
- Publication Date
- 2026-08-21
AI Technical Summary
相关技术中,在回收内存页时,在反向映射过程中申请信号量锁,若信号量锁被其他进程或线程占用,反向映射任务只能进入睡眠状态,这种卡锁现象会阻塞内存回收流程,导致内存页无法及时回收,影响内存回收效率
[0023]The memory page list is traversed. When a target memory page is encountered, an attempt is made to acquire a semaphore lock for it. If the semaphore lock acquisition is successful, the reverse mapping process is executed, and the target memory page is reclaimed. If the semaphore lock acquisition fails, it means that the semaphore lock is already held by another process or thread, and the target memory page is reclaimed by an asynchronous garbage collection thread. On the one hand, by attempting to acquire the semaphore lock in advance before executing the reverse mapping, blocking problems caused by lock contention during the reverse mapping process are avoided, ensuring that the memory reclamation process can run continuously. On the other hand, by having an asynchronous garbage collection thread reclaim the target memory page for which lock acquisition failed, idle system resources can be utilized for memory reclamation, avoiding untimely memory reclamation and improving memory reclamation efficiency.
Smart Images

Figure CN122614531A_ABST
Abstract
Description
Technical Field
[0001] This disclosure relates to the field of electronic equipment technology, and in particular to a memory recycling method, apparatus, device, and storage medium. Background Technology
[0002] In the field of electronic device technology, with the continuous iteration of applications and functions, users' demands for the performance of electronic devices are also constantly increasing. Therefore, the efficiency of memory reclamation has become particularly important. In related technologies, when reclaiming memory pages, a semaphore lock is requested during the reverse mapping process. If the semaphore lock is occupied by another process or thread, the reverse mapping task can only enter a sleep state. This locking phenomenon will block the memory reclamation process, causing memory pages to be unable to be reclaimed in a timely manner, thus affecting the efficiency of memory reclamation.
[0003] It should be noted that the information disclosed in the background section above is only used to enhance the understanding of the background of this disclosure, and therefore may include information that does not constitute prior art known to those skilled in the art. Summary of the Invention
[0004] The purpose of this disclosure is to provide a memory reclamation method, apparatus, device, and storage medium.
[0005] According to a first aspect of the present disclosure, a memory reclamation method is provided, the method comprising: traversing memory pages in a memory page linked list to obtain a target memory page currently being traversed, and requesting a semaphore lock for the target memory page; in response to a successful request for a semaphore lock for the target memory page, reclaiming the target memory page based on a reverse mapping mechanism; and in response to a failure to request a semaphore lock for the target memory page, reclaiming the target memory page through an asynchronous reclamation thread.
[0006] In some embodiments of this disclosure, the step of reclaiming the target memory page via an asynchronous reclamation thread in response to the failure to acquire a semaphore lock for the target memory page includes: setting a lock acquisition failure flag for the target memory page in response to the failure to acquire a semaphore lock for the target memory page; the lock acquisition failure flag is used to indicate that the acquisition of the semaphore lock failed; placing the target memory page into an asynchronous linked list; and reclaiming the memory pages in the asynchronous linked list via the asynchronous reclamation thread.
[0007] In some embodiments of this disclosure, the step of reclaiming memory pages in the asynchronous linked list through the asynchronous reclamation thread includes: setting a waiting queue and placing the asynchronous reclamation thread into the waiting queue; waking up the asynchronous reclamation thread in the waiting queue in response to the existence of memory pages in the asynchronous linked list with a lock acquisition failure flag set, and reclaiming the memory pages in the asynchronous linked list through the asynchronous reclamation thread; and setting the state of the asynchronous reclamation thread to a sleep state in response to the asynchronous linked list being empty.
[0008] In some embodiments of this disclosure, the method further includes: monitoring processor load values; in response to the processor load value being greater than or equal to a preset load threshold, binding the asynchronous recycling thread to a first processor core; the frequency of the first processor core is lower than the frequency of a second processor core, and the second processor core is the processor core to which the asynchronous recycling thread is bound by default.
[0009] In some embodiments of this disclosure, the step of reclaiming the target memory page based on a reverse mapping mechanism in response to a successful acquisition of a semaphore lock for the target memory page includes: performing a reverse mapping on the target memory page in response to a successful acquisition of a semaphore lock for the target memory page to obtain a reverse mapping result of the target memory page; determining the active state of the target memory page based on the reverse mapping result of the target memory page; and reclaiming the target memory page based on the active state of the target memory page.
[0010] In some embodiments of this disclosure, the memory page list includes an active page list and an inactive page list; wherein, the step of reclaiming the target memory page based on its active state includes: if the target memory page is in the active page list and is inactive, then moving the target memory page from the active page list to the inactive page list; if the target memory page is in the inactive page list and is active, then moving the target memory page from the inactive page list to the active page list; if the target memory page is in the inactive page list and is inactive, then reclaiming the target memory page.
[0011] In some embodiments of this disclosure, before traversing the memory pages in the memory page linked list, the method further includes: monitoring the remaining memory value; in response to the remaining memory value being greater than or equal to a first memory threshold and less than a second memory threshold, determining to periodically reclaim memory from the memory pages in the memory page linked list; the first memory threshold being less than the second memory threshold; and in response to the remaining memory value being less than the first memory threshold, determining to directly reclaim memory from the memory pages in the memory page linked list.
[0012] According to a second aspect of the present disclosure, a memory reclamation apparatus is provided, the apparatus comprising: a lock request module configured to traverse memory pages in a memory page linked list, obtain a target memory page currently being traversed, and request a semaphore lock for the target memory page; a first reclamation module configured to reclaim the target memory page based on a reverse mapping mechanism in response to a successful semaphore lock request for the target memory page; and a second reclamation module configured to reclaim the target memory page through an asynchronous reclamation thread in response to a failure to request a semaphore lock for the target memory page.
[0013] In some embodiments of this disclosure, the second recycling module is further configured to: in response to failure to request a semaphore lock for the target memory page, set a lock request failure flag for the target memory page; the lock request failure flag is used to indicate that the semaphore lock request failed; put the target memory page into an asynchronous linked list; and recycle the memory pages in the asynchronous linked list through the asynchronous recycling thread.
[0014] In some embodiments of this disclosure, the second recycling module is further configured to: set a waiting queue and place the asynchronous recycling thread into the waiting queue; in response to the existence of a memory page in the asynchronous linked list with a lock acquisition failure flag set, wake up the asynchronous recycling thread in the waiting queue and recycle the memory page in the asynchronous linked list through the asynchronous recycling thread; in response to the asynchronous linked list being empty, set the state of the asynchronous recycling thread to a sleep state.
[0015] In some embodiments of this disclosure, the device further includes a monitoring module configured to: monitor a processor load value; and, in response to the processor load value being greater than or equal to a preset load threshold, bind the asynchronous recycling thread to a first processor core; wherein the frequency of the first processor core is lower than the frequency of a second processor core, and the second processor core is the processor core to which the asynchronous recycling thread is bound by default.
[0016] In some embodiments of this disclosure, the first recycling module is further configured to: in response to a successful acquisition of a semaphore lock for the target memory page, perform a reverse mapping on the target memory page to obtain a reverse mapping result of the target memory page; determine the active state of the target memory page based on the reverse mapping result of the target memory page; and reclaim the target memory page based on the active state of the target memory page.
[0017] In some embodiments of this disclosure, the memory page list includes an active page list and an inactive page list; wherein, the first reclamation module is further configured to: if the target memory page is located in the active page list and the target memory page is inactive, then move the target memory page from the active page list to the inactive page list; if the target memory page is located in the inactive page list and the target memory page is active, then move the target memory page from the inactive page list to the active page list; if the target memory page is located in the inactive page list and the target memory page is inactive, then reclaim the target memory page.
[0018] In some embodiments of this disclosure, the monitoring module is further configured to: monitor the remaining memory value; in response to the remaining memory value being greater than or equal to a first memory threshold and less than a second memory threshold, determine to periodically reclaim memory pages in the memory page linked list; the first memory threshold being less than the second memory threshold; and in response to the remaining memory value being less than the first memory threshold, determine to directly reclaim memory pages in the memory page linked list.
[0019] According to a third aspect of the present disclosure, an electronic device is provided, characterized in that it includes: a processor; a memory for storing processor-executable instructions; wherein the processor is configured to implement the memory reclamation method described above.
[0020] According to a fourth aspect of the present disclosure, a non-transitory computer-readable storage medium is provided. When instructions in the storage medium are executed by a processor of a mobile terminal, the mobile terminal is enabled to execute a memory reclamation method. The method includes: traversing memory pages in a memory page linked list to obtain a target memory page currently being traversed, and requesting a semaphore lock for the target memory page; in response to a successful request for a semaphore lock for the target memory page, reclaiming the target memory page based on a reverse mapping mechanism; and in response to a failure to request a semaphore lock for the target memory page, reclaiming the target memory page through an asynchronous reclamation thread.
[0021] According to a fifth aspect of the present disclosure, a computer program product is provided, including a computer program that, when executed by a processor, implements the memory reclamation method described above.
[0022] The technical solutions provided by the embodiments of this disclosure may include the following beneficial effects:
[0023] The memory page list is traversed. When a target memory page is encountered, an attempt is made to acquire a semaphore lock for it. If the semaphore lock acquisition is successful, the reverse mapping process is executed, and the target memory page is reclaimed. If the semaphore lock acquisition fails, it means that the semaphore lock is already held by another process or thread, and the target memory page is reclaimed by an asynchronous garbage collection thread. On the one hand, by attempting to acquire the semaphore lock in advance before executing the reverse mapping, blocking problems caused by lock contention during the reverse mapping process are avoided, ensuring that the memory reclamation process can run continuously. On the other hand, by having an asynchronous garbage collection thread reclaim the target memory page for which lock acquisition failed, idle system resources can be utilized for memory reclamation, avoiding untimely memory reclamation and improving memory reclamation efficiency.
[0024] It should be understood that the above general description and the following detailed description are exemplary and explanatory only, and are not intended to limit this disclosure. Attached Figure Description
[0025] The accompanying drawings, which are incorporated in and form a part of this specification, illustrate embodiments consistent with this disclosure and, together with the description, serve to explain the principles of this disclosure.
[0026] Figure 1 This is a flowchart illustrating a memory reclamation method according to some embodiments of the present disclosure.
[0027] Figure 2 This is a flowchart illustrating another memory reclamation method according to some embodiments of the present disclosure.
[0028] Figure 3 This is a flowchart illustrating a memory reclamation method according to some embodiments of the present disclosure, in which a target memory page is reclaimed based on a reverse mapping mechanism when a semaphore lock is successfully acquired for the target memory page.
[0029] Figure 4 This is a flowchart illustrating a memory reclamation method according to some embodiments of the present disclosure, in which an asynchronous reclamation thread reclaims a target memory page when the application for a semaphore lock on the target memory page fails.
[0030] Figure 5 This is a flowchart illustrating a memory reclamation method according to some embodiments of the present disclosure, in which memory pages in an asynchronous linked list are reclaimed by an asynchronous reclamation thread.
[0031] Figure 6 This is a block diagram illustrating a memory reclamation apparatus according to some embodiments of the present disclosure.
[0032] Figure 7 This is a block diagram illustrating an electronic device according to some embodiments of the present disclosure. Detailed Implementation
[0033] Exemplary embodiments of this disclosure will be described in detail herein, examples of which are illustrated in the accompanying drawings. When the following description refers to the drawings, the same numbers in different drawings denote the same or similar elements unless otherwise indicated. Various changes, modifications, and equivalents of the methods, apparatus, and / or systems described herein will become apparent upon understanding this disclosure. For example, the order of operations described herein is merely illustrative and is not limited to those orders set forth herein, but can be changed as will become apparent upon understanding this disclosure, except for operations that must be performed in a particular order. Furthermore, for clarity and brevity, descriptions of features known in the art may be omitted.
[0034] The embodiments described below, which are examples of some of the embodiments of this disclosure, do not represent all embodiments consistent with this disclosure. Rather, they are merely examples of apparatuses and methods consistent with some aspects of this disclosure as detailed in the appended claims.
[0035] It should be noted that the acquisition, storage, use, and processing of data in this disclosed technical solution comply with the relevant provisions of national laws and regulations. The various types of data, such as personal identity data, operational data, and behavioral data related to individuals, customers, and groups, obtained in the embodiments of this disclosure have all been authorized.
[0036] Figure 1 This is a flowchart illustrating a memory reclamation method according to some embodiments of this disclosure. (Refer to...) Figure 1 This memory reclamation method can be applied to various types of electronic devices and includes the following steps.
[0037] In step S110, the memory pages in the memory page linked list are traversed to obtain the target memory page being traversed, and a semaphore lock is requested for the target memory page.
[0038] The memory page linked list is a data structure that stores information about memory pages. A semaphore lock is a mechanism used in multithreaded or multiprocess programming to implement synchronization and mutual exclusion. Semaphores control access permissions to shared resources; once a process or thread successfully acquires a semaphore lock, other processes or threads cannot access the shared resource simultaneously until the lock is released.
[0039] In this embodiment of the disclosure, during memory reclamation, each memory page is traversed starting from the head of the memory page linked list to find the memory page that needs to be reclaimed. The currently traversed memory page is defined as the target memory page. When the target memory page is reached, an attempt is made to acquire a semaphore lock for the target memory page. In this way, the semaphore lock can be acquired for the target memory page before the reverse mapping is performed.
[0040] In some embodiments of this disclosure, the memory page list includes an active page list and an inactive page list. The active page list records memory pages that are frequently accessed and used, such as those occupied by running program code or data being processed. The inactive page list stores memory pages that are not frequently accessed at the moment; these pages may be data that the program does not currently need or memory space occupied by code that has completed a phase of its task.
[0041] For example, when determining to perform memory reclamation, the memory pages in the active page list can be traversed first, and then the memory pages in the inactive page list can be traversed.
[0042] In step S120, in response to the successful acquisition of a semaphore lock for the target memory page, the target memory page is reclaimed based on the reverse mapping mechanism.
[0043] The purpose of reverse mapping is to find all virtual memory areas (VMAs) and page tables mapped to a memory page by using the physical address of the memory page.
[0044] A Virtualized Address Space (VMA) can be understood as an abstraction of a contiguous linear address space. When an application installed on an electronic device runs, the operating system deployed within the device creates a process for the application and allocates a VMA to the process in response to its memory request. A mapping exists between the VMA and physical memory pages, and this mapping is stored in the page table corresponding to the process.
[0045] In this embodiment of the disclosure, if a semaphore lock is successfully acquired for the target memory page, all VMAs and page tables mapped to the target memory page can be determined based on the reverse mapping mechanism, and then it can be determined whether to perform a reclamation operation on the target memory page.
[0046] For example, if it is determined that a target memory page needs to be reclaimed, the target memory page can be removed from the memory page list, releasing the physical memory space it occupies so that this memory space can be reallocated to other processes or tasks that need it. The VMA and page table mapped to the target memory page can also be updated to ensure the consistency and accuracy of memory management.
[0047] In step S130, in response to the failure to acquire a semaphore lock for the target memory page, the target memory page is reclaimed by an asynchronous reclamation thread.
[0048] In this embodiment, the asynchronous garbage collection thread refers to a background thread that is responsible for reclaiming memory pages. The asynchronous garbage collection thread can run independently of the thread currently requesting the lock and will not be blocked while waiting for the lock. If acquiring a semaphore lock for the target memory page fails, it means that the semaphore lock has already been occupied by another process or thread. In this case, the target memory page can be reclaimed using the asynchronous garbage collection thread.
[0049] As can be seen from the above steps, the memory reclamation method provided in this embodiment traverses the memory pages in the memory page linked list. When the target memory page is encountered, an attempt is made to acquire a semaphore lock for it. If the semaphore lock acquisition is successful, a reverse mapping process is executed to reclaim the target memory page. If the semaphore lock acquisition fails, it indicates that the semaphore lock has been occupied by another process or thread, and in this case, an asynchronous reclamation thread reclaims the target memory page. On the one hand, by attempting to acquire the semaphore lock in advance before executing the reverse mapping, the blocking problem caused by lock contention during the reverse mapping process is avoided, ensuring that the memory reclamation process can continue to run. On the other hand, by using an asynchronous reclamation thread to reclaim the target memory page where the lock acquisition failed, idle system resources can be utilized for memory reclamation, avoiding untimely memory reclamation and improving memory reclamation efficiency.
[0050] Figure 2 This is a flowchart illustrating another memory reclamation method according to some embodiments of the present disclosure. In the embodiments of the present disclosure, Figure 2 In the memory reclamation method shown, steps S240, S250, and S260 are respectively related to... Figure 1 Steps S110, S120, and S130 in the memory reclamation method shown correspond to each other and will not be repeated here.
[0051] In this embodiment of the disclosure, Figure 1 Based on the memory reclamation method shown, Figure 2 The memory reclamation method shown may also include the following steps.
[0052] In step S210, the remaining memory value is monitored.
[0053] The remaining memory value refers to the amount of unused memory in the current system of an electronic device. By monitoring the remaining memory value in real time, the system's memory usage can be assessed, thereby determining whether memory reclamation should be triggered, and if so, which memory reclamation mechanism should be used.
[0054] For example, the remaining memory value can be checked periodically through a memory management module in the system kernel, such as kswapd in the Linux operating system.
[0055] In step S220, in response to the remaining memory value being greater than or equal to a first memory threshold and less than a second memory threshold, it is determined that periodic memory reclamation will be performed on the memory pages in the memory page linked list; the first memory threshold is less than the second memory threshold.
[0056] In step S230, in response to the remaining memory value being less than a first memory threshold, it is determined to perform direct memory reclamation on the memory pages in the memory page linked list.
[0057] The first memory threshold and the second memory threshold refer to the memory water level set in the operating system. In addition to the first memory threshold and the second memory threshold, a third memory threshold can also be set.
[0058] The first memory threshold corresponds to the minimum memory watermark, which is the lowest warning level for system memory. When the remaining memory value is below the minimum memory watermark, it indicates that the system memory may not be able to meet the memory requirements of new processes or tasks. The second memory threshold corresponds to the low memory watermark, which is the warning level for system memory. When the remaining memory value is below the low memory watermark but above or equal to the minimum memory watermark, it indicates that system memory is starting to become strained, but has not yet reached an emergency state. The third memory threshold corresponds to the high memory watermark. When the remaining memory value is above or equal to the high memory watermark, it indicates that the system memory is sufficient.
[0059] The first memory threshold is less than the second memory threshold, and the second memory threshold is less than the third memory threshold. Taking a 12G electronic device as an example, the first memory threshold can be set to 70.34M, the second memory threshold to 101.21M, and the third memory threshold to 132.17M. Of course, the values of the first, second, and third memory thresholds can be dynamically changed according to needs, and this embodiment does not limit them.
[0060] In this embodiment, periodic memory reclamation and direct memory reclamation are different memory reclamation mechanisms. If the remaining memory value is determined to be greater than or equal to a first memory threshold and less than a second memory threshold, it indicates that system memory is starting to become strained, triggering periodic memory reclamation. If the remaining memory value is determined to be less than the first memory threshold, it indicates that system memory is very strained, triggering direct memory reclamation. If the remaining memory value is determined to be greater than or equal to a third memory threshold, it indicates that system memory is sufficient, and memory reclamation does not need to be triggered.
[0061] For example, a `kswapd` thread can be used to handle periodic memory reclamation. When the remaining memory value is detected to be greater than or equal to a first memory threshold and less than a second memory threshold, the `kswapd` thread is woken up to begin asynchronous memory reclamation. The `kswapd` thread runs in the background and will not block currently executing user processes. It traverses the memory page linked list to find memory pages that can be reclaimed.
[0062] For example, direct memory reclamation is performed synchronously and will block currently running user processes. In other words, direct memory reclamation will forcibly reclaim some memory pages that are in use, even if these memory pages may be reused in the future.
[0063] As can be seen from the above steps, the memory reclamation method provided in this embodiment can dynamically adjust the memory reclamation strategy according to the current memory usage by monitoring the remaining memory value and triggering different memory reclamation mechanisms (periodic memory reclamation and direct memory reclamation) based on preset memory thresholds (first memory threshold and second memory threshold), thereby avoiding memory waste or insufficient memory. It can also reclaim memory in a timely manner to prevent the system from crashing or experiencing performance degradation due to insufficient memory.
[0064] Figure 3 This is a flowchart illustrating a memory reclamation method according to some embodiments of the present disclosure, whereby the target memory page is reclaimed based on a reverse mapping mechanism after a successful semaphore lock acquisition. (Refer to...) Figure 3 This may include the following steps.
[0065] In step S310, in response to the successful acquisition of a semaphore lock for the target memory page, a reverse mapping is performed on the target memory page to obtain the reverse mapping result of the target memory page.
[0066] In this embodiment of the disclosure, the reverse mapping result of the target memory page includes all VMAs mapped to the target memory page and page tables.
[0067] If the semaphore lock acquisition for the target memory page is successful, it means that semaphore locks can be used to protect critical section resources on the reverse mapping path, namely all VMAs and page tables mapped to the target memory page, preventing access by other processes or threads. In other words, after successfully acquiring the semaphore lock for the target memory page, the reverse mapping operation can continue.
[0068] For example, a reverse mapping of a target memory page can be performed using the `page_referenced()` function. The target memory page can be a file page or an anonymous page. A file page is a memory page associated with a file in the file system, while an anonymous page is a memory page unrelated to a file.
[0069] Whether it's a file page or an anonymous page, the reverse mapping reuses the `mapping` field of the `page` structure and organizes all VMAs and page tables mapped to that page using a red-black tree. If the target memory page is a file page, `page_referenced()` will traverse the red-black tree in the `address_space` object to find all VMAs and page tables mapped to that page. If the target memory page is an anonymous page, `page_referenced()` will traverse the red-black tree in the `anon_vma` object to find all VMAs and page tables mapped to that page.
[0070] In step S320, the active state of the target memory page is determined based on the reverse mapping result of the target memory page.
[0071] The reverse mapping result of the target memory page includes all Virtual Address Maps (VMAs) mapped to the target memory page, as well as the page table. A VMA is a data structure in the operating system kernel used to describe the virtual address space of a process. If the reverse mapping result contains all VMAs mapped to the target memory page, it indicates which processes or threads are using the target memory page. The page table is a data structure used by the operating system to manage the mapping relationship between virtual and physical addresses. If the reverse mapping result contains an entry (PTE) for the target memory page in the page table, it indicates the physical address and access permissions of the target memory page. Therefore, the page table can be used to determine whether the target memory page is frequently accessed.
[0072] In this embodiment of the disclosure, if the reverse mapping result of the target memory page indicates that the target memory page is mapped by one or more VMAs, and the access bit in the page table is set, it indicates that the target memory page has been recently accessed, and the target memory page is marked as active. If the reverse mapping result of the target memory page indicates that the target memory page is not mapped by any VMA, or the access bit in the page table is not set, it indicates that the target memory page has not been recently accessed, and the target memory page is marked as inactive.
[0073] In step S330, the target memory page is reclaimed based on its active state.
[0074] In some embodiments of this disclosure, the target memory page is reclaimed based on its active state, including: if the target memory page is in the active page list and is inactive, then the target memory page is moved from the active page list to the inactive page list; if the target memory page is in the inactive page list and is active, then the target memory page is moved from the inactive page list to the active page list; if the target memory page is in the inactive page list and is inactive, then the target memory page is reclaimed.
[0075] In this embodiment of the disclosure, the memory page list includes an active page list and an inactive page list. After triggering periodic memory reclamation or direct memory reclamation, the memory pages in the active page list can be traversed first, and then the memory pages in the inactive page list can be traversed. When the target memory page is reached, it is reclaimed according to the page list in which the target memory page is located and its active status.
[0076] For example, the `shrink_active_list` function is used to traverse the memory pages in the active page list. For each target memory page encountered, the `page_referenced()` function is called to perform a reverse mapping on the target memory page, obtaining the VMAs and page tables mapped to the target memory page, and then determining whether the target memory page has been recently referenced. If the target memory page has been recently referenced, it means that the target memory page is in an active state, and it is kept in the active page list; if the target memory page has not been recently referenced, it means that the target memory page is in an inactive state, and it is moved to the inactive page list.
[0077] After traversing the memory pages in the active page list, the `shrink_inactive_list` function is used to traverse the memory pages in the inactive page list. For each target memory page encountered, the `page_referenced()` function is called to perform a reverse mapping on the target memory page, obtaining the VMA and page table mapped to the target memory page, and then determining whether the target memory page has been recently referenced. If the target memory page has been recently referenced, it means that the target memory page is in an active state, and it is moved to the active page list; if the target has not been recently referenced, it means that the target memory page is in an inactive state, and it is determined that the target memory page is a reclaimable page, removed from the inactive page list, releasing the physical memory space it occupies, and updating the relevant page table and virtual memory region information.
[0078] As can be seen from the above steps, the memory reclamation method provided in this embodiment retains memory pages in the active page list if they are active, otherwise they are moved to the inactive page list; conversely, for memory pages in the inactive page list, if they are active, they are moved to the active page list, otherwise they are reclaimed. This ensures that only truly unused memory pages are reclaimed, avoiding the accidental reclamation of still-active memory pages and improving memory management efficiency and system stability.
[0079] Figure 4 This is a flowchart illustrating a memory reclamation method according to some embodiments of the present disclosure, in which an asynchronous reclamation thread reclaims a target memory page when the acquisition of a semaphore lock for the target memory page fails. (Refer to...) Figure 4 This may include the following steps.
[0080] In step S410, in response to the failure to acquire a semaphore lock for the target memory page, a lock acquisition failure flag is set for the target memory page; the lock acquisition failure flag is used to indicate that the acquisition of the semaphore lock failed.
[0081] In this embodiment of the disclosure, if the acquisition of a semaphore lock for the target memory page fails, the reclamation of the target memory page based on the reflection mechanism is terminated, and a lock acquisition failure flag, such as the trylock_failed flag, is set for the target memory page to indicate that the acquisition of a semaphore lock for the target memory page has failed.
[0082] In step S420, the target memory page is placed into the asynchronous linked list.
[0083] In step S430, the memory pages in the asynchronous linked list are reclaimed by an asynchronous reclamation thread.
[0084] In this embodiment, the asynchronous linked list is used to store memory pages for which lock acquisitions have failed. After determining that a semaphore lock acquisition for a target memory page has failed, the target memory page is placed in the asynchronous linked list, and the memory pages in the asynchronous linked list are reclaimed by an asynchronous garbage collection thread.
[0085] In some embodiments of this disclosure, the memory reclamation method further includes: monitoring the processor load value; in response to the processor load value being greater than or equal to a preset load threshold, binding the asynchronous reclamation thread to a first processor core; the frequency of the first processor core is lower than the frequency of the second processor core, and the second processor core is the processor core to which the asynchronous reclamation thread is bound by default.
[0086] In this context, the asynchronous garbage collection thread refers to a background thread that is responsible for reclaiming memory pages. The asynchronous garbage collection thread can run independently of the currently requesting lock in the background and will not be blocked while waiting for the lock. After creating the asynchronous garbage collection thread, it can be bound to the second processor core by default.
[0087] In this embodiment of the disclosure, the processor load value can be monitored in real time. For example, the processor load value can be monitored through performance analysis tools such as top and sar to assess the current computing resource usage of the system.
[0088] If the processor load is less than a preset load threshold, the processor affinity of the asynchronous garbage collection thread remains unchanged; that is, the asynchronous garbage collection thread remains bound to the second processor core. If the processor load is greater than or equal to the preset load threshold, the asynchronous garbage collection thread is bound from the default second processor core to the first processor core, where the frequency of the first processor core is lower than that of the second processor core. This changes the core affinity of the asynchronous garbage collection thread, making it tend to run on the lower-frequency processor core, thus freeing up the higher-frequency processor core for system-critical threads and reducing the additional power consumption of the asynchronous garbage collection thread under high-pressure scenarios.
[0089] As can be seen from the above steps, the memory reclamation method provided in this embodiment binds the asynchronous reclamation thread to the second processor core by default. When the processor load value is detected to be greater than or equal to the preset load threshold, the asynchronous reclamation thread is rebound to the low-frequency first processor core, which can reduce the additional power consumption of the asynchronous reclamation thread to the system.
[0090] Figure 5 This is a flowchart illustrating a memory reclamation method according to some embodiments of the present disclosure, in which memory pages in an asynchronous linked list are reclaimed via an asynchronous reclamation thread. (Refer to...) Figure 5 This may include the following steps.
[0091] In step S510, a waiting queue is set up, and asynchronous recycling threads are placed into the waiting queue.
[0092] In step S520, in response to the existence of a memory page in the asynchronous linked list with a lock acquisition failure flag set, the asynchronous recycling thread in the waiting queue is woken up, and the asynchronous recycling thread reclaims the memory page in the asynchronous linked list.
[0093] In step S530, in response to the asynchronous linked list being empty, the state of the asynchronous recycling thread is set to sleep state.
[0094] In this embodiment of the disclosure, a waiting queue can be set up to place asynchronous recycling threads in the waiting queue, and a trigger condition for waking up the waiting queue can be set. The trigger condition is whether there are still memory pages in the asynchronous linked list that have a lock acquisition failure flag set.
[0095] If a memory page with a lock acquisition failure flag is set in the asynchronous linked list, the asynchronous garbage collection thread in the waiting queue is woken up. This thread then reclaims the memory page in the linked list. Specifically, for the memory page in the asynchronous linked list, the asynchronous garbage collection thread can attempt to re-acquire the semaphore lock and then reclaim it. If the asynchronous linked list is empty, the asynchronous garbage collection thread in the waiting queue is put into a sleep state, waiting to be woken up again when memory pages need to be reclaimed, thus avoiding additional power consumption.
[0096] The memory reclamation method provided in this disclosure compares the remaining memory value with various set memory thresholds. If the remaining memory value is greater than or equal to a third memory threshold, no memory reclamation needs to be triggered; if the remaining memory value is less than a second memory threshold but greater than or equal to a first memory threshold, periodic memory reclamation is triggered; if the remaining memory value is less than the first memory threshold, direct memory reclamation is triggered. After triggering periodic or direct memory reclamation, the `shrink_node()` method can be called to traverse the memory pages in the memory page linked list, first traversing the memory pages in the active page linked list, and then traversing the memory pages in the inactive page linked list.
[0097] The currently traversed memory page is identified as the target memory page, and an attempt is made to acquire a semaphore lock for it. If the acquisition of the semaphore lock is successful, the target memory page is reclaimed based on the reverse mapping mechanism. If the acquisition of the semaphore lock fails, a lock acquisition failure flag is added to the target memory page, and the target memory page is added to the asynchronous linked list. In response to the existence of a memory page with the lock acquisition failure flag set in the asynchronous linked list, an asynchronous thread is triggered to reclaim the memory pages in the asynchronous linked list.
[0098] As can be seen, the memory reclamation method provided in this embodiment avoids the blocking problem caused by lock contention during the reverse mapping process by attempting to request a semaphore lock in advance before performing reverse mapping, ensuring that the memory reclamation process can run continuously; and by using an asynchronous reclamation thread to reclaim the target memory pages for which lock requests failed, memory reclamation can be carried out using idle system resources, avoiding untimely memory reclamation and improving memory reclamation efficiency.
[0099] It should be noted that the above figures are merely illustrative representations of the processes included in methods according to some embodiments of this disclosure, and are not intended to be limiting. It is readily understood that the processes shown in the above figures do not indicate or limit the temporal order of these processes. Furthermore, it is readily understood that these processes may be executed synchronously or asynchronously, for example, in multiple modules.
[0100] The following are embodiments of the apparatus disclosed herein, which can be used to execute embodiments of the method disclosed herein. For details not disclosed in the apparatus embodiments of this disclosure, please refer to the embodiments of the method disclosed herein.
[0101] Figure 6 This is a block diagram illustrating a memory reclamation apparatus according to some embodiments of the present disclosure. (Refer to...) Figure 6 The memory recycling device 600 includes: a lock request module 610, a first recycling module 620, and a second recycling module 630.
[0102] Specifically, the lock acquisition module 610 is configured to traverse the memory pages in the memory page linked list, obtain the target memory page being traversed, and acquire a semaphore lock for the target memory page. The first reclamation module 620 is configured to reclaim the target memory page based on a reverse mapping mechanism in response to a successful acquisition of a semaphore lock for the target memory page. The second reclamation module 630 is configured to reclaim the target memory page through an asynchronous reclamation thread in response to a failure to acquire a semaphore lock for the target memory page.
[0103] In some embodiments of this disclosure, the second reclamation module 630 is further configured to: in response to failure to acquire a semaphore lock for the target memory page, set a lock acquisition failure flag for the target memory page; the lock acquisition failure flag is used to indicate that the acquisition of the semaphore lock failed; put the target memory page into an asynchronous linked list; and reclaim the memory pages in the asynchronous linked list through an asynchronous reclamation thread.
[0104] In some embodiments of this disclosure, the second recycling module 630 is further configured to: set a waiting queue and put the asynchronous recycling thread into the waiting queue; in response to the existence of a memory page in the asynchronous linked list with a lock acquisition failure flag set, wake up the asynchronous recycling thread in the waiting queue and reclaim the memory page in the asynchronous linked list through the asynchronous recycling thread; in response to the asynchronous linked list being empty, set the state of the asynchronous recycling thread to a sleep state.
[0105] In some embodiments of this disclosure, such as Figure 6 As shown, the device 600 also includes a monitoring module 640, configured to: monitor the processor load value; in response to the processor load value being greater than or equal to a preset load threshold, bind the asynchronous recycling thread to a first processor core; the frequency of the first processor core is lower than the frequency of the second processor core, and the second processor core is the processor core to which the asynchronous recycling thread is bound by default.
[0106] In some embodiments of this disclosure, the first reclamation module 620 is further configured to: in response to a successful acquisition of a semaphore lock for a target memory page, perform a reverse mapping on the target memory page to obtain a reverse mapping result of the target memory page; determine the active state of the target memory page based on the reverse mapping result of the target memory page; and reclaim the target memory page based on the active state of the target memory page.
[0107] In some embodiments of this disclosure, the memory page list includes an active page list and an inactive page list. The first reclamation module 620 is further configured to: if a target memory page is in the active page list and is inactive, move the target memory page from the active page list to the inactive page list; if a target memory page is in the inactive page list and is active, move the target memory page from the inactive page list to the active page list; and if a target memory page is in the inactive page list and is inactive, reclaim the target memory page.
[0108] In some embodiments of this disclosure, the monitoring module 640 is further configured to: monitor the remaining memory value; determine to periodically reclaim memory pages in the memory page linked list in response to the remaining memory value being greater than or equal to a first memory threshold and less than a second memory threshold; the first memory threshold being less than the second memory threshold; and determine to directly reclaim memory pages in the memory page linked list in response to the remaining memory value being less than the first memory threshold.
[0109] Regarding the apparatus in the above embodiments, the specific manner in which each module performs its operation has been described in detail in the embodiments related to the method, and will not be elaborated upon here.
[0110] Figure 7 This is a block diagram illustrating an electronic device according to some embodiments of the present disclosure. The electronic device 700 can be various types of electronic devices, such as mobile phones, computers, digital broadcasting terminals, messaging devices, game consoles, tablet devices, medical devices, fitness equipment, personal digital assistants, etc.
[0111] The memory reclamation method provided in this disclosure can be applied to various fields, such as smartphones, cloud technology, artificial intelligence, vehicle systems, and aerospace. Taking smartphones as an example, when a user simultaneously opens multiple applications, such as social media apps, video players, and games, system memory becomes strained. The memory reclamation method of this disclosure can reclaim memory pages occupied by applications that have not been used for a long time or are running in the background. In the field of vehicle systems, smart cars are equipped with various intelligent functions, such as navigation, multimedia entertainment, and autonomous driving assistance. When a user reaches their destination using the navigation system, the memory reclamation method of this disclosure can reclaim the memory pages of the navigation application, ensuring the stable operation and real-time response of the vehicle system.
[0112] Reference Figure 7The device 700 may include one or more of the following components: a processing component 702, a memory 704, a power supply component 706, a multimedia component 708, an audio component 710, an input / output (I / O) interface 712, a sensor component 714, and a communication component 716.
[0113] Processing component 702 typically controls the overall operation of device 700, such as operations associated with display, telephone calls, data communication, camera operation, and recording. Processing component 702 may include one or more processors 720 to execute instructions to complete all or part of the steps of the methods described above. Furthermore, processing component 702 may include one or more modules to facilitate interaction between processing component 702 and other components. For example, processing component 702 may include a multimedia module to facilitate interaction between multimedia component 708 and processing component 702.
[0114] Memory 704 is configured to store various types of data to support the operation of device 700. Examples of this data include instructions for any application or method operating on device 700, contact data, phonebook data, messages, pictures, videos, etc. Memory 704 can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic storage, flash memory, magnetic disk, or optical disk.
[0115] Power supply assembly 706 provides power to the various components of device 700. Power supply assembly 706 may include a power management system, one or more power supplies, and other components associated with generating, managing, and distributing power to device 700.
[0116] Multimedia component 708 includes a screen that provides an output interface between the device 700 and the user. In some embodiments, the screen may include a liquid crystal display (LCD) and a touch panel (TP). If the screen includes a touch panel, the screen may be implemented as a touchscreen to receive input signals from the user. The touch panel includes one or more touch sensors to sense touches, swipes, and gestures on the touch panel. The touch sensors may sense not only the boundaries of the touch or swipe action but also the duration and pressure associated with the touch or swipe operation. In some embodiments, multimedia component 708 includes a front-facing camera and / or a rear-facing camera. When the device 700 is in an operating mode, such as a shooting mode or a video mode, the front-facing camera and / or the rear-facing camera may receive external multimedia data. Each front-facing camera and rear-facing camera may be a fixed optical lens system or have focal length and optical zoom capabilities.
[0117] Audio component 710 is configured to output and / or input audio signals. For example, audio component 710 includes a microphone (MIC) configured to receive external audio signals when device 700 is in an operating mode, such as call mode, recording mode, and voice recognition mode. The received audio signals may be further stored in memory 704 or transmitted via communication component 716. In some embodiments, audio component 710 also includes a speaker for outputting audio signals.
[0118] I / O interface 712 provides an interface between processing component 702 and peripheral interface modules, such as keyboards, click wheels, buttons, etc. These buttons may include, but are not limited to, home buttons, volume buttons, power buttons, and lock buttons.
[0119] Sensor assembly 714 includes one or more sensors for providing status assessments of various aspects of device 700. For example, sensor assembly 714 may detect the on / off state of device 700, the relative positioning of components such as the display and keypad of device 700, changes in the position of device 700 or a component of device 700, the presence or absence of user contact with device 700, the orientation or acceleration / deceleration of device 700, and temperature changes of device 700. Sensor assembly 714 may include a proximity sensor configured to detect the presence of nearby objects without any physical contact. Sensor assembly 714 may also include a light sensor, such as a CMOS or CCD image sensor, for use in imaging applications. In some embodiments, sensor assembly 714 may also include a magnetic sensor, a pressure sensor, or a temperature sensor.
[0120] Communication component 716 is configured to facilitate wired or wireless communication between device 700 and other devices. Device 700 can access wireless networks based on communication standards, such as WiFi, 3G, 4G, 5G, other communication standards, or combinations thereof. In some embodiments of this disclosure, communication component 716 receives broadcast signals or broadcast-related information from an external broadcast management system via a broadcast channel. In some embodiments of this disclosure, communication component 716 further includes a near-field communication (NFC) module to facilitate short-range communication. For example, the NFC module may be implemented based on radio frequency identification (RFID) technology, Infrared Data Association (IrDA) technology, ultra-wideband (UWB) technology, Bluetooth (BT) technology, and other technologies.
[0121] In some embodiments of this disclosure, the apparatus 700 may be implemented by one or more application-specific integrated circuits (ASICs), digital signal processors (DSPs), digital signal processing devices (DSPDs), programmable logic devices (PLDs), field-programmable gate arrays (FPGAs), controllers, microcontrollers, microprocessors, or other electronic components to perform the methods described above.
[0122] In some embodiments of this disclosure, a non-transitory computer-readable storage medium including instructions is also provided, such as a memory 704 including instructions, which can be executed by a processor 720 of device 700 to perform the above-described method. For example, the non-transitory computer-readable storage medium may be a ROM, random access memory (RAM), CD-ROM, magnetic tape, floppy disk, and optical data storage device, etc.
[0123] This disclosure provides a non-transitory computer-readable storage medium. When the instructions in the storage medium are executed by the processor of a mobile terminal, the mobile terminal is able to execute a memory reclamation method. The method includes: traversing memory pages in a memory page linked list, obtaining the target memory page being traversed, and requesting a semaphore lock for the target memory page; in response to successfully requesting the semaphore lock for the target memory page, reclaiming the target memory page based on a reverse mapping mechanism; and in response to failure to request the semaphore lock for the target memory page, reclaiming the target memory page through an asynchronous reclamation thread.
[0124] Other embodiments of this disclosure will readily occur to those skilled in the art upon consideration of the specification and practice of the invention disclosed herein. This application is intended to cover any variations, uses, or adaptations of this disclosure that follow the general principles of this disclosure and include common knowledge or customary techniques in the art not disclosed herein. The specification and examples are to be considered exemplary only, and the true scope and spirit of this disclosure are indicated by the following claims.
[0125] It should be understood that this disclosure is not limited to the precise structures described above and shown in the accompanying drawings, and various modifications and changes can be made without departing from its scope. The scope of this disclosure is limited only by the appended claims.
Claims
1. A memory reclamation method, characterized in that, The method includes: Traverse the memory pages in the memory page linked list, obtain the target memory page to be traversed, and request a semaphore lock for the target memory page; In response to the successful acquisition of a semaphore lock for the target memory page, the target memory page is reclaimed based on the reverse mapping mechanism; In response to the failure to acquire a semaphore lock for the target memory page, the target memory page is reclaimed by an asynchronous reclamation thread.
2. The method according to claim 1, characterized in that, The step of reclaiming the target memory page via an asynchronous reclamation thread in response to the failure to acquire a semaphore lock for the target memory page includes: In response to the failure to acquire a semaphore lock for the target memory page, a lock acquisition failure flag is set for the target memory page; the lock acquisition failure flag is used to indicate that the acquisition of the semaphore lock failed. Place the target memory page into the asynchronous linked list; The asynchronous recycling thread reclaims memory pages in the asynchronous linked list.
3. The method according to claim 2, characterized in that, The step of reclaiming memory pages in the asynchronous linked list through the asynchronous reclamation thread includes: Set up a waiting queue and put the asynchronous recycling thread into the waiting queue; In response to the existence of a memory page in the asynchronous linked list that has a lock acquisition failure flag set, the asynchronous recycling thread in the waiting queue is woken up, and the asynchronous recycling thread reclaims the memory page in the asynchronous linked list; In response to the asynchronous linked list being empty, the state of the asynchronous recycling thread is set to sleep.
4. The method according to any one of claims 1 to 3, characterized in that, The method further includes: Monitor processor load values; In response to the processor load value being greater than or equal to a preset load threshold, the asynchronous recycling thread is bound to a first processor core; the frequency of the first processor core is lower than the frequency of the second processor core, and the second processor core is the processor core to which the asynchronous recycling thread is bound by default.
5. The method according to claim 1, characterized in that, The step of successfully acquiring a semaphore lock for the target memory page and reclaiming the target memory page based on a reverse mapping mechanism includes: In response to the successful acquisition of a semaphore lock for the target memory page, a reverse mapping is performed on the target memory page to obtain the reverse mapping result of the target memory page; The active state of the target memory page is determined based on the reverse mapping result of the target memory page; The target memory page is reclaimed based on its active state.
6. The method according to claim 5, characterized in that, The memory page linked list includes an active page linked list and an inactive page linked list; The step of reclaiming the target memory page based on its active state includes: If the target memory page is located in the active page list and the target memory page is inactive, then the target memory page is moved from the active page list to the inactive page list; If the target memory page is located in the inactive page list and the target memory page is in an active state, then the target memory page is moved from the inactive page list into the active page list; If the target memory page is located in the inactive page list and the target memory page is inactive, then the target memory page is reclaimed.
7. The method according to claim 1, characterized in that, Before traversing the memory pages in the memory page linked list, the method further includes: Monitor remaining memory values; In response to the remaining memory value being greater than or equal to a first memory threshold and less than a second memory threshold, it is determined that periodic memory reclamation will be performed on the memory pages in the memory page linked list; the first memory threshold is less than the second memory threshold. In response to the remaining memory value being less than the first memory threshold, it is determined that the memory pages in the memory page list will be directly reclaimed.
8. A memory reclamation device, characterized in that, The device includes: The lock request module is configured to traverse the memory pages in the memory page linked list, obtain the target memory page to be traversed, and request a semaphore lock for the target memory page. The first reclamation module is configured to reclaim the target memory page based on a reverse mapping mechanism in response to a successful acquisition of a semaphore lock for the target memory page. The second reclamation module is configured to reclaim the target memory page via an asynchronous reclamation thread in response to a failure to acquire a semaphore lock for the target memory page.
9. An electronic device, characterized in that, include: processor; Memory used to store processor-executable instructions; The processor is configured to implement the memory reclamation method according to any one of claims 1-7.
10. A non-transitory computer-readable storage medium, wherein when instructions in the storage medium are executed by a processor of a mobile terminal, the mobile terminal is enabled to perform a memory reclamation method, the method comprising: Traverse the memory pages in the memory page linked list, obtain the target memory page to be traversed, and request a semaphore lock for the target memory page; In response to the successful acquisition of a semaphore lock for the target memory page, the target memory page is reclaimed based on the reverse mapping mechanism; In response to the failure to acquire a semaphore lock for the target memory page, the target memory page is reclaimed by an asynchronous reclamation thread.