Memory management method and electronic device
Patent Information
- Application Number
- CN202210397263.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-04-15
- Publication Date
- 2026-10-27
- Estimated Expiration
- 2042-04-15
AI Technical Summary
[0004]但是,在系统内存高负载下进行kswapd内存回收时,按比例回收匿名页和文件页,极易导致出现回收页面类型不均衡的现象,使得内存回收次数增多,影响内存管理效率
[0040]第二方面以及第二方面中任意一种实现方式所对应的技术效果,可参见上述第一方面及第一方面中任意一种实现方式所对应的技术效果,此处不再赘述。
Smart Images

Figure CN116954876B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of terminal technology, and in particular to a memory management method and an electronic device. Background Technology
[0002] With the development of terminal technology, the number of applications installed on electronic devices (such as mobile phones and tablets) is gradually increasing, providing users with more and more complex functions. However, the running of more applications also puts pressure on the memory of electronic devices.
[0003] Electronic devices typically manage memory through the kernel swap daemon (kswapd). Specifically, during application runtime, different types of pages are requested from the system, such as anonymous pages and file pages. Each page allocation occupies memory. When memory is insufficient, kswapd switches from a sleep state to an active state and begins memory reclamation. During memory reclamation, kswapd reclaims anonymous and file pages proportionally, thereby reducing memory usage.
[0004] However, when performing kswapd memory reclamation under high system memory load, the proportional reclamation of anonymous pages and file pages can easily lead to an imbalance in the types of pages reclaimed, resulting in an increase in the number of memory reclamation operations and affecting memory management efficiency. Summary of the Invention
[0005] To address the aforementioned technical problems, embodiments of this application provide a memory management method and an electronic device. The technical solution provided in this application configures three asynchronous threads to manage the memory generated by different types of pages. This decouples the memory management processes for different types of pages, improving memory management efficiency.
[0006] To achieve the above-mentioned technical objectives, the embodiments of this application provide the following technical solutions:
[0007] Firstly, a memory management method is provided for use in an electronic device. The method includes: upon detecting a memory allocation request, waking up an anonymous page compaction thread, and / or a page swapping thread, and / or a file page reclamation thread; the anonymous page compaction thread manages anonymous pages in memory, the page swapping thread manages compacted anonymous pages in memory, and the file page reclamation thread manages file pages in memory. At least two of the anonymous page compaction thread, page swapping thread, and file page reclamation thread are run asynchronously to acquire free memory.
[0008] In this way, electronic devices can achieve on-demand and efficient management of different page types by asynchronously running at least two of the three memory management threads. This effectively reduces memory pressure and improves system performance.
[0009] According to the first aspect, at least two of the following threads—an anonymous page compression thread, a page swapping thread, and a file page reclamation thread—are run asynchronously to acquire free memory: the anonymous page compression thread determines whether the anonymous page size satisfies a first condition, which includes the anonymous page size being greater than or equal to a first watermark value. If the anonymous page size satisfies the first condition, the anonymous page compression thread determines the target compression amount based on the anonymous page result refault value, and reclaims anonymous pages based on the target compression amount; wherein, the anonymous page refault value includes the number of anonymous pages that were decompressed after the previous anonymous page compression thread was awakened. If the anonymous page size does not satisfy the first condition, the anonymous page compression thread enters a sleep state.
[0010] In some embodiments, a watermark control mechanism is set in the kernel so that page reclamation can be triggered when system memory is insufficient. For example, a watermark is set corresponding to the amount of anonymous pages in memory (e.g., in MB), such as a high watermark (i.e., the first watermark value), a low watermark (i.e., the second watermark value), and a min watermark (i.e., the third watermark value). This watermark is used to measure the amount of anonymous pages occupied in memory. As applications request memory, the amount of anonymous pages in memory increases accordingly. If the amount of anonymous pages occupied exceeds the high watermark, it indicates that there are too many anonymous pages being used.
[0011] For example, after the anonymous page compaction thread is awakened and exits the sleep state, it determines whether the number of anonymous pages in memory (e.g., in MB) is greater than or equal to the value corresponding to the high watermark. If the number of anonymous pages in memory is greater than or equal to the value corresponding to the high watermark, it can be determined that the number of anonymous pages occupied is too high, and anonymous page reclamation is required to release free memory. If the number of anonymous pages in memory is less than the value corresponding to the high watermark, it can be determined that the number of anonymous pages occupied is not currently exceeding the requirement, and anonymous page reclamation may not be triggered.
[0012] In this way, a separate anonymous page compression thread is set up for recycling anonymous pages, thereby avoiding the impact of file page recycling on anonymous page recycling and improving the efficiency of anonymous page recycling.
[0013] According to the first aspect, or any implementation of the first aspect above, the anonymous page compression thread determines the target compression amount based on the anonymous page refault value, including: the anonymous page compression thread determines that the anonymous page refault value is greater than a first threshold, and determines the target compression amount as the difference between the anonymous page size and a second watermark value, where the second watermark value is less than the first watermark value. Alternatively, the anonymous page compression thread determines that the anonymous page refault value is less than or equal to the first threshold, and determines the target compression amount as the difference between the anonymous page size and a third watermark value, where the third watermark value is less than the second watermark value.
[0014] For example, if the refault value is greater than the first threshold, it can be determined that the previous anonymous page compaction thread compacted too many anonymous pages, and the number of anonymous pages to be compacted needs to be reduced this time. Therefore, during the memory reclamation process of this anonymous page compaction thread, it can be determined to compress the anonymous page usage to the value corresponding to the low watermark (i.e., the second watermark value).
[0015] For example, if the refault value is less than or equal to the first threshold, it can be determined that the previous anonymous page compression thread compressed a small number of anonymous pages, and the number of anonymous pages to be compressed needs to be increased this time. Therefore, during the memory reclamation process, the anonymous page compression thread can determine to compress the anonymous page usage to the value corresponding to the min watermark (i.e., the third watermark value).
[0016] According to the first aspect, or any implementation of the first aspect above, at least two of the following threads—an anonymous page compression thread, a page swapping thread, and a file page reclamation thread—are run asynchronously to acquire free memory. This includes: the page swapping thread determining whether the compressed anonymous page size satisfies a second condition, whereby the compressed anonymous page size is greater than or equal to the fourth watermark value. If the compressed anonymous page size satisfies the second condition, the page swapping thread determines the target swap-out amount based on the refault value of the compressed anonymous page result, and reclaims the compressed anonymous pages according to the target swap-out amount; wherein, the compressed anonymous page refault value includes the number of compressed anonymous pages swapped back after the previous page swapping thread was awakened. If the compressed anonymous page size does not satisfy the second condition, the page swapping thread enters a sleep state.
[0017] In some embodiments, a watermark control mechanism is set in the kernel so that page reclamation can be triggered when system memory is insufficient. For example, a watermark is set corresponding to the size of compressed anonymous pages in memory (e.g., in MB), such as a high watermark (i.e., the fourth watermark value), a low watermark (i.e., the fifth watermark value), and a min watermark (i.e., the sixth watermark value). This watermark is used to measure the amount of compressed anonymous pages occupied in memory. Optionally, compressed anonymous pages are stored in ZRAM, so the compressed anonymous page watermark is the watermark corresponding to the compressed anonymous pages in ZRAM. The page swapping thread determines whether the size of compressed anonymous pages in ZRAM meets the watermark.
[0018] For example, after the page swapping thread is awakened and exits the sleep state, it determines whether the number of compressed anonymous pages in memory (e.g., in MB) is greater than or equal to the value corresponding to the high watermark. If the number of compressed anonymous pages in memory is greater than or equal to the value corresponding to the high watermark, it can be determined that the compressed anonymous page usage is too high, and compressed anonymous page reclamation is required to release free memory. If the number of compressed anonymous pages in memory is less than the value corresponding to the high watermark, it can be determined that the compressed anonymous page usage has not yet exceeded the requirement, and compressed anonymous page reclamation may not be triggered.
[0019] In this way, a separate page swapping thread is set up for recycling compressed anonymous pages, thereby avoiding the impact of file page recycling on compressed anonymous page recycling and improving the efficiency of compressed anonymous page recycling.
[0020] According to the first aspect, or any implementation of the first aspect above, the page swapping thread determines the target swap amount based on the refault value of the compressed anonymous page, including: the page swapping thread determines that the refault value of the compressed anonymous page is greater than the second threshold, and determines that the target swap amount is the difference between the compressed anonymous page level and the fifth watermark value, where the fifth watermark value is less than the fourth watermark value. Alternatively, the page swapping thread determines that the refault value of the compressed anonymous page is less than or equal to the second threshold, and determines that the target swap amount is the difference between the compressed anonymous page level and the sixth watermark value, where the sixth watermark value is less than the fifth watermark value.
[0021] For example, if the refault value is greater than the second threshold, it can be determined that the number of compressed anonymous pages swapped out by the previous page swapping thread was too high, and the number of compressed anonymous pages swapped out needs to be reduced this time. Therefore, during the memory reclamation process performed by the page swapping thread this time, it can be determined that the amount of compressed anonymous pages occupied should be swapped out to the value corresponding to the low watermark (i.e., the fifth watermark value).
[0022] For example, if the refault value is less than or equal to the second threshold, it can be determined that the number of compressed anonymous pages swapped out in the previous page swapping thread was too small, and the number of compressed anonymous pages swapped out this time needs to be increased. Therefore, during the memory reclamation process performed by the page swapping thread this time, it can be determined that the amount of compressed anonymous pages occupied will be compressed to the value corresponding to the minimum watermark (i.e., the sixth watermark value).
[0023] According to the first aspect, or any implementation of the first aspect above, at least two of the following threads—anonymous page compression thread, a page swapping thread, and a file page reclamation thread—are run asynchronously to acquire free memory. This includes: the file page reclamation thread determining whether a storage region in memory meets a third condition, where the third condition includes the amount of free memory in the storage region being less than or equal to the seventh watermark value. If the storage region in memory meets the third condition, the file page reclamation thread determines the difference between the amount of free memory in the storage region and the eighth watermark value as the target reclamation amount, and reclaims file pages according to the target reclamation amount; wherein the eighth watermark value is greater than the seventh watermark value. If the storage region in memory does not meet the third condition, the file page reclamation thread enters a sleep state.
[0024] In some embodiments, a watermark control mechanism is set in the kernel so that page reclamation can be triggered when system memory is insufficient. For example, based on the different functions of different storage zones in memory, a watermark corresponding to file pages is pre-set for each zone. The watermarks for file pages in different zones may be the same or different. When memory is insufficient in any zone, file page reclamation can be triggered by the file page reclamation thread. Watermarks are set for each zone, including a high watermark (i.e., the eighth watermark value), a low watermark (i.e., the seventh watermark value), and a min watermark. This watermark measures the amount of free memory in a zone. As applications request memory, the amount of free memory in a zone decreases. If the amount of free memory reaches the low watermark, it indicates that the amount of free memory is low, and the file page reclamation thread needs to perform file page reclamation. If the amount of free memory reaches the min watermark, it indicates that the amount of free memory is too low, and the system needs to directly perform memory reclamation.
[0025] In this way, a separate file page reclamation thread is set up to reclaim file pages, thereby avoiding the impact of anonymous page reclamation on file page reclamation and improving file page reclamation efficiency.
[0026] According to the first aspect, or any implementation of the first aspect above, the seventh watermark values corresponding to different storage areas in memory may be the same or different.
[0027] According to the first aspect, or any implementation of the first aspect above, upon detecting a memory allocation request, the anonymous page compaction thread, and / or the page swapping thread, and / or the file page reclamation thread are awakened; this includes: upon detecting a memory allocation request, first awakening the anonymous page compaction thread and / or the page swapping thread. Only after determining that free memory cannot be acquired is the file page reclamation thread awakened.
[0028] Thus, by waking up the anonymous page compaction thread and the page swapping thread, the number of times the memory management thread is woken up is increased, thereby increasing the amount of free memory. Furthermore, since the anonymous page compaction thread and the page swapping thread perform anonymous page reclamation and compaction without causing corresponding anonymous page data loss, these two threads can be woken up first to achieve memory reclamation.
[0029] According to the first aspect, or any implementation of the first aspect above, the method further includes: if the file page recycling thread is woken up a preset number of times and no free memory is available, the memory occupied by infrequently used data in memory is released according to a preset rule.
[0030] Optionally, since file pages are mapped and released relatively quickly, electronic devices can repeatedly wake up the file page reclamation thread during slow path memory management to increase memory reclamation efficiency.
[0031] Secondly, an electronic device is provided. The electronic device includes a processor and a memory, the memory being coupled to the processor. The memory stores computer program code, including computer instructions. When the processor reads computer instructions from the memory, the electronic device can execute: upon detecting a memory allocation request, waking up an anonymous page compaction thread, and / or a page swapping thread, and / or a file page reclamation thread; the anonymous page compaction thread manages anonymous pages in memory, the page swapping thread manages compacted anonymous pages in memory, and the file page reclamation thread manages file pages in memory. At least two of the anonymous page compaction thread, page swapping thread, and file page reclamation thread are run asynchronously to acquire free memory.
[0032] According to the second aspect, at least two of the following threads—an anonymous page compression thread, a page swapping thread, and a file page reclamation thread—are run asynchronously to acquire free memory: The anonymous page compression thread determines whether the anonymous page size satisfies a first condition, which includes the anonymous page size being greater than or equal to a first watermark value. If the anonymous page size satisfies the first condition, the anonymous page compression thread determines the target compression amount based on the anonymous page result refault value and reclaims anonymous pages according to the target compression amount; wherein, the anonymous page refault value includes the number of anonymous pages that were decompressed after the previous anonymous page compression thread was awakened. If the anonymous page size does not satisfy the first condition, the anonymous page compression thread enters a sleep state.
[0033] According to the second aspect, or any implementation of the second aspect above, the anonymous page compression thread determines the target compression amount based on the anonymous page refault value, including: the anonymous page compression thread determines that the anonymous page refault value is greater than a first threshold, and determines the target compression amount as the difference between the anonymous page size and a second watermark value, where the second watermark value is less than the first watermark value. Alternatively, the anonymous page compression thread determines that the anonymous page refault value is less than or equal to the first threshold, and determines the target compression amount as the difference between the anonymous page size and a third watermark value, where the third watermark value is less than the second watermark value.
[0034] According to the second aspect, or any implementation of the second aspect above, at least two of the following threads—an anonymous page compression thread, a page swapping thread, and a file page reclamation thread—are run asynchronously to acquire free memory. This includes: the page swapping thread determining whether the compressed anonymous page size satisfies a second condition, whereby the compressed anonymous page size is greater than or equal to the fourth watermark value. If the compressed anonymous page size satisfies the second condition, the page swapping thread determines the target swap-out amount based on the refault value of the compressed anonymous page result, and reclaims the compressed anonymous pages according to the target swap-out amount; wherein, the compressed anonymous page refault value includes the number of compressed anonymous pages swapped back after the previous page swapping thread was awakened. If the compressed anonymous page size does not satisfy the second condition, the page swapping thread enters a sleep state.
[0035] According to the second aspect, or any implementation of the second aspect above, the page swapping thread determines the target swap amount based on the refault value of the compressed anonymous page, including: the page swapping thread determines that the refault value of the compressed anonymous page is greater than the second threshold, and determines that the target swap amount is the difference between the compressed anonymous page magnitude and the fifth watermark value, where the fifth watermark value is less than the fourth watermark value. Alternatively, the page swapping thread determines that the refault value of the compressed anonymous page is less than or equal to the second threshold, and determines that the target swap amount is the difference between the compressed anonymous page magnitude and the sixth watermark value, where the sixth watermark value is less than the fifth watermark value.
[0036] According to the second aspect, or any implementation of the second aspect above, at least two of the following threads—anonymous page compression thread, a page swapping thread, and a file page reclamation thread—are run asynchronously to acquire free memory. This includes: the file page reclamation thread determining whether a storage region in memory meets a third condition, where the third condition includes the amount of free memory in the storage region being less than or equal to the seventh watermark value. If the storage region in memory meets the third condition, the file page reclamation thread determines the difference between the amount of free memory in the storage region and the eighth watermark value as the target reclamation amount, and reclaims file pages according to the target reclamation amount; wherein the eighth watermark value is greater than the seventh watermark value. If the storage region in memory does not meet the third condition, the file page reclamation thread enters a sleep state.
[0037] According to the second aspect, or any implementation of the second aspect above, the seventh watermark values corresponding to different storage areas in memory may be the same or different.
[0038] According to the second aspect, or any implementation of the second aspect above, upon detecting a memory allocation request, the anonymous page compaction thread, and / or the page swapping thread, and / or the file page reclamation thread are awakened; this includes: upon detecting a memory allocation request, first awakening the anonymous page compaction thread and / or the page swapping thread. Only after determining that free memory cannot be acquired is the file page reclamation thread awakened.
[0039] According to the second aspect, or any implementation of the second aspect above, when the processor reads computer-readable instructions from memory, it also causes the electronic device to perform the following operations: if the file page reclamation thread is woken up a preset number of times and no free memory is available, the memory occupied by infrequently used data in memory is released according to a preset rule.
[0040] For the technical effects of the second aspect and any of its implementation methods, please refer to the technical effects of the first aspect and any of its implementation methods mentioned above, which will not be repeated here.
[0041] Thirdly, embodiments of this application provide an electronic device having the function of implementing the memory management method as described in the first aspect and any of its possible implementations. This function can be implemented in hardware or by hardware executing corresponding software. The hardware or software includes one or more modules corresponding to the above-described function.
[0042] For the technical effects of the third aspect and any of its implementation methods, please refer to the technical effects of the first aspect and any of its implementation methods mentioned above, which will not be repeated here.
[0043] Fourthly, a computer-readable storage medium is provided. The computer-readable storage medium stores a computer program (also referred to as instructions or code) that, when executed by an electronic device, causes the electronic device to perform the method of the first aspect or any embodiment of the first aspect.
[0044] For the technical effects of the fourth aspect and any of its implementation methods, please refer to the technical effects of the first aspect and any of its implementation methods mentioned above, which will not be repeated here.
[0045] Fifthly, embodiments of this application provide a computer program product that, when run on an electronic device, causes the electronic device to perform the method of the first aspect or any one of the embodiments of the first aspect.
[0046] The technical effects of the fifth aspect and any of its implementations can be found in the first aspect and any of its implementations, and will not be repeated here.
[0047] In a sixth aspect, embodiments of this application provide a circuit system including a processing circuit configured to perform the method of the first aspect or any of the embodiments of the first aspect.
[0048] The technical effects of the sixth aspect and any implementation thereof can be found in the first aspect and any implementation thereof, and will not be repeated here.
[0049] In a seventh aspect, embodiments of this application provide a chip system including at least one processor and at least one interface circuit. The at least one interface circuit is used to perform transceiver functions and send instructions to the at least one processor. When the at least one processor executes the instructions, the at least one processor performs the method of the first aspect or any one of the embodiments of the first aspect.
[0050] The technical effects of the seventh aspect and any implementation thereof can be found in the first aspect and any implementation thereof, and will not be repeated here. Attached Figure Description
[0051] Figure 1 Flowchart of the memory management method provided in the embodiments of this application Figure 1 ;
[0052] Figure 2 This is a schematic diagram of the execution lifecycle of kswapd provided in the embodiments of this application;
[0053] Figure 3 This is a schematic diagram of the kswapd waterline control mechanism provided in an embodiment of this application;
[0054] Figure 4 This is a schematic diagram of the types of electronic devices provided in the embodiments of this application;
[0055] Figure 5 A schematic diagram of the hardware structure of the electronic device provided in the embodiments of this application;
[0056] Figure 6 A schematic diagram of the Linux memory management architecture provided in this application embodiment;
[0057] Figure 7 Flowchart of the memory management method provided in the embodiments of this application Figure 2 ;
[0058] Figure 8 A flowchart illustrating the memory management method for the anonymous page compression thread provided in this application embodiment;
[0059] Figure 9 A schematic diagram of the anonymous page compression thread waterline control mechanism provided in this application embodiment;
[0060] Figure 10 A flowchart illustrating the memory management method for the page swapping thread provided in this application embodiment;
[0061] Figure 11 A schematic diagram of the page swapping thread waterline control mechanism provided in the embodiments of this application;
[0062] Figure 12 A flowchart illustrating the memory management method of the file page reclamation thread provided in this application embodiment;
[0063] Figure 13 A schematic diagram of the file page reclamation thread waterline control mechanism provided in this application embodiment;
[0064] Figure 14 Flowchart of the memory management method provided in the embodiments of this application Figure 3 ;
[0065] Figure 15 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. Detailed Implementation
[0066] The technical solutions of the embodiments of this application are described below with reference to the accompanying drawings. In the description of the embodiments of this application, the terminology used in the following embodiments is for the purpose of describing specific embodiments only and is not intended to be a limitation of this application. As used in the specification and appended claims of this application, the singular expressions “a,” “an,” “the,” “the,” “the,” and “this” are intended to include expressions such as “one or more,” unless the context clearly indicates otherwise. It should also be understood that in the following embodiments of this application, “at least one” and “one or more” refer to one or more (including two).
[0067] References to "one embodiment" or "some embodiments" in this specification mean that one or more embodiments of this application include a specific feature, structure, or characteristic described in connection with that embodiment. Therefore, the phrases "in one embodiment," "in some embodiments," "in other embodiments," "in still other embodiments," etc., appearing in different parts of this specification do not necessarily refer to the same embodiment, but rather mean "one or more, but not all, embodiments," unless otherwise specifically emphasized. The terms "comprising," "including," "having," and variations thereof mean "including but not limited to," unless otherwise specifically emphasized. The term "connection" includes direct connections and indirect connections, unless otherwise stated. "First" and "second" are used for descriptive purposes only and should not be construed as indicating or implying relative importance or implicitly specifying the number of technical features indicated.
[0068] In the embodiments of this application, the words "exemplarily" or "for example" are used to indicate examples, illustrations, or explanations. Any embodiment or design described as "exemplarily" or "for example" in the embodiments of this application should not be construed as being more preferred or advantageous than other embodiments or design solutions. Specifically, the use of the words "exemplarily" or "for example" is intended to present the relevant concepts in a specific manner.
[0069] In some embodiments, applications request different types of pages from the system during runtime, including anonymous pages and file pages. As the number of applications running on electronic devices increases, and the complexity of their functions grows, system memory usage also increases. Therefore, it is necessary to manage the memory reclamation process for these pages.
[0070] Anonymous pages include memory without a file background on disk, such as heap, stack, and data segments that are not mapped to data on disk. Since anonymous pages have no background files on disk, releasing them during memory reclamation can lead to data loss and affect future access. Therefore, memory reclamation typically uses page compression to free up more memory by compressing reclaimable anonymous pages. This allows for decompression and reuse of the compressed anonymous page when needed, preventing disruption to future access.
[0071] File pages include memory with a file background on disk, such as disk-mapped memory, caches, and buffers. Because file pages are mapped and released quickly, they are typically used to acquire more memory during memory reclamation. For example, when a process requests to load an image from disk, the kernel allocates memory (such as a file page) for the image and establishes a mapping between the file page's virtual address and the image's physical address, storing this mapping in a page table to complete the memory allocation. When a process modifies data on a file page in memory, the kernel writes the modified data to disk based on the mapping recorded in the page table, preventing data loss during subsequent memory reclamation. Therefore, free memory can be acquired by releasing file pages during memory reclamation.
[0072] Generally, electronic device kernels employ memory management methods such as memory compaction (Zram) and memory swapping. For example, memory management is performed through the kernel swap daemon (kswapd) in the Linux kernel. When an electronic device determines that free memory does not meet the memory requirements of an application, it can use kswapd to reclaim memory and release free memory for application use. kswapd performs memory reclamation through methods such as compacting anonymous pages and releasing file pages.
[0073] For example, Figure 1 This is a schematic diagram of the memory management process provided in an embodiment of this application. Figure 1 As shown, after the electronic device detects an application requesting memory (i.e., step 1.1), it determines whether a page can be obtained from the free list (i.e., step 1.2). Optionally, the electronic device organizes the free memory in memory into a free list. After detecting a memory request, the memory in the free list is allocated to the requesting process. And after memory reclamation, the reclaimed memory is reinserted into the free list for subsequent memory allocation. Thus, during the memory allocation process, if the electronic device obtains a page from the free list (the memory allocation process uses pages as the unit of memory allocation), it indicates that the page request is successful (i.e., step 1.3), and the page is used by the application requesting memory; if the electronic device fails to obtain a page from the free list, it can enter the slow path (i.e., step 1.4), where the electronic device attempts to reclaim memory.
[0074] Specifically, the slow path process involves the electronic device reclaiming memory via kswapd. After attempting to wake up kswapd (step 1.5.1), the electronic device determines whether a page can be acquired from the free list (step 1.5.2). kswapd is an asynchronous thread; during step 1.5.2, it asynchronously reclaims memory upon being woken up. If the electronic device acquires a page from the free list, the page allocation is successful (step 1.3), and the page is used by the application requesting memory. If the electronic device fails to acquire a page from the free list, kswapd is woken up again (step 1.6.1), and the process continues to determine whether a page can be acquired from the free list (step 1.6.2). Subsequently, if the electronic device acquires a page from the free list, the page allocation is successful (step 1.3), and the page is used by the application requesting memory. If the electronic device fails to acquire a page from the free list, the memory is reclaimed directly by the system (step 1.7). If the page is successfully acquired after direct reclamation (i.e., step 1.8), the page allocation is successful (i.e., step 1.3), and the page is used by the application requesting memory. For example, in step 1.7, the application thread directly requests free memory from the kernel. If the kernel determines that there is no free memory, it can try to acquire free memory by actively releasing infrequently used processes, offline processes, etc. If free memory is acquired, it can be allocated to the application requesting memory, indicating that the page allocation is successful. In step 1.8, if the page is still not acquired after direct reclamation, the electronic device determines whether to retry (i.e., step 1.9). If a retry is determined, the process returns to step 1.6.1, and kswapd is woken up again to continue memory reclamation; if no further retry is determined, the out-of-memory (OOM) process is triggered (i.e., step 1.10), and the application's memory allocation fails.
[0075] In the above Figure 1 In the illustrated process, in response to an electronic device's attempt to wake up, kswapd, once awakened, executes an asynchronous memory management process. During kswapd's memory reclamation, the electronic device can then determine whether it can directly acquire a page from the free list, thereby achieving dynamic memory management.
[0076] For example, Figure 2 This is a schematic diagram of the kswapd execution lifecycle provided in an embodiment of this application. Figure 2As shown, after kswapd is woken up, it performs initialization operations (i.e., step 2.1). kswapd determines whether each storage zone meets the watermark requirements (i.e., step 2.2). If the watermark requirements are not met, it can enter a sleep state and wait for the next wake-up (i.e., step 2.3); if the watermark requirements are met, memory reclamation can begin.
[0077] Among them, such as Figure 3 As shown, the amount of free memory decreases as applications request memory. Watermarks are set based on zone capacity, including a high watermark (upper limit), a low watermark (lower limit), and a minimum watermark. When kswapd is awakened, it begins memory reclamation when it determines that free memory in a zone has fallen below the low watermark, continuing until free memory reaches the high watermark. Alternatively, if free memory is determined to be below the minimum watermark, the system executes a direct reclamation process to quickly acquire free memory. Optionally, different zones have different functions; kswapd can manage multiple zones. If it determines that free memory in any zone has fallen below the low watermark, kswapd begins memory reclamation.
[0078] like Figure 2 As shown, kswapd determines when free memory drops below the low waterline and begins the memory management process. First, kswapd initializes the reclamation parameters (step 2.4), then calculates the target reclamation amount based on the current storage status of each zone (step 2.5). The memory within the system changes dynamically. Therefore, if an application requests memory, such as... Figure 3 As shown, kswapd determines the difference between the amount of free memory below the low watermark and the high watermark, using this difference as the target reclamation amount to determine whether to continue reclamation (i.e., step 2.6). This ensures that after memory reclamation, the amount of free memory exceeds the high watermark. Then, it iterates sequentially through the memory control group (memcg) (i.e., step 2.7) and the least recently used (lru) list (i.e., step 2.9) to determine reclaimable memory.
[0079] After calculating the target reclamation amount and determining to continue reclamation (i.e., step 2.6), kswapd can begin traversing memcg. During the traversal of memcg, the target reclamation amount for file pages and anonymous pages is calculated proportionally (i.e., step 2.8). Then, kswapd traverses the LRU linked list (i.e., step 2.9), and reclaims the file pages and anonymous pages in the LRU linked list under memcg proportionally according to the calculated target reclamation amount (i.e., steps 2.10-2.12).
[0080] For example, in step 2.5, kswapd determines the target reclamation amount to be 100M, and the preset reclamation ratio of file pages and anonymous pages in the LRU list is 1:1. Therefore, in this loop, kswapd needs to reclaim 50M of anonymous pages and 50M of file pages.
[0081] It should be noted that, as Figure 2 As shown, if kswapd fails in any of the decision steps and the final recycling step, it can return to the previous decision step and continue executing the loop step. After memory reclamation, the free memory meets the high waterline requirement.
[0082] In this way, electronic devices achieve memory reclamation through kswapd, meeting the application's memory requirements. However, for various types of pages, single-threaded memory reclamation cannot achieve on-demand memory management, the memory reclamation process has poor accuracy, and the execution steps are numerous, affecting reclamation efficiency.
[0083] For example, as shown in the example above, kswapd determines that the target reclamation amount is 100MB, requiring the reclamation of 50MB of anonymous pages and 50MB of file pages. However, if there are insufficient anonymous pages in the storage area, only 25MB of anonymous pages can be reclaimed, while the number of file pages is sufficient, and file page reclamation will stop after reclamation of 50MB of file pages. In other words, this loop process can only reclaim 75MB (25MB + 50MB) of memory, leaving 25MB of memory unreclaimed. Therefore, kswapd needs to repeat the process as follows: Figure 2 The process shown reclaims anonymous pages and file pages at a 1:1 reclamation ratio.
[0084] It can be seen that because a single thread cannot perform precise memory management according to page type, the memory management process is repeatedly executed, resulting in resource waste and affecting recycling efficiency, which in turn affects the application's page loading.
[0085] Therefore, this application provides a memory management method that configures three asynchronous threads to manage the memory generated by different types of pages. This decouples the memory management processes for different page types and improves memory reclamation efficiency.
[0086] The memory management method provided in this application embodiment can be applied to electronic device 100. For example, as Figure 4 As shown, electronic device 100 can specifically be a mobile phone 41, laptop computer 42, tablet computer 43, large-screen device 44, wearable device (such as smartwatch, smart bracelet, etc.) 45, in-vehicle device, ultra-mobile personal computer (UMPC), netbook, personal digital assistant (PDA), artificial intelligence (AI) device, and other terminal devices. Optionally, the operating system installed on electronic device 100 may include, but is not limited to, […]. Alternatively, other operating systems may be used. In some embodiments, the electronic device 100 may be a fixed device or a portable device. This application does not limit the specific type of the electronic device 100 or the operating system installed on it.
[0087] For example, Figure 5 A schematic diagram of an electronic device 100 is shown.
[0088] Electronic device 100 may include processor 110, external memory interface 120, internal memory 121, universal serial bus (USB) interface 130, charging management module 140, power management module 141, battery 142, antenna 1, antenna 2, mobile communication module 150, wireless communication module 160, audio module 170, sensor module 180, button 190, motor 191, indicator 192, camera 193, display screen 194, and subscriber identification module (SIM) card interface 195, etc.
[0089] It is understood that the structures illustrated in the embodiments of this application do not constitute a specific limitation on the electronic device 100. In other embodiments of this application, the electronic device 100 may include more or fewer components than illustrated, or combine some components, or split some components, or have different component arrangements. The illustrated components may be implemented in hardware, software, or a combination of software and hardware.
[0090] Processor 110 may include one or more processing units, such as application processors (APs), modem processors, graphics processing units (GPUs), image signal processors (ISPs), controllers, video codecs, digital signal processors (DSPs), baseband processors, and / or neural network processing units (NPUs). These different processing units may be independent devices or integrated into one or more processors.
[0091] The controller can generate operation control signals based on the instruction opcode and timing signals to complete the control of instruction fetching and execution.
[0092] The processor 110 may also include a memory for storing instructions and data. In some embodiments, the memory in the processor 110 is a cache memory. This memory can store instructions or data that the processor 110 has just used or that are used repeatedly. If the processor 110 needs to use the instruction or data again, it can retrieve it directly from the memory. This avoids repeated accesses, reduces the waiting time of the processor 110, and thus improves the efficiency of the system.
[0093] In some embodiments, the processor 110 may include one or more interfaces. Interfaces may include an inter-integrated circuit (I2C) interface, an inter-integrated circuit sound (I2S) interface, a pulse code modulation (PCM) interface, a universal asynchronous receiver / transmitter (UART) interface, a mobile industry processor interface (MIPI), a general-purpose input / output (GPIO) interface, a subscriber identity module (SIM) interface, and / or a universal serial bus (USB) interface, etc.
[0094] USB port 130 is a USB standard compliant interface, specifically a Mini USB port, Micro USB port, USB Type-C port, etc. USB port 130 can be used to connect a charger to charge electronic device 100, and can also be used for data transfer between electronic device 100 and peripheral devices. It can also be used to connect headphones for audio playback. This interface can also be used to connect other electronic devices, such as AR devices.
[0095] It is understood that the interface connection relationships between the modules illustrated in the embodiments of this application are merely illustrative and do not constitute a structural limitation on the electronic device 100. In other embodiments of this application, the electronic device 100 may also employ different interface connection methods or combinations of multiple interface connection methods as described in the above embodiments.
[0096] The charging management module 140 receives charging input from a charger. The charger can be a wireless charger or a wired charger. In some wired charging embodiments, the charging management module 140 receives charging input from the wired charger via the USB interface 130. In some wireless charging embodiments, the charging management module 140 receives wireless charging input via the wireless charging coil of the electronic device 100. While charging the battery 142, the charging management module 140 can also supply power to the electronic device via the power management module 141.
[0097] The power management module 141 connects the battery 142, the charging management module 140, and the processor 110. The power management module 141 receives input from the battery 142 and / or the charging management module 140, providing power to the processor 110, internal memory 121, display screen 194, camera 193, and wireless communication module 160, etc. The power management module 141 can also monitor parameters such as battery capacity, battery cycle count, and battery health status (leakage current, impedance). In some other embodiments, the power management module 141 may also be located within the processor 110. In other embodiments, the power management module 141 and the charging management module 140 may be located in the same device.
[0098] The wireless communication function of electronic device 100 can be realized through antenna 1, antenna 2, mobile communication module 150, wireless communication module 160, modem processor and baseband processor, etc.
[0099] Antenna 1 and antenna 2 are used to transmit and receive electromagnetic wave signals. Each antenna in electronic device 100 can be used to cover one or more communication frequency bands. Different antennas can also be multiplexed to improve antenna utilization. For example, antenna 1 can be multiplexed as a diversity antenna for a wireless local area network. In some other embodiments, the antennas can be used in conjunction with tuning switches.
[0100] The mobile communication module 150 can provide solutions for wireless communication, including 2G / 3G / 4G / 5G, applied to the electronic device 100. The mobile communication module 150 may include at least one filter, switch, power amplifier, low noise amplifier (LNA), etc. The mobile communication module 150 can receive electromagnetic waves via antenna 1, and perform filtering, amplification, and other processing on the received electromagnetic waves before transmitting them to a modem processor for demodulation. The mobile communication module 150 can also amplify the signal modulated by the modem processor and convert it into electromagnetic waves for radiation via antenna 1. In some embodiments, at least some functional modules of the mobile communication module 150 may be housed in the processor 110. In some embodiments, at least some functional modules of the mobile communication module 150 and at least some modules of the processor 110 may be housed in the same device.
[0101] The modem processor may include a modulator and a demodulator. The modulator modulates the low-frequency baseband signal to be transmitted into a mid-to-high frequency signal. The demodulator demodulates the received electromagnetic wave signal into a low-frequency baseband signal. The demodulator then transmits the demodulated low-frequency baseband signal to the baseband processor for processing. After processing by the baseband processor, the low-frequency baseband signal is transmitted to the application processor. The application processor outputs a sound signal through an audio device or displays an image or video through the display screen 194. In some embodiments, the modem processor may be a separate device. In other embodiments, the modem processor may be independent of the processor 110 and may be housed in the same device as the mobile communication module 150 or other functional modules.
[0102] The wireless communication module 160 can provide solutions for wireless communication applications on the electronic device 100, including wireless local area networks (WLAN) (such as wireless fidelity (Wi-Fi) networks), Bluetooth (BT), global navigation satellite system (GNSS), frequency modulation (FM), near field communication (NFC), and infrared (IR) technologies. The wireless communication module 160 can be one or more devices integrating at least one communication processing module. The wireless communication module 160 receives electromagnetic waves via antenna 2, performs frequency modulation and filtering of the electromagnetic wave signals, and sends the processed signal to processor 110. The wireless communication module 160 can also receive signals to be transmitted from processor 110, perform frequency modulation and amplification, and convert them into electromagnetic waves for radiation via antenna 2.
[0103] In some embodiments, antenna 1 of electronic device 100 is coupled to mobile communication module 150, and antenna 2 is coupled to wireless communication module 160, enabling electronic device 100 to communicate with networks and other devices via wireless communication technology. The wireless communication technology may include Global System for Mobile Communications (GSM), General Packet Radio Service (GPRS), Code Division Multiple Access (CDMA), Wideband Code Division Multiple Access (WCDMA), Time Division Code Division Multiple Access (TD-SCDMA), Long Term Evolution (LTE), BT, GNSS, WLAN, NFC, FM, and / or IR technologies, etc. The GNSS may include the Global Positioning System (GPS), the Global Navigation Satellite System (GLONASS), the BeiDou Navigation Satellite System (BDS), the Quasi-Zenith Satellite System (QZSS), and / or satellite-based augmentation systems (SBAS).
[0104] Electronic device 100 implements display functions through a GPU, a display screen 194, and an application processor. The GPU is a microprocessor for image processing, connected to the display screen 194 and the application processor. The GPU is used to perform mathematical and geometric calculations and for graphics rendering. Processor 110 may include one or more GPUs, which execute program instructions to generate or modify display information.
[0105] Display screen 194 is used to display images, videos, etc. Display screen 194 includes a display panel. The display panel can be manufactured using a liquid crystal display (LCD), such as an organic light-emitting diode (OLED), an active-matrix organic light-emitting diode (AMOLED), a flexible light-emitting diode (FLED), a mini-LED, a micro-LED, a micro-OLED, a quantum dot light-emitting diode (QLED), etc. In some embodiments, electronic device 100 may include one or N displays 194, where N is a positive integer greater than 1.
[0106] Camera 193 is used to capture still images or videos. An object is projected onto a photosensitive element by generating an optical image through the lens. The photosensitive element can be a charge-coupled device (CCD) or a complementary metal-oxide-semiconductor (CMOS) phototransistor. The photosensitive element converts the light signal into an electrical signal, which is then passed to an ISP for conversion into a digital image signal. The ISP outputs the digital image signal to a DSP for processing. The DSP converts the digital image signal into image signals in standard RGB, YUV, or other formats. In some embodiments, the electronic device 100 may include one or N cameras 193, where N is a positive integer greater than 1.
[0107] The external storage interface 120 can be used to connect an external memory card, such as a Micro SD card, to expand the storage capacity of the electronic device 100. The external memory card communicates with the processor 110 through the external storage interface 120 to perform data storage functions. For example, music, video, and other files can be saved on the external memory card.
[0108] Internal memory 121 can be used to store computer executable program code, which includes instructions. Internal memory 121 may include a program storage area and a data storage area. The program storage area may store the operating system, at least one application program required for a function (such as sound playback, image playback, etc.), etc. The data storage area may store data created during the use of electronic device 100 (such as audio data, phonebook, etc.). Furthermore, internal memory 121 may include high-speed random access memory, and may also include non-volatile memory, such as at least one disk storage device, flash memory device, universal flash storage (UFS), etc. Processor 110 executes various functional applications and data processing of electronic device 100 by running instructions stored in internal memory 121 and / or instructions stored in memory located in the processor.
[0109] In some embodiments, an application installed in the electronic device 100 requests free memory in the internal memory 121 to load pages. If the electronic device 100 determines that there is insufficient free memory, it can perform memory management asynchronously using three threads: a file page reclamation thread, an anonymous page compression thread, and a page swapping thread. This ensures efficient memory management and enables precise memory management.
[0110] The audio module 170 is used to convert digital audio information into analog audio signals for output, and also to convert analog audio input into digital audio signals. The audio module 170 can also be used for encoding and decoding audio signals. In some embodiments, the audio module 170 may be located in the processor 110, or some functional modules of the audio module 170 may be located in the processor 110. The electronic device 100 can use the audio module 170 for functions such as music playback and recording. The audio module 170 may include a speaker, receiver, microphone, headphone jack, and application processor to implement audio functions.
[0111] The sensor module 180 may include pressure sensors, gyroscope sensors, barometric pressure sensors, magnetic sensors, accelerometers, distance sensors, proximity sensors, fingerprint sensors, temperature sensors, touch sensors, ambient light sensors, bone conduction sensors, etc.
[0112] Buttons 190 include a power button, volume buttons, etc. Buttons 190 can be mechanical buttons or touch-sensitive buttons. Electronic device 100 can receive button input and generate key signal inputs related to user settings and function control of electronic device 100.
[0113] Motor 191 can generate vibration alerts. Motor 191 can be used for incoming call vibration alerts or for touch vibration feedback. For example, different vibration feedback effects can be corresponding to touch operations applied to different applications (such as taking photos, playing audio, etc.). Motor 191 can also correspond to different vibration feedback effects for touch operations applied to different areas of the display screen 194. Different application scenarios (such as time reminders, receiving messages, alarm clocks, games, etc.) can also correspond to different vibration feedback effects. The touch vibration feedback effect can also be customized.
[0114] Indicator 192 can be an indicator light, used to indicate charging status, power changes, or to indicate messages, missed calls, notifications, etc.
[0115] The SIM card interface 195 is used to connect a SIM card. The SIM card can be inserted into or removed from the SIM card interface 195 to achieve contact and separation with the electronic device 100. The electronic device 100 can support one or N SIM card interfaces, where N is a positive integer greater than 1.
[0116] For example, Figure 6 This is a schematic diagram of the Linux memory management architecture provided for an embodiment of this application. The memory management process is described using the Linux system as an example.
[0117] Page table 601 is used to store the mapping relationship between the virtual address and physical address of a page. For example, when a process requests to load a page, the kernel allocates memory for the requested page and assigns the corresponding virtual address. Then, the established mapping relationship between the page's virtual address and physical address is cached in page table 601.
[0118] CPU page cache 602 is a page buffer between the CPU and memory, used to cache various types of pages frequently used by the CPU, thereby improving page loading efficiency.
[0119] Slab cache 603 is a cache page for the slab allocator, used to handle frequently allocated and freed objects. Optionally, directly using the buddy system for such frequently allocated and freed objects would generate significant memory fragmentation and impact processing speed. Therefore, using slab cache 603 to handle these objects improves caching efficiency.
[0120] The buddy system 604 is a page allocation management system that reduces external memory fragmentation. Optionally, free memory in the system is always grouped in pairs, with the two memory blocks in each group called buddies. Buddies can be allocated independently. However, if two buddies are both free, the kernel merges them into a larger memory block, making it a buddy for a memory block at the next lower level.
[0121] Optionally, the Buddy System 604 includes an anti-fragmentation management mechanism, which is a page fragmentation optimization mechanism that can reduce fragmentation through page migration (i.e., migrating page content to a new page).
[0122] File recycling thread 605, anonymous page compression thread 606, and page swapping thread 607 are kernel memory management threads that can perform memory management asynchronously in response to memory allocation requests.
[0123] The Out of Memory (OOM) error 608 is used to free up memory by stopping processes when memory resources are scarce. For example, the system may proactively close infrequently used or offline processes, thereby freeing up memory for the current process. Optionally, during the OOM process, if free memory cannot be obtained, a memory request failure response can be directly returned to the process, confirming the failure of the memory request.
[0124] The following uses electronic device 100 as an example to describe the memory management method provided in this application in detail with reference to various embodiments.
[0125] For example, Figure 7 This is a schematic flowchart of a memory management method provided in an embodiment of this application. Figure 7 As shown, this method is applied to electronic devices and includes the following steps.
[0126] S701 receives memory request.
[0127] In some embodiments, during runtime, an application sends a memory request to the electronic device kernel to request different types of pages. Accordingly, upon receiving the memory request, the electronic device kernel determines to allocate free memory to the application, with memory allocation on a page-by-page basis. Optionally, the memory may include different types of pages, such as anonymous pages and file pages.
[0128] S702, Attempt to wake up the anonymous page compression thread and the page swapping thread.
[0129] In some embodiments, an anonymous page compression thread is used to reclaim anonymous pages stored in memory. For example, compressing anonymous pages results in compressed anonymous pages, whose memory footprint is smaller than that of the original anonymous pages. In some scenarios, allocated anonymous pages in memory (such as infrequently used anonymous pages) can also be released. After release, these pages no longer occupy memory. This achieves memory reclamation, increasing free memory available for application use.
[0130] In some embodiments, a page swapping thread is used to reclaim compressed anonymous pages in memory. This includes swapping compressed anonymous pages out of memory to disk for storage, thereby reducing memory usage and facilitating subsequent memory allocation. For example, the page swapping thread might send compressed anonymous pages stored in double data rate (DDR) synchronous dynamic random access memory (SDRAM) to disk via an input / output (I / O) interface. This reduces memory usage in DDR SDRAM, making it easier for applications to request memory from DDR SDRAM later.
[0131] In some embodiments, upon receiving a memory allocation request, the electronic device first wakes up the anonymous page compaction thread and the page swapping thread to perform anonymous page reclamation and compaction. Optionally, in response to the memory allocation request, the system sends a wake-up instruction to the anonymous page compaction thread and the page swapping thread to wake up these two memory management threads.
[0132] In this process, the anonymous page compaction thread reclaims memory by compaction. Since this process does not result in data loss, the anonymous page compaction thread can be woken up first to perform memory reclamation. Then, when a subsequent request from a process to use a compressed anonymous page is detected, the compressed anonymous page can be decompressed and allocated to the process. Similarly, the page swapping thread reclaims memory by swapping compressed anonymous pages from memory to disk. Again, this process does not result in data loss, so the page swapping thread can be woken up first to perform memory reclamation. Then, when a subsequent request from a process to use a swapped-out anonymous page is detected, the compressed anonymous page can be swapped back into memory and decompressed, then allocated to the process. Therefore, by waking up both the anonymous page compaction thread and the page swapping thread first, the number of times the memory management thread is woken up is increased, thus increasing the amount of free memory.
[0133] It should be noted that the embodiments of this application do not limit the wake-up order of the anonymous page compaction thread and the page swapping thread. For example, in response to a memory allocation request, the electronic device wakes up the anonymous page compaction thread first, and then wakes up the page swapping thread. Or, in response to a memory allocation request, the electronic device wakes up the page swapping thread first, and then wakes up the anonymous page compaction thread. Or, in response to a memory allocation request, the electronic device wakes up both the anonymous page compaction thread and the page swapping thread simultaneously.
[0134] Furthermore, upon receiving a memory allocation request, the electronic device may wake up either the anonymous page compaction thread or the page swapping thread. Then, if a page cannot be directly obtained from the free list in step S703 below, another type of unawakened memory management thread will be woken up. For example, in response to a memory allocation request, the electronic device wakes up the anonymous page compaction thread. Then, if a page cannot be directly obtained from the free list in step S703 below, the page swapping thread will be woken up. Or, for example, in response to a memory allocation request, the electronic device wakes up the page swapping thread. Then, if a page cannot be directly obtained from the free list in step S703 below, the anonymous page compaction thread will be woken up. In other words, the electronic device may wake up the anonymous page compaction thread and / or the page swapping thread in response to a memory allocation request.
[0135] S703. Does the page need to be retrieved directly from the free list? If the page is retrieved, proceed to step S704; if the page is not retrieved, proceed to step S705.
[0136] In some embodiments, the anonymous page compression thread and the page swapping thread are asynchronous threads (i.e., they do not affect the operation of other threads in the electronic device). Therefore, after the electronic device wakes up the anonymous page compression thread and the page swapping thread, in the process of determining whether a page can be obtained from the free list (i.e., determining whether there is currently available free memory), the anonymous page compression thread and the page swapping thread asynchronously perform memory management. If the electronic device directly obtains a page during this process, it can be determined that the memory allocation was successful, and the current memory management process ends (i.e., step S704 below is executed), but this does not affect the memory management performed by the already woken anonymous page compression thread and the page swapping thread. If a page cannot be directly obtained, it can be determined that memory management is needed to obtain free memory (i.e., step S705 below is executed).
[0137] Optionally, as described above, the electronic device organizes free memory in memory into a linked list, forming a free list. If the electronic device determines that it can obtain a page from the free list, it can determine that there is free memory available to allocate to the application, and the application's memory request is successful. If the electronic device determines that it cannot obtain a page from the free list and that there is currently no free memory available for allocation, it can determine to perform memory reclamation.
[0138] S704, memory allocation successful.
[0139] In some embodiments, after the electronic device determines that a memory request has been successfully made, it allocates the corresponding memory to the requesting application and ends the memory management process. However, the electronic device ends... Figure 7 The memory management process shown does not affect the memory management performed by the already awakened anonymous page compaction thread and page swapping thread.
[0140] S705, Attempt to wake up the file page recycling thread.
[0141] In some embodiments, a file page reclamation thread is used to reclaim file pages in memory, such as writing back and releasing file pages, thereby reducing the memory footprint of file pages. Optionally, the system sends a wake-up instruction to the file page reclamation thread to wake it up.
[0142] Optionally, since memory read / write speeds are much higher than disk read / write speeds, the system caches frequently accessed data in memory, implementing high-speed caching. In Linux, pages are the unit of cache. When a process modifies cached data, the corresponding file page is marked as dirty by the kernel. Subsequently, the kernel writes the data in the dirty page to disk according to preset rules to ensure consistency between the data in the cache and the data on disk; this process is called write-back. Based on this, the file page reclamation thread needs to write back the dirty pages involved during memory management before releasing them. File pages that do not require write-back can be released directly.
[0143] In some embodiments, after determining that it cannot directly obtain a page from the free list, the electronic device may attempt to wake up the file page reclamation thread to perform file page reclamation in order to release the memory occupied by the file pages in memory.
[0144] Optionally, a file page is memory containing a background file on the disk. After the file page reclamation thread reclaims a file page, the mapping relationship between the reclaimed file page and the data on the disk is released. Subsequently, if the electronic device detects a process requesting to read this part of the data on the disk, it needs to reallocate file pages for the process and establish the mapping relationship between this part of the data and the file page. During the process of establishing the mapping relationship, it may affect the process's execution of this part of the disk data, thereby affecting the user experience. For example, when a process reads an image from the disk, it can generate a cache corresponding to the image in memory, thereby increasing the display efficiency of subsequent images. If the file page reclamation thread releases the cache (i.e., file page) in memory, and the electronic device detects an image display request again, it needs to generate the corresponding cache and establish the mapping relationship before displaying the image, thus reducing the image display efficiency. Therefore, in the above step S702, in response to the memory allocation request, the electronic device first attempts to wake up the anonymous page compression thread and the page swapping thread used to manage anonymous pages or compressed anonymous pages without background files on the disk. Then, after determining that a page cannot be directly obtained from the free list, it attempts to wake up the file page reclamation thread to reclaim the file page containing the file background on the disk.
[0145] In other embodiments, the electronic device may also attempt to wake up the file page reclamation thread in response to a memory allocation request. That is, in step S702 above, the electronic device attempts to wake up the anonymous page compaction thread, and / or the page swapping thread, and / or the file page reclamation thread in response to a memory allocation request. Subsequently, after determining that it cannot directly obtain a page from the free list (i.e., step S703), the electronic device may simultaneously or separately attempt to wake up the memory management threads that were not woken up in step S702. That is, the embodiments of this application do not limit the wake-up order of the anonymous page compaction thread, the page swapping thread, and the file page reclamation thread.
[0146] S706. Does the page need to be retrieved directly from the free list? If the page is retrieved, proceed to step S704; if the page is not retrieved, proceed to step S707.
[0147] In some embodiments, the file page reclamation thread is an asynchronous thread (i.e., it does not affect the operation of other threads on the electronic device). Therefore, after the electronic device wakes up the file page reclamation thread, in the process of determining whether a page can be obtained from the free list (i.e., determining whether there is currently available free memory), the file page reclamation thread asynchronously performs memory management. If the electronic device directly obtains a page during this process, it can be determined that the memory allocation was successful, and the current memory management process ends (i.e., step S704 above is executed). The end of the memory management process does not affect the memory management of the already woken file page reclamation thread. If a page cannot be directly obtained, it can be determined that memory management needs to continue to obtain free memory (i.e., step S707 below is executed).
[0148] S707, Attempt to wake up the file page reclamation thread.
[0149] S708. Does the page need to be retrieved directly from the free list? If the page is retrieved, proceed to step S704; if the page is not retrieved, proceed to step S709.
[0150] In some embodiments, during steps S705-S708, the electronic device performs slow-path memory management. For example, it asynchronously repeats the operations of waking up the file page reclamation thread and determining whether a page can be directly obtained from the free list a preset number of times, in order to obtain the free memory requested by the application. Optionally, since file page mapping and release are relatively fast, the electronic device can attempt to repeatedly wake up the file page reclamation thread during slow-path memory management to increase memory reclamation efficiency. After completing its current file page reclamation, the file page reclamation thread enters a sleep state, waiting to be woken up again.
[0151] For example, in steps S705 and S707 above, the electronic device repeats the operation of waking up the file page reclamation thread twice to release file page memory usage and thus acquire more free memory. Furthermore, memory usage in the kernel changes rapidly (e.g., memory usage from other processes ending). Therefore, during the attempt to wake up the file page reclamation thread, the electronic device can also re-determine whether free memory exists (e.g., in the corresponding steps S706 and S708, determining whether a page can be directly obtained from the free list) to increase the likelihood of a successful memory allocation.
[0152] S709 performs memory reclamation directly.
[0153] In some embodiments, if an electronic device fails to acquire the corresponding free memory after attempting to wake up the file page reclamation thread a preset number of times, it can directly perform memory reclamation.
[0154] For example, an electronic device instructs its application to directly request free memory from the kernel based on its memory needs. In this case, the application thread no longer relies on memory management threads (such as anonymous page compaction threads, page swapping threads, and file page reclamation threads) to reclaim memory; instead, it directly sends a memory request to the kernel. Correspondingly, the kernel, based on the memory request from the application thread, directly releases infrequently used memory to acquire free memory. After the memory is released, if free memory is acquired, the kernel can allocate the acquired free memory to the application thread.
[0155] In some embodiments, during the process of memory reclamation directly performed by the kernel in an electronic device, the pages reclaimed include anonymous pages, and / or compressed anonymous pages, and / or file pages, without distinguishing the page type corresponding to the reclaimed memory.
[0156] S710. After memory reclamation, was the page retrieved? If the page was retrieved, proceed to step S704; if the page was not retrieved, proceed to step S711.
[0157] In some embodiments, after the electronic device performs memory reclamation and determines that free memory has been acquired, it can determine that the memory acquisition was successful and end the current memory management process (i.e., execute step S704 above). If it is determined that free memory cannot be acquired, step S711 below can be executed.
[0158] S711. Should I retry? If I need to retry, repeat step S707 above; if I don't need to retry, proceed to step S712.
[0159] In some embodiments, if the electronic device determines that it has failed to acquire free memory, it can determine whether memory reclamation needs to be performed again according to preset rules. If it is determined that memory reclamation needs to be performed again, it can return to step S707 above to attempt to wake up the file page reclamation thread and perform file page memory reclamation. If it is determined that memory reclamation should not be performed again, the OOM process can be triggered (i.e., step S712 below is executed).
[0160] S712, triggers the OOM process.
[0161] In some embodiments, if an electronic device determines that there is insufficient free memory to satisfy an application's memory request and there is no reclaimable memory in memory, it may return a memory request failure response to the application. The application can determine that the memory request has failed based on the memory request failure response.
[0162] Optionally, before returning a memory request failure response to the application, the electronic device can try to acquire free memory by actively releasing infrequently used or offline processes. If free memory is acquired, it can allocate memory to the application. If free memory is still not acquired, a memory request failure response can be sent to the application.
[0163] In this way, electronic devices manage memory by configuring three asynchronous memory management threads (such as anonymous page compression thread, page swapping thread, and file page reclamation thread) to meet the memory requirements of page operation. Furthermore, memory is reclaimed separately according to page type, decoupling the memory management process and effectively improving memory management efficiency.
[0164] The memory management processes of the anonymous page compression thread, page swapping thread, and file page reclamation thread are described in detail below.
[0165] For example, Figure 8 This is a flowchart illustrating the memory management method for the anonymous page compression thread provided in an embodiment of this application. Figure 8 As shown, this method is applied to electronic devices and includes the following steps.
[0166] S801. Initialize the anonymous page compression thread.
[0167] In some embodiments, the anonymous page compression thread is initialized during kernel initialization. Afterwards, the anonymous page compression thread enters a sleep state. Upon receiving a wake-up instruction from the system (as described above)... Figure 7 After step S702 shown, the anonymous page compression thread ends its sleep and begins to execute the memory management process.
[0168] S802. Does the anonymous page size meet the waterline? If it does, proceed to step S804; otherwise, proceed to step S803.
[0169] In some embodiments, a waterline control mechanism is set in the kernel so that page reclamation can be triggered when system memory is insufficient.
[0170] For example, such as Figure 9 As shown, watermarks are set to represent the amount of anonymous pages in memory (e.g., in MB), including a high watermark (the first watermark value), a low watermark (the second watermark value), and a min watermark (the third watermark value). These watermarks measure the amount of anonymous pages in memory. As the application requests memory, the amount of anonymous pages in memory increases. If the amount of anonymous pages exceeds the high watermark, it indicates that there are too many anonymous pages.
[0171] Optionally, after the anonymous page compression thread is awakened and exits the sleep state, it determines whether the number of anonymous pages in memory (e.g., in MB) is greater than or equal to the value corresponding to the high watermark. If the number of anonymous pages in memory is greater than or equal to the value corresponding to the high watermark, it can be determined that the number of anonymous pages occupied is too high, and anonymous page reclamation is required to release free memory (i.e., execute step S804). If the number of anonymous pages in memory is less than the value corresponding to the high watermark, it can be determined that the number of anonymous pages occupied has not yet exceeded the requirements, and anonymous page reclamation may not be triggered (i.e., execute step S803).
[0172] In some embodiments, the waterline corresponding to anonymous pages can be a global waterline or a regional waterline. For example, to better control global resources, a global waterline corresponding to the number of anonymous pages in all memory is set. When the total number of anonymous pages in memory is greater than or equal to the high waterline, it is determined that the number of anonymous pages meets the waterline, and anonymous page reclamation begins. Another example is that memory includes multiple storage zones with different functions. Waterlines corresponding to the number of anonymous pages in different zones are pre-configured, and the anonymous page waterlines corresponding to different zones may be the same or different. When the number of anonymous pages in one or more zones is greater than or equal to the high waterline, it is determined that the number of anonymous pages meets the waterline, and anonymous page reclamation begins. It should be noted that the rules for setting the waterline corresponding to the number of anonymous pages can be determined based on the capabilities of the electronic device, memory size, experimental data, empirical values, etc. The setting method can refer to existing technologies, such as the waterline setting method of kswapd, and this application embodiment does not limit this.
[0173] S803, Anonymous page compression thread sleeps.
[0174] In some embodiments, if the anonymous page compression thread determines that the current number of anonymous pages in memory does not meet the watermark (i.e., the number of anonymous pages is less than the value corresponding to the high watermark), it can re-enter the sleep state and wait to be woken up next time.
[0175] S804. Initialize recycling parameters.
[0176] In some embodiments, the anonymous page compaction thread determines that the current number of anonymous pages in memory meets the watermark (e.g., the number of anonymous pages in memory is greater than or equal to the value corresponding to the high watermark), and can then begin anonymous page reclamation. Before anonymous page reclamation, the reclamation parameters used in the anonymous page reclamation process need to be initialized to facilitate subsequent anonymous page reclamation.
[0177] S805. Determine the target compression amount based on the anonymous page result (refault) value.
[0178] In some embodiments, anonymous pages are compressed during memory reclamation following the previous wake-up. However, during the period from the end of the previous memory reclamation when the anonymous page compression thread enters a sleep state until the current anonymous page compression thread is awakened, some anonymous pages are decompressed. The number of these compressed and then decompressed anonymous pages is the refault value, which the anonymous page compression thread can use to determine the target compression amount for this anonymous page compression.
[0179] For example, if the refault value is greater than a preset threshold (i.e., the first threshold), it can be determined that the previous anonymous page compression thread compressed too many anonymous pages, and the number of anonymous pages to be compressed needs to be reduced this time. Therefore, during the memory reclamation process performed by the anonymous page compression thread this time, it can be determined that the anonymous page usage will be compressed to a certain level. Figure 9 The value corresponding to the low watermark shown is the target compression amount, which is the difference between the number of anonymous pages determined in step S802 above and the value corresponding to the low watermark (e.g., the target compression amount is the difference between the determined number of anonymous pages and the second watermark value).
[0180] For example, if the refault value is less than or equal to a preset threshold (i.e., the first threshold), it can be determined that the previous anonymous page compression thread compressed too few anonymous pages, and the number of anonymous pages to be compressed this time needs to be increased. Therefore, during the memory reclamation process performed by the anonymous page compression thread this time, it can be determined that the anonymous page usage will be compressed to a certain level. Figure 9 The value corresponding to the min waterline shown is the target compression amount, which is the difference between the number of anonymous pages determined in step S802 above and the value corresponding to the min waterline (e.g., the target compression amount is the difference between the determined number of anonymous pages and the third waterline value).
[0181] Among them, such as Figure 9As shown, the value corresponding to the low watermark is greater than the value corresponding to the min watermark (that is, the three watermark values corresponding to the anonymous page are the first watermark value, the second watermark value, and the third watermark value in descending order).
[0182] For example, after the anonymous page compression thread was woken up previously, the number of compressed anonymous pages was 10. After the anonymous page compression thread went to sleep, 5 of these 10 compressed anonymous pages were decompressed. Therefore, after the anonymous page compression thread is woken up this time, it can be determined that the refault value of the anonymous pages is 5. If the preset threshold is 4, the anonymous page compression thread determines that the refault value is greater than the preset threshold. Therefore, the anonymous page compression thread can determine that the target compression amount is the difference between the number of anonymous pages determined in step S802 above and the value corresponding to the low watermark.
[0183] S806. Should memory reclamation continue? If it is determined to continue reclamation, proceed to step S807; if it is determined not to continue reclamation, return to step S805 above.
[0184] In some embodiments, memory usage in the kernel is dynamically changing, such as memory being released after other processes terminate, reducing memory usage. Therefore, after determining the target compression amount, the anonymous page compression thread can determine the change in the number of anonymous pages in memory. If the number of anonymous pages decreases, and the decrease is greater than or equal to the target compression amount, then the anonymous page compression thread can determine that memory reclamation is not necessary and can re-determine the target compression amount (i.e., execute step S805). Alternatively, if the number of anonymous pages does not decrease, or the decrease is less than the target compression amount, then the anonymous page compression thread can determine to continue reclamation (i.e., execute step S807).
[0185] It should be noted that after determining that it will no longer reclaim memory, the anonymous page compaction thread can directly return to step S802 above to reassess whether the anonymous page size meets the waterline requirements. If it is determined that it does not meet the requirements, it can enter a sleep state and end this memory management process.
[0186] S807. Should we continue traversing the memory management group (memcg)? If yes, proceed to step S808; otherwise, return to step S806 above.
[0187] In some embodiments, memcg is a memory subsystem used to manage processes or applications with different memory requirements. memcg maintains an LRU linked list for each storage zone in memory, enabling resource utilization and isolation. As described in step S806 above, memory usage in the kernel changes dynamically. After determining to continue reclaiming memory, the anonymous page compression thread can also determine whether to continue traversing memcg. If it determines to continue traversing memcg, it can determine whether there are compressible anonymous pages in memcg (i.e., execute step S808 below). If it determines not to continue traversing memcg, it can return to determining whether to continue reclaiming memory (i.e., execute step S806 above).
[0188] It should be noted that after the anonymous page compression thread determines that it will no longer traverse memcg, it can directly return to step S805 or step S802.
[0189] Furthermore, after determining the target compression amount in step S805 above, the anonymous page compression thread can also directly execute step S807 to determine whether to continue traversing memcg. That is, step S806 above is an optional step.
[0190] S808. Determine the compressible size of the anonymous page.
[0191] In some embodiments, after traversing memcg, the anonymous page compression thread can determine that there is a memcg with compressible anonymous pages (or an infrequently used memcg), and then determine the compressible amount of anonymous pages in this part of memcg.
[0192] Optionally, during the process of traversing memcg, the anonymous page compression thread can determine the number of anonymous pages in memcg that contain compressible anonymous pages that need to be compressed, based on the target compression amount determined in step S805 above. This number is then used as the compressible amount of anonymous pages, and compression is performed in the following steps to release memory.
[0193] S809. Should we continue traversing the Least Recently Used (lru) list? If yes, proceed to step S810; otherwise, return to step S807.
[0194] In some embodiments, each memcg manages its own LRU linked list. After determining the compressible amount of anonymous pages, the anonymous page compression thread can traverse the LRU linked list managed by the memcgs determined in step S808 (memcgs with compressible anonymous pages or infrequently used memcgs) to perform memory reclamation. Similarly, as described in step S806 above, the memory usage in the kernel changes dynamically, and the anonymous page compression thread can determine whether to continue traversing the LRU linked list based on the current actual memory situation of the kernel. If it is determined to continue traversing, step S810 below can be executed to perform memory reclamation. If it is determined not to continue traversing, the process can return to step S807 above.
[0195] It should be noted that after the anonymous page compression thread determines that it will not continue traversing the LRU linked list, it can also return to execute the above steps S806, S805, or S802.
[0196] S810, Recycle the Least Recently Used (lru) linked list.
[0197] S811, Compress and release anonymous pages.
[0198] In some embodiments, in steps S810 and S811, the anonymous page compression thread reclaims the determined LRU linked list in memcg, compresses and releases the anonymous pages therein to free up memory. Optionally, the anonymous page compression thread can compress frequently used anonymous pages and directly release infrequently used anonymous pages. After compressing the anonymous pages, the anonymous page compression thread obtains compressed anonymous pages, which can then be stored in ZRAM. ZRAM is a region in memory allocated for storing compressed anonymous pages.
[0199] In some embodiments, the anonymous page compression thread cyclically executes steps S809-S811 above to complete the recycling of LRUs in each determined memcg, thereby ensuring that the number of compressed and released anonymous pages meets the target compression amount requirement.
[0200] In some embodiments, after the anonymous page compression thread completes the compression and release of anonymous pages, it can return to step S802 above to re-determine whether the amount of anonymous pages in memory meets the watermark. If the amount of anonymous pages does not meet the watermark, the anonymous page compression thread can enter a sleep state and wait for the next wake-up. If the amount of anonymous pages meets the watermark, the anonymous page compression thread needs to continue executing the above anonymous page reclamation process.
[0201] In this way, a separate anonymous page compression thread is set up for recycling anonymous pages, thereby avoiding the impact of file page recycling on anonymous page recycling and improving the efficiency of anonymous page recycling.
[0202] For example, Figure 10This is a flowchart illustrating the memory management method for the page swapping thread provided in an embodiment of this application. Figure 10 As shown, this method is applied to electronic devices and includes the following steps.
[0203] S1001. Initialize the page swapping thread.
[0204] In some embodiments, the page swapping thread is initialized during kernel initialization. Afterwards, the page swapping thread enters a sleep state. Upon receiving a wake-up instruction from the system (as described above)... Figure 7 After step S702 shown, the page swapping thread ends its sleep and begins executing the memory management process.
[0205] S1002. Does the compressed anonymous page size meet the waterline? If it does, proceed to step S1004; otherwise, proceed to step S1003.
[0206] In some embodiments, a waterline control mechanism is set in the kernel so that page reclamation can be triggered when system memory is insufficient.
[0207] For example, such as Figure 11 As shown, a watermark is set corresponding to the size of compressed anonymous pages in memory (e.g., in MB), including a high watermark (the fourth watermark value), a low watermark (the fifth watermark value), and a min watermark (the sixth watermark value). This watermark is used to measure the amount of compressed anonymous pages in memory. Optionally, compressed anonymous pages are stored in ZRAM, so the compressed anonymous page watermark is the watermark corresponding to the compressed anonymous pages in ZRAM. The page swapping thread determines whether the size of compressed anonymous pages in ZRAM meets the watermark.
[0208] Optionally, electronic devices may write temporarily unused data from memory to disk (e.g., compress this data before sending it from memory to disk), thus freeing up memory for other uses. This process is called data write-to-disk. An increase in the amount of data in memory that can be written to disk indicates an increase in temporarily unused anonymous pages in memory (e.g., an increase in the amount of compressed anonymous pages). If the amount of compressed anonymous pages exceeds the high watermark (i.e., the fourth watermark value), it indicates excessive compressed anonymous page usage. As mentioned above... Figure 7 As shown in step S702, the page swapping thread can send compressed anonymous pages stored in memory to the disk through the I / O interface, thereby swapping out some compressed anonymous pages and reducing memory storage pressure.
[0209] Optionally, after the page swapping thread is awakened and exits the sleep state, it determines whether the number of compressed anonymous pages in memory (e.g., in MB) is greater than or equal to the value corresponding to the high watermark. If the number of compressed anonymous pages in memory is greater than or equal to the value corresponding to the high watermark, it can be determined that the compressed anonymous page usage is too high, and compressed anonymous page reclamation is required to release free memory (i.e., execute step S1004). If the number of compressed anonymous pages in memory is less than the value corresponding to the high watermark, it can be determined that the compressed anonymous page usage has not yet exceeded the requirement, and compressed anonymous page reclamation may not be triggered (i.e., execute step 1003).
[0210] It should be noted that the waterline setting rules corresponding to the compressed anonymous page size can be determined based on the capabilities of the electronic device, memory size, experimental data, experience values, etc. The setting method can refer to existing technologies, such as the waterline setting method of kswapd. This application embodiment does not limit this.
[0211] Furthermore, the term "compressed anonymous page reclamation" in the following text refers to the page swapping thread swapping compressed anonymous pages out of memory to disk, which will not be explained further below.
[0212] S1003, Page swapping thread sleeps.
[0213] In some embodiments, if the page swapping thread determines that the current number of compressed anonymous pages in memory does not meet the watermark (i.e., the number of compressed anonymous pages is less than the value corresponding to the high watermark), it can re-enter the sleep state and wait to be woken up next time.
[0214] S1004. Initialize recycling parameters.
[0215] In some embodiments, the page swapping thread determines that the current number of compressed anonymous pages in memory meets the watermark (i.e., the number of compressed anonymous pages in memory is greater than or equal to the value corresponding to the high watermark), and can then begin compressed anonymous page reclamation. Before compressing anonymous pages reclamation, the reclamation parameters used in the process need to be initialized to facilitate subsequent compressed anonymous page reclamation.
[0216] S1005. Determine the target swap amount based on the compressed anonymous page result value.
[0217] In some embodiments, compressed anonymous pages are swapped out during memory reclamation after the previous wake-up (e.g., swapped from memory to disk). However, during the period from when the page swapping thread enters a sleep state after the end of the previous memory reclamation to when the page swapping thread is awakened this time, some compressed anonymous pages are swapped back in (e.g., swapped from disk to memory). The number of compressed anonymous pages that are swapped out and then swapped back in is the refault value. The page swapping thread can determine the target compression amount for this compressed anonymous page compression based on the refault value.
[0218] For example, if the refault value is greater than a preset threshold (i.e., the second threshold), it can be determined that the number of compressed anonymous pages swapped out in the previous page swapping thread was too high, and the number of compressed anonymous pages swapped out needs to be reduced this time. Therefore, during the memory reclamation process performed by the page swapping thread this time, it can be determined that the amount of compressed anonymous pages occupied should be swapped out to a location where... Figure 11 The value corresponding to the low watermark shown is the difference between the number of compressed anonymous pages determined in step S1002 above and the value corresponding to the low watermark (e.g., the target swap amount is the difference between the determined number of compressed anonymous pages and the fifth watermark value).
[0219] For example, if the refault value is less than or equal to a preset threshold (i.e., the second threshold), it can be determined that the number of compressed anonymous pages swapped out in the previous page swapping thread was too small, and the number of compressed anonymous pages swapped out this time needs to be increased. Therefore, during the memory reclamation process performed by the page swapping thread this time, it can be determined that the amount of compressed anonymous pages occupied will be compressed to a certain level. Figure 11 The value corresponding to the min waterline shown is the target swap amount, which is the difference between the number of compressed anonymous pages determined in step S1002 above and the value corresponding to the min waterline (e.g., the target swap amount is the difference between the determined number of compressed anonymous pages and the sixth waterline value).
[0220] Among them, such as Figure 11 As shown, the value corresponding to the low watermark is greater than the value corresponding to the min watermark (that is, the three watermark values corresponding to the compressed anonymous page are the fourth watermark value, the fifth watermark value, and the sixth watermark value in descending order).
[0221] For example, after the previous page swapping thread was awakened, 10 compressed anonymous pages were swapped out. After the page swapping thread went to sleep, 5 of these 10 swapped-out compressed anonymous pages were swapped back in (e.g., the electronic device responds to a process request, swaps in these 5 compressed anonymous pages, decompresses them, and allocates them to the requesting process). Therefore, after the page swapping thread is awakened this time, it can determine that the refault value of the compressed anonymous pages is 5. If the preset threshold is 4, the page swapping thread determines that the refault value is greater than the preset threshold. Therefore, the page swapping thread can determine that the target swapping amount is the difference between the number of compressed anonymous pages determined in step S1002 above and the value corresponding to the low watermark.
[0222] S1006. Should we continue traversing the memory management group (memcg)? If yes, proceed to step S1007; otherwise, return to step S1002 above.
[0223] In some embodiments, memory usage in the kernel is dynamic, such as memory being released after other processes terminate, reducing memory usage. Therefore, after determining the target swap-out amount, the page swapping thread can determine the change in the number of compressed anonymous pages in memory. If the number of compressed anonymous pages decreases, and the decrease is greater than or equal to the target swap-out amount, then the page swapping thread can determine that it is not necessary to continue reclaiming memory (e.g., it is not necessary to continue traversing memcg), and can return to step S1002 above to re-determine whether the compressed anonymous page level meets the waterline requirement. Alternatively, if the number of compressed anonymous pages does not decrease, or the decrease is less than the target swap-out amount, then the page swapping thread can determine to continue traversing memcg to determine the number of anonymous pages that can be swapped out (i.e., execute step S1007).
[0224] It should be noted that after the page swapping thread determines that it will not continue to traverse memcg, it can also return to the previous step S1005 and redetermine the target swap amount.
[0225] S1007. Determine the amount of compressed anonymous pages that can be swapped out.
[0226] S1008, Replace compressed anonymous pages.
[0227] In some embodiments, in steps S1007 and S1008, the page swapping thread can determine the amount of compressed anonymous pages that can be swapped out during the process of traversing memcg based on the target swap-out amount, and then swap out this part of the compressed anonymous pages to achieve memory release.
[0228] In some embodiments, the page swapping thread executes steps S1006-S1008 repeatedly to ensure that the number of compressed anonymous pages swapped out meets the target swapping amount requirement.
[0229] In some embodiments, after the page swapping thread completes the swapping out of compressed anonymous pages, it can return to step S1002 above to re-determine whether the amount of compressed anonymous pages in memory meets the watermark. If the amount of compressed anonymous pages does not meet the watermark, the page swapping thread can enter a sleep state and wait for the next wake-up. If the amount of compressed anonymous pages meets the watermark, the page swapping thread needs to continue executing the above compressed anonymous page reclamation process.
[0230] In this way, a separate page swapping thread is set up for recycling compressed anonymous pages, thereby avoiding the impact of file page recycling on compressed anonymous page recycling and improving the efficiency of compressed anonymous page recycling.
[0231] For example, Figure 12 This is a flowchart illustrating the memory management method of the file page reclamation thread provided in an embodiment of this application. Figure 12 As shown, this method is applied to electronic devices and includes the following steps.
[0232] S1201. Initialize the file page reclamation thread.
[0233] In some embodiments, the file page reclamation thread is initialized during kernel initialization. Afterwards, the file page reclamation thread enters a sleep state. Upon receiving a wake-up instruction from the system (as described above)... Figure 7 After step S705 or step 707 shown, the file page reclamation thread ends its sleep state and begins to execute the memory management process.
[0234] S1202. Determine whether the storage zone meets the waterline. If it does, proceed to step S1204; otherwise, proceed to step S1203.
[0235] In some embodiments, a watermark corresponding to a file page is pre-set for each zone based on the different functions of different storage regions (zones) in memory. The watermarks for file pages in different zones may be the same or different. If any zone runs out of memory, a file page reclamation thread can be triggered to reclaim file pages.
[0236] For example, such as Figure 13 As shown, watermarks are set for each zone, including a high watermark (the eighth watermark value), a low watermark (the seventh watermark value), and a minimum watermark. These watermarks measure the amount of free memory in a zone. As applications request memory, the amount of free memory in a zone decreases. If the amount of free memory reaches the low watermark, it indicates that the amount of free memory is low, and the file page reclamation thread needs to perform file page reclamation. If the amount of free memory reaches the minimum watermark, it indicates that the amount of free memory is too low, and the system needs to directly reclaim memory.
[0237] Optionally, after the file page reclamation thread is awakened and exits the sleep state, it determines whether the amount of free memory (e.g., in MB) in each zone is less than or equal to the value corresponding to the low watermark (i.e., whether it is less than or equal to the seventh watermark value). If the amount of free memory in a zone is less than or equal to the value corresponding to the low watermark, it can be determined that the amount of free memory is small, and file page reclamation is required to release the free memory (i.e., execute step S1204). If the amount of free memory in a zone is greater than the value corresponding to the low watermark, it can be determined that the amount of free memory meets the usage requirements, and file page reclamation does not need to be triggered (i.e., execute step S1203).
[0238] It should be noted that the waterline setting rules for a zone can be determined based on the capabilities of the electronic device, the zone function, the zone size, experimental data, empirical values, etc. The setting method can refer to existing technologies, such as the waterline setting method of kswapd. This application embodiment does not limit this.
[0239] S1203, the file recycling thread is in sleep mode.
[0240] In some embodiments, if the file recycling thread determines that all zones in the current memory do not meet the waterline (i.e., the amount of free memory is greater than the value corresponding to the low waterline), it can re-enter the sleep state and wait to be woken up next time.
[0241] S1204. Initialize recycling parameters.
[0242] In some embodiments, the file reclamation thread determines that there are zones in the current memory that meet the watermark (i.e., the amount of free memory is less than or equal to the value corresponding to the low watermark), and can then begin file page reclamation. Before file page reclamation, the reclamation parameters used in the file page reclamation process need to be initialized to facilitate subsequent file page reclamation.
[0243] S1205, Calculate the target recovery amount.
[0244] In some embodiments, the file reclamation thread determines the difference between the amount of free memory and the high watermark value based on the amount of free memory in the zone determined in step S1202 above, and sets this difference as the target reclamation amount (i.e., the target reclamation amount is the difference between the amount of free memory and the eighth watermark value). This ensures that after memory reclamation, the amount of free memory meets the high watermark requirement, thereby satisfying the application's page caching needs.
[0245] S1206. Should memory reclamation continue? If it is determined to continue reclamation, proceed to step S1207; if it is determined not to continue reclamation, return to step S1205 above.
[0246] In some embodiments, memory usage in the kernel is dynamic, such as when other processes terminate and memory is released, reducing memory usage. Therefore, after determining the target reclamation amount, the file page reclamation thread can determine the changes in the amount of free memory in each zone of the current memory. If the amount of free memory increases, and the increase is greater than or equal to the target reclamation amount, then the file page reclamation thread can determine that it is not necessary to continue reclamation of memory and can re-determine the target reclamation amount (i.e., execute step S1205). Alternatively, if the amount of free memory does not increase, or the increase is less than the target reclamation amount, then the file page reclamation thread can determine to continue reclamation of memory (i.e., execute step S1207).
[0247] It should be noted that after determining that it will no longer reclaim memory, the file page reclamation thread can directly return to step S1202 above to reassess whether each zone meets the waterline requirements. If it is determined that the requirements are not met, it can enter a sleep state and end this memory management process.
[0248] S1207. Should we continue traversing the memory management group (memcg)? If yes, proceed to step S1208; otherwise, return to step S1206 above.
[0249] In some embodiments, memcg is a memory subsystem used to manage processes or applications with different memory requirements. memcg maintains an LRU linked list for each storage zone in memory, enabling resource utilization and isolation. As described in step S1206 above, the memory usage in the kernel changes dynamically. After determining to continue reclaiming memory, the file page reclamation thread can also determine whether to continue traversing memcg (e.g., the memcg corresponding to a zone that meets the watermark). If it is determined to continue traversing memcg, it can determine whether there are reclaimable file pages in memcg (i.e., execute step S1208 below). If it is determined not to continue traversing memcg, it can return to determining whether to continue reclaiming memory (i.e., execute step S1206 above).
[0250] It should be noted that after the file page reclamation thread determines that it will no longer continue traversing memcg, it can directly return to step S1205 or step S1202.
[0251] Furthermore, after determining the target amount to be reclaimed in step S1205 above, the file page reclamation thread can directly execute step S1207 to determine whether to continue traversing memcg. That is, step S1206 above is an optional step.
[0252] S1208. Determine the target number of file pages to be recycled.
[0253] In some embodiments, after traversing the memcg, the file page reclamation thread can determine whether there are reclaimable file pages in the memcg corresponding to the zone that meets the watermark. Based on the reclaimable file pages and the target reclamation amount determined in step S1205 above, the target reclamation amount of file pages for these zones is determined.
[0254] Optionally, there is a mapping relationship between file pages and data on the disk. When some file pages are reclaimed, the corresponding mapping relationship is released. Subsequently, if this data is needed, the mapping relationship needs to be re-established. This process may affect process execution, thus impacting the user experience. Therefore, according to preset rules, a portion of the target reclamation amount determined in step S1205 can be used as the target reclamation amount for this file page reclamation process to reduce the impact of file page reclamation on user experience. Furthermore, since memory in the kernel changes dynamically, some processes may have already released memory during the file page reclamation thread's memory reclamation process; therefore, reducing the target reclamation amount for file pages can still meet memory reclamation requirements.
[0255] For example, in step S1205 above, the target amount to be reclaimed by the file page reclamation thread is 100M. According to the preset rules, the file page reclamation thread can determine that the target amount of file pages to be reclaimed is 10M (that is, one-tenth of the original target amount to be reclaimed).
[0256] S1209. Should we continue traversing the Least Recently Used (lru) list? If yes, proceed to step S1210; otherwise, return to step S1207.
[0257] S1210, Recycle the least recently used list.
[0258] S1211, Write back and release file pages.
[0259] In some embodiments, during steps S1209-S1211, if the file page reclamation thread determines that it needs to traverse the LRU list, it can reclaim the LRU list and perform file page write-back and release. If the file page reclamation thread determines that it does not need to traverse the LRU list, it can return to step S1207. Optionally, as described above... Figure 7 As shown in step S705, the file page reclamation thread will first write back the file pages that need to be written back (such as dirty pages) and then release them, while directly releasing the file pages that do not need to be written back.
[0260] In some embodiments, the file page reclamation thread cyclically executes the above steps S1209-S1211 to complete the reclamation of LRUs in each determined memcg, so as to achieve the requirement that the number of anonymous pages written back and released meets the target reclamation amount.
[0261] In some embodiments, after the file page reclamation thread completes the write-back and release of file pages, it can return to step S1202 above to re-determine whether each zone in memory meets the watermark. If none of the zones meet the watermark, the file page reclamation thread can enter a sleep state and wait for the next wake-up. If any zone meets the watermark, the file page reclamation thread needs to continue executing the above file page reclamation process.
[0262] In this way, a separate file page reclamation thread is set up to reclaim file pages, thereby avoiding the impact of anonymous page reclamation on file page reclamation and improving file page reclamation efficiency.
[0263] Based on the above description of the anonymous page compression thread, page swapping thread, and file page reclamation thread, electronic devices can achieve on-demand and efficient management of different page types through three asynchronous threads. This effectively reduces memory pressure and improves system performance.
[0264] In some scenarios, electronic devices can also manage different types of pages through anonymous page compression threads and file page recycling threads. As mentioned above... Figure 7In the method shown, the electronic device attempts to wake up the anonymous page compression thread and the file reclamation thread to perform memory reclamation. That is, the electronic device may or may not have a page swapping thread configured.
[0265] For example, Figure 14 This is a schematic flowchart illustrating a memory management method provided in an embodiment of this application. Figure 14 As shown, the method includes the following steps.
[0266] S1401. After the electronic device detects a memory request, it wakes up the anonymous page compaction thread, and / or the page swapping thread, and / or the file page reclamation thread.
[0267] In some embodiments, an anonymous page compression thread is used to manage anonymous pages in memory, a page swapping thread is used to manage compressed anonymous pages in memory, and a file page reclamation thread is used to manage file pages in memory.
[0268] For example, the anonymous page compression thread is used to compress anonymous pages in memory, obtain compressed anonymous pages, and store them in ZRAM; or, to release infrequently used anonymous pages. The page swapping thread is used to swap compressed anonymous pages from memory (such as ZRAM) to disk. The file page reclamation thread is used to write back file pages that need to be reclaimed (such as dirty pages) before releasing them, and to directly release file pages that do not need to be written back.
[0269] In some embodiments, after detecting a memory request, the electronic device wakes up the anonymous page compaction thread, and / or the page swapping thread, and / or the file page reclamation thread.
[0270] For example, such as Figure 7 As shown, the electronic device wakes up the anonymous page compression thread and the page swapping thread in step S702, and wakes up the file page recycling thread in steps S705 and S707.
[0271] In this way, memory management is performed by waking up at least one of the three pre-configured memory management threads. This reduces memory usage and allows memory to be allocated to applications that request it.
[0272] In some embodiments, after detecting a memory allocation request, the electronic device first wakes up the anonymous page compaction thread and / or the page swapping thread to perform memory reclamation. Then, it determines whether a page can be obtained from the free list, i.e., whether free memory can be acquired. If it is determined that free memory cannot be acquired, the file page reclamation thread is then woken up to perform memory reclamation.
[0273] For example, as described above Figure 7The flowchart illustrates that after receiving a memory request (as shown in step S701), the electronic device first attempts to wake up the anonymous page compaction thread and the page swapping thread (as shown in step S702). Then, if it is determined that free memory cannot be acquired (as shown in step S703), it attempts to wake up the file page reclamation thread (as shown in step S705).
[0274] Thus, by waking up the anonymous page compaction thread and the page swapping thread, the number of times the memory management thread is woken up is increased, thereby increasing the amount of free memory. Furthermore, since the anonymous page compaction thread and the page swapping thread perform anonymous page reclamation and compaction without causing corresponding anonymous page data loss, these two threads can be woken up first to achieve memory reclamation.
[0275] In some embodiments, if the electronic device fails to acquire free memory after waking up the anonymous page compression thread and the page swapping thread, it may enter a slow path to further attempt to reclaim memory. For example, if the file page reclamation thread is woken up a preset number of times and still fails to acquire free memory, memory occupied by infrequently used data in memory may be released according to preset rules.
[0276] For example, file pages are mapped and released relatively quickly. Therefore, during slow-path memory management, electronic devices can attempt to repeatedly wake up the file page reclamation thread to increase memory reclamation efficiency. For instance, in the above... Figure 7 In steps S705 and S707, the electronic device repeatedly attempts to wake up the file page reclamation thread twice to release file page memory and thus obtain more free memory. Afterwards, if the electronic device fails to obtain free memory after attempting to wake up the file page reclamation thread a preset number of times, memory reclamation can be performed directly (as in step S709). This direct memory reclamation can be achieved by releasing memory occupied by infrequently used data.
[0277] S1402, The electronic device asynchronously runs at least two of the following threads: an anonymous page compression thread, a page swapping thread, and a file page reclamation thread, to acquire free memory.
[0278] In some embodiments, after waking up the anonymous page compression thread, and / or the page swapping thread, and / or the file page reclamation thread, the electronic device asynchronously runs at least two of the anonymous page compression thread, the page swapping thread, and the file page reclamation thread to perform memory reclamation according to different page types in order to obtain free memory.
[0279] For example, the electronic device follows the above Figure 8 , Figure 10 ,as well as Figure 12 The method shown asynchronously runs at least two of the three memory management threads to perform memory management and acquire free memory.
[0280] For example, an electronic device asynchronously runs an anonymous page compression thread to reclaim memory. Specifically, the anonymous page compression thread determines whether the anonymous page size meets a first condition, which includes the anonymous page size being greater than or equal to a first watermark value. If the anonymous page size meets the first condition, the anonymous page compression thread determines the target compression amount based on the anonymous page refault value and reclaims anonymous pages according to the target compression amount. The anonymous page refault value includes the number of anonymous pages that were decompressed after the previous anonymous page compression thread was awakened. If the anonymous page size does not meet the first condition, the anonymous page compression thread enters a sleep state.
[0281] Optional, as described above Figure 8 The flowchart illustrates that after the anonymous page compression thread is awakened and exits its sleep state, it determines whether the number of anonymous pages in memory (e.g., in MB) is greater than or equal to the value corresponding to the high watermark (i.e., the first watermark value). If the number of anonymous pages in memory is greater than or equal to the value corresponding to the high watermark, it can be determined that the anonymous page usage is excessive (i.e., the first condition is met), and anonymous page reclamation is required to release free memory. If the number of anonymous pages in memory is less than the value corresponding to the high watermark (i.e., the first condition is not met), it can be determined that the anonymous page usage has not yet exceeded the requirement, and anonymous page reclamation does not need to be triggered.
[0282] For example, if the anonymous page compression thread determines that the anonymous page refault value is greater than the first threshold, it can be concluded that the previous anonymous page compression thread compressed too many anonymous pages, and the number of anonymous pages to be compressed needs to be reduced this time. Therefore, the target compression amount is determined to be the difference between the anonymous page size and the second watermark value, where the second watermark value is less than the first watermark value (i.e., the anonymous page occupancy is determined to be compressed to a value such that...). Figure 9 (The value corresponding to the low watermark shown). Alternatively, if the anonymous page compression thread determines that the anonymous page refault value is less than or equal to the first threshold, it indicates that the previous anonymous page compression thread compressed too few anonymous pages, and the number of anonymous pages to be compressed needs to be increased this time. Therefore, the target compression amount is determined to be the difference between the anonymous page size and the third watermark value, where the third watermark value is less than the second watermark value (i.e., the anonymous page occupancy is determined to be compressed to the following value). Figure 9 The value corresponding to the min waterline shown.
[0283] For example, electronic devices may asynchronously run page swapping threads to perform memory reclamation. Specifically, the page swapping thread determines whether the size of the compressed anonymous pages meets a second condition, which includes the compressed anonymous page size being greater than or equal to the fourth watermark value. If the compressed anonymous page size meets the second condition, the page swapping thread determines the target swap-out amount based on the refault value of the compressed anonymous pages and reclaims the compressed anonymous pages according to the target swap-out amount. The compressed anonymous page refault value includes the number of compressed anonymous pages swapped back after the previous page swapping thread was awakened. If the compressed anonymous page size does not meet the second condition, the page swapping thread enters a sleep state.
[0284] Optional, as described above Figure 10 The flowchart illustrates that after the page swapping thread is awakened and exits its sleep state, it determines whether the number of compressed anonymous pages in memory (e.g., in MB) is greater than or equal to the value corresponding to the high watermark (i.e., the fourth watermark value). If the number of compressed anonymous pages in memory is greater than or equal to the value corresponding to the high watermark (i.e., the second condition is met), it can be determined that the number of compressed anonymous pages in memory is excessive, and compressed anonymous page reclamation is required to release free memory. If the number of compressed anonymous pages in memory is less than the value corresponding to the high watermark (i.e., the second condition is not met), it can be determined that the number of compressed anonymous pages in memory is temporarily within the required limit, and compressed anonymous page reclamation does not need to be triggered.
[0285] For example, if the page swapping thread determines that the refault value of a compressed anonymous page is greater than the second threshold, it can be determined that the previous page swapping thread swapped out too many compressed anonymous pages, and the number of compressed anonymous pages swapped out needs to be reduced this time. Therefore, the target swapping amount is determined to be the difference between the compressed anonymous page level and the fifth watermark value (i.e., determining the amount of compressed anonymous pages occupied to be swapped out to a value that is less than the threshold value). Figure 11 The value corresponding to the low watermark shown is shown in the diagram. The fifth watermark value is less than the fourth watermark value. Alternatively, the page swapping thread determines that the refault value of the compressed anonymous page is less than or equal to the second threshold, and determines the target swapping amount as the difference between the compressed anonymous page size and the sixth watermark value (i.e., determines to compress the compressed anonymous page usage to such a level). Figure 11 The value corresponding to the min waterline shown is less than the value of the fifth waterline.
[0286] For example, electronic devices asynchronously run a file page reclamation thread to perform memory reclamation. Specifically, the file page reclamation thread determines whether a storage area in memory meets a third condition, which includes the amount of free memory in the storage area being less than or equal to the seventh watermark value. If the storage area in memory meets the third condition, the file page reclamation thread determines the difference between the amount of free memory in the storage area and the eighth watermark value as the target reclamation amount, and reclaims file pages according to the target reclamation amount. The eighth watermark value is greater than the seventh watermark value. If the storage area in memory does not meet the third condition, the file page reclamation thread enters a sleep state.
[0287] Optionally, the seventh watermark values may be the same or different for different storage zones in memory.
[0288] Optional, as described above Figure 12 The flowchart illustrates that after the file page reclamation thread is awakened and exits its sleep state, it determines whether the amount of free memory (e.g., in MB) in each zone is less than or equal to the value corresponding to the low watermark (i.e., whether the third condition is met). If the amount of free memory in a zone is less than or equal to the value corresponding to the low watermark (i.e., the third condition is met), it can be determined that the amount of free memory is low, and file page reclamation is required to release the free memory. If the amount of free memory in a zone is greater than the value corresponding to the low watermark (i.e., the third condition is not met), it can be determined that the amount of free memory meets the usage requirements, and file page reclamation does not need to be triggered.
[0289] In this way, electronic devices can achieve on-demand and efficient management of different page types by asynchronously running at least two of the three memory management threads. This effectively reduces memory pressure and improves system performance.
[0290] The above combination Figures 7-14 The memory management method provided in the embodiments of this application is described in detail below. Figure 15 This application provides a detailed description of the electronic device provided in its embodiments.
[0291] In one possible design, Figure 15 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. Figure 15 As shown, the electronic device 1500 may include a transceiver unit 1501 and a processing unit 1502. The electronic device 1500 can be used to implement the functions of the electronic device involved in the above method embodiments.
[0292] Optionally, the transceiver unit 1501 is used to support the electronic device 1500 in performing... Figure 7 S701 in; and / or, for supporting electronic device 1500 to perform Figure 14 S1401 and S1402 in the example.
[0293] Optionally, the processing unit 1502 is used to support the electronic device 1500 in performing operations. Figure 7 S702-S712; and / or, for supporting electronic device 1500 to perform Figure 8 S801-S811; and / or, for supporting electronic device 1500 to perform Figure 10 S1001-S1008; and / or, for supporting electronic device 1500 to perform Figure 12 S1201-S1211; and / or, for supporting electronic device 1500 to perform Figure 14 S1402 in the middle.
[0294] The transceiver unit may include a receiving unit and a transmitting unit, and may be implemented by a transceiver or transceiver-related circuit components, and may be a transceiver or transceiver module. The operation and / or function of each unit in the electronic device 1500 are respectively for implementing the corresponding process of the memory management method described in the above method embodiments. All relevant content of each step involved in the above method embodiments can be referred to the functional description of the corresponding functional unit, and for the sake of brevity, it will not be repeated here.
[0295] Optionally, Figure 15 The illustrated electronic device 1500 may also include a storage unit ( Figure 15 (not shown in the image), this storage unit stores a program or instruction. When the transceiver unit 1501 and the processing unit 1502 execute the program or instruction, it causes... Figure 15 The electronic device 1500 shown can execute the memory management method described in the above method embodiments.
[0296] Figure 15 The technical effects of the electronic device 1500 shown can be referred to the technical effects of the memory management method described in the above method embodiments, and will not be repeated here.
[0297] In addition to being in the form of electronic device 1500, the technical solution provided in this application can also be a functional unit or chip in an electronic device, or a device used in conjunction with an electronic device.
[0298] This application also provides a chip system, including: a processor coupled to a memory, the memory being used to store programs or instructions, which, when executed by the processor, cause the chip system to implement the methods in any of the above method embodiments.
[0299] Optionally, the chip system may contain one or more processors. These processors can be implemented in hardware or software. When implemented in hardware, the processor can be a logic circuit, an integrated circuit, etc. When implemented in software, the processor can be a general-purpose processor, implemented by reading software code stored in memory.
[0300] Optionally, the chip system may contain one or more memories. The memory may be integrated with the processor or disposed separately from it; this application embodiment does not limit this. For example, the memory may be a non-transient processor, such as a read-only memory (ROM), which may be integrated with the processor on the same chip or disposed separately on different chips. This application embodiment does not specifically limit the type of memory or the arrangement of the memory and processor.
[0301] For example, the chip system may be a field programmable gate array (FPGA), an application specific integrated circuit (ASIC), a system on chip (SoC), a central processor (CPU), a network processor (NP), a digital signal processor (DSP), a micro controller unit (MCU), a programmable logic device (PLD), or other integrated chips.
[0302] It should be understood that each step in the above method embodiments can be completed by integrated logic circuits in the processor hardware or by instructions in software form. The method steps disclosed in the embodiments of this application can be directly manifested as being executed by a hardware processor, or being executed by a combination of hardware and software modules in the processor.
[0303] This application also provides a computer-readable storage medium storing a computer program. When the computer program is run on a computer, it causes the computer to perform the aforementioned steps to implement the memory management method described in the above embodiments.
[0304] This application also provides a computer program product that, when run on a computer, causes the computer to perform the aforementioned steps to implement the memory management method described in the above embodiments.
[0305] In addition, this application also provides an apparatus. Specifically, the apparatus may be a component or module, and may include one or more processors and a memory connected together. The memory is used to store a computer program. When the computer program is executed by one or more processors, the apparatus performs the memory management methods described in the above method embodiments.
[0306] The apparatus, computer-readable storage medium, computer program product, or chip provided in the embodiments of this application are all used to execute the corresponding methods provided above. Therefore, the beneficial effects they can achieve can be referred to the beneficial effects of the corresponding methods provided above, and will not be repeated here.
[0307] The steps of the methods or algorithms described in conjunction with the embodiments of this application can be implemented in hardware or by a processor executing software instructions. The software instructions can consist of corresponding software modules, which can be stored in random access memory (RAM), flash memory, read-only memory (ROM), erasable programmable read-only memory (EPROM), electrically erasable programmable read-only memory (EEPROM), registers, hard disks, portable hard disks, CD-ROMs, or any other form of storage medium well known in the art. An exemplary storage medium is coupled to a processor, enabling the processor to read information from and write information to the storage medium. Of course, the storage medium can also be a component of the processor. The processor and the storage medium can reside in an application-specific integrated circuit (ASIC).
[0308] Through the above description of the embodiments, those skilled in the art will clearly understand that, for the sake of convenience and brevity, the division of the above functional modules is only used as an example. In practical applications, the above functions can be assigned to different functional modules as needed; that is, the internal structure of the device can be divided into different functional modules to complete all or part of the functions described above. The specific working process of the system, device, and unit described above can be referred to the corresponding process in the foregoing method embodiments, and will not be repeated here.
[0309] In the several embodiments provided in this application, it should be understood that the disclosed methods can be implemented in other ways. The device embodiments described above are merely illustrative. For example, the division of modules or units is only a logical functional division, and there may be other division methods in actual implementation; for example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. In addition, the mutual coupling or direct coupling or communication connection shown or discussed may be through some interfaces, and the indirect coupling or communication connection of modules or units may be electrical, mechanical or other forms.
[0310] Furthermore, the functional units in the various embodiments of this application can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit.
[0311] Computer-readable storage media include, but are not limited to, any of the following: USB flash drive, portable hard drive, read-only memory (ROM), random access memory (RAM), magnetic disk or optical disk, and other media capable of storing program code.
[0312] The above description is merely a specific embodiment of this application, but the scope of protection of this application is not limited thereto. Any changes or substitutions within the technical scope disclosed in this application should be included within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.
Claims
1. A memory management method, characterized in that, Applied to electronic devices, the method includes: Upon detecting a memory allocation request, the anonymous page compression thread, and / or the page swapping thread, and / or the file page reclamation thread are activated; the anonymous page compression thread is used to manage anonymous pages in memory, the page swapping thread is used to manage compressed anonymous pages in memory, and the file page reclamation thread is used to manage file pages in memory. The anonymous page compression thread, the page swapping thread, and the file page recycling thread each independently determine whether to perform a recycling operation based on the magnitude watermark value of the corresponding page. At least two of the anonymous page compression thread, the page swapping thread, and the file page recycling thread are run asynchronously to obtain free memory.
2. The method according to claim 1, characterized in that, The asynchronous execution of at least two of the anonymous page compression thread, the page swapping thread, and the file page reclamation thread to acquire free memory includes: The anonymous page compression thread determines whether the anonymous page size meets a first condition, the first condition including that the anonymous page size is greater than or equal to a first watermark value; If the anonymous page size meets the first condition, the anonymous page compression thread determines the target compression amount based on the anonymous page result refault value, and reclaims the anonymous pages based on the target compression amount; wherein, the anonymous page refault value includes the number of anonymous pages that were decompressed after the anonymous page compression thread was woken up in the previous time; If the anonymous page size does not meet the first condition, the anonymous page compression thread enters a sleep state.
3. The method according to claim 2, characterized in that, The anonymous page compression thread determines the target compression amount based on the anonymous page refault value, including: The anonymous page compression thread determines that the anonymous page refault value is greater than the first threshold, and determines that the target compression amount is the difference between the anonymous page size and the second waterline value, wherein the second waterline value is less than the first waterline value; The anonymous page compression thread determines that the anonymous page refault value is less than or equal to the first threshold, and determines that the target compression amount is the difference between the anonymous page magnitude and the third watermark value, wherein the third watermark value is less than the second watermark value.
4. The method according to any one of claims 1-3, characterized in that, The asynchronous execution of at least two of the anonymous page compression thread, the page swapping thread, and the file page reclamation thread to acquire free memory includes: The page swapping thread determines whether the compressed anonymous page size meets the second condition, the second condition including that the compressed anonymous page size is greater than or equal to the fourth watermark value; If the compressed anonymous page quantity satisfies the second condition, the page swapping thread determines the target swap-out quantity based on the refault value of the compressed anonymous page result, and reclaims the compressed anonymous pages based on the target swap-out quantity; wherein, the compressed anonymous page refault value includes the number of compressed anonymous pages swapped back after the previous page swapping thread was awakened; If the compressed anonymous page size does not meet the second condition, the page swapping thread enters a sleep state.
5. The method according to claim 4, characterized in that, The page swapping thread determines the target swap amount based on the refault value of the compressed anonymous page, including: The page swapping thread determines that the refault value of the compressed anonymous page is greater than the second threshold, and determines that the target swapping amount is the difference between the compressed anonymous page magnitude and the fifth waterline value, wherein the fifth waterline value is less than the fourth waterline value; The page swapping thread determines that the refault value of the compressed anonymous page is less than or equal to the second threshold, and determines that the target swapping amount is the difference between the compressed anonymous page magnitude and the sixth waterline value, wherein the sixth waterline value is less than the fifth waterline value.
6. The method according to any one of claims 1-3, characterized in that, The asynchronous execution of at least two of the anonymous page compression thread, the page swapping thread, and the file page reclamation thread to acquire free memory includes: The file page reclamation thread determines whether the storage area in memory meets the third condition, which includes that the amount of free memory in the storage area is less than or equal to the seventh waterline value; If the storage area in the memory meets the third condition, the file page reclamation thread determines the difference between the amount of free memory in the storage area and the eighth watermark value as the target reclamation amount, and reclaims file pages according to the target reclamation amount; wherein, the eighth watermark value is greater than the seventh watermark value; If the storage area in memory does not meet the third condition, the file page reclamation thread enters a sleep state.
7. The method according to claim 6, characterized in that, The seventh watermark values corresponding to different storage regions in the memory may be the same or different.
8. The method according to any one of claims 1-3, characterized in that, Upon detecting a memory request, the process of waking up the anonymous page compaction thread, and / or the page swapping thread, and / or the file page reclamation thread includes: Upon detecting the memory request, the anonymous page compression thread and / or the page swapping thread are first awakened; The file page reclamation thread is then woken up after it is determined that no free memory can be acquired.
9. The method according to claim 8, characterized in that, The method further includes: If the file page recycling thread is woken up a preset number of times and no free memory is available, the memory occupied by infrequently used data in the memory is released according to preset rules.
10. An electronic device, characterized in that, include: A processor and a memory, the memory being coupled to the processor, the memory being used to store computer program code, the computer program code including computer instructions, which, when the processor reads the computer instructions from the memory, cause the electronic device to perform the method as described in any one of claims 1-9.
11. A computer-readable storage medium, characterized in that, The computer-readable storage medium includes a computer program that, when run on an electronic device, causes the electronic device to perform the method as described in any one of claims 1-9.
12. A computer program product, characterized in that, When the computer program product is run on a computer, it causes the computer to perform the method as described in any one of claims 1-9.
13. A chip system, characterized in that, The chip system includes at least one processor and at least one interface circuit. The at least one interface circuit is used to perform transceiver functions and send instructions to the at least one processor. When the at least one processor executes the instructions, the at least one processor performs the method as described in any one of claims 1-9.
Citation Information
Patent Citations
Memory management method and apparatus
CN107885666A
Memory recovery method and device, electronic equipment and storage medium
CN111984413A