Memory recovery method, electronic device, and storage medium
By adjusting the memory waterline threshold and optimizing the CPU resource usage of the asynchronous garbage collection thread, the problem of electronic device lag in traditional memory reclamation methods has been solved, achieving more efficient memory management and lighter CPU resource usage.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- HONOR DEVICE CO LTD
- Filing Date
- 2024-12-31
- Publication Date
- 2026-06-30
AI Technical Summary
Traditional memory reclamation methods can easily cause lag issues in electronic devices, especially when asynchronous reclamation threads consume too much CPU resources.
By adjusting the memory waterline threshold and priority, the asynchronous reclamation thread is woken up in advance and controlled to run on either a small or large CPU core, thus optimizing the CPU resource usage of the asynchronous reclamation thread. Combined with the specified allocation and priority adjustment of the small and large CPU cores, the CPU resource usage of memory reclamation is reduced.
It reduces application blocking and lag caused by insufficient memory, improves memory reclamation efficiency, optimizes the performance of electronic devices, and alleviates lag issues.
Smart Images

Figure CN122309116A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of storage technology, and in particular to a memory reclamation method, electronic device, and storage medium. Background Technology
[0002] To meet the memory resource requirements of applications in electronic devices, the system performs memory reclamation when memory resources are insufficient. An asynchronous reclamation thread is a kernel thread that implements memory reclamation. This thread performs memory reclamation tasks in the background and does not directly block memory allocation requests; therefore, it is also called a background reclamation thread.
[0003] In traditional methods, an asynchronous garbage collection thread is triggered to reclaim memory when the amount of free memory falls below the low watermark. However, during the use of electronic devices, memory reclamation based on this traditional method can easily cause lag or stuttering. Summary of the Invention
[0004] This application provides a memory reclamation method, an electronic device, and a storage medium, which can reduce the problem of electronic device lag caused by memory reclamation.
[0005] To achieve the above objectives, the embodiments of this application adopt the following technical solutions:
[0006] Firstly, a memory reclamation method is provided for use in electronic devices. When an application or program in an electronic device needs to use memory resources, it initiates a memory allocation request. In response to this request, if the current amount of free memory is below the high memory threshold but above the low memory threshold (equivalent to relatively abundant memory), the electronic device can wake up an asynchronous reclamation thread. Furthermore, the electronic device can control the asynchronous reclamation thread to run on a CPU small core and / or based on a second priority, and reclaim memory during its operation; wherein the second priority is lower than a pre-set first priority for the asynchronous reclamation thread; the CPU time slice corresponding to the second priority is less than the CPU time slice corresponding to the first priority; and the CPU time slice represents the maximum duration for which the asynchronous reclamation thread can run continuously on the CPU.
[0007] It should be understood that controlling the asynchronous garbage collection thread to run on a CPU small core and / or based on the second priority is equivalent to controlling the asynchronous garbage collection thread to consume relatively fewer CPU resources during operation. In the above scheme, when memory is relatively abundant, the asynchronous garbage collection thread is triggered in advance to perform memory reclamation, which not only ensures that the memory in the electronic device is relatively sufficient, reducing application blocking and lag caused by insufficient memory, but also controls the asynchronous garbage collection thread to consume relatively fewer CPU resources when it is woken up in advance, further reducing the lag of the electronic device.
[0008] In one possible implementation, when the current amount of free memory is below the low memory threshold but above the minimum memory threshold (equivalent to significantly insufficient or tight memory), the electronic device can wake up the asynchronous memory reclamation thread. In this case, the electronic device can control the asynchronous memory reclamation thread to bind to a large CPU core and / or run based on the third priority, and reclaim memory during operation; wherein the third priority is greater than the first priority, and the CPU time slice corresponding to the third priority is greater than the CPU time slice corresponding to the first priority.
[0009] It should be understood that controlling the asynchronous memory reclamation thread to run on a large CPU core and / or based on the third priority is equivalent to controlling the asynchronous memory reclamation thread to reclaim memory faster. In the above scheme, when memory is scarce, the asynchronous memory reclamation thread can be specifically controlled to reclaim memory faster, which to some extent reduces application blocking and lag caused by untimely memory reclamation.
[0010] In one possible implementation, when the current amount of free memory is below the low memory threshold but above the minimum memory threshold, the electronic device can wake up the asynchronous garbage collection thread and increase the CPU frequency. The asynchronous garbage collection thread is then run based on the increased CPU frequency.
[0011] In the above solution, when memory is tight, the memory reclamation efficiency of the asynchronous reclamation thread can be improved by increasing the CPU frequency, which to some extent reduces the application blocking and lag caused by untimely memory reclamation.
[0012] In one possible implementation, the low memory watermark is lower than the initial low memory watermark. The initial low memory watermark is the low memory watermark built into the system's native memory reclamation mechanism. The low memory watermark in this embodiment is lower than the initial low memory watermark. By using a lower low memory watermark, the time or space for asynchronous reclamation threads to consume high CPU resources can be shortened. Under memory-constrained conditions (e.g., when the current amount of free memory is lower than the low memory watermark but higher than the minimum memory watermark), memory reclamation efficiency can be improved. Overall, this reduces the CPU resource consumption during the entire memory reclamation process to some extent.
[0013] In one possible implementation, the high memory waterline is higher than the initial high memory waterline. The initial high memory waterline is the high memory waterline inherent in the system's native memory reclamation mechanism. In this embodiment, the high memory waterline is adjusted upwards compared to the initial high memory waterline. A higher high memory waterline increases the time available for asynchronous reclamation threads to reclaim memory. When memory is relatively abundant (e.g., the current amount of free memory is below the high memory waterline but above the low memory waterline), more time is reserved for asynchronous reclamation threads, allowing them to reclaim a sufficient amount of memory more smoothly. This reduces the pressure on CPU resources from asynchronous reclamation threads while ensuring the memory reclamation requirements are met.
[0014] In one possible implementation, if the current amount of free memory falls below the minimum memory threshold, the electronic device can trigger direct memory reclamation. This addresses situations of severe memory shortages and ensures that the application's memory requirements are met as much as possible.
[0015] In one possible implementation, if the amount of free memory is increased to above the minimum memory threshold based on the direct memory reclamation, the direct memory reclamation is stopped, and the asynchronous reclamation thread is woken up and bound to a large CPU core; the asynchronous reclamation thread bound to the large CPU core continues to reclaim memory.
[0016] In the above scheme, after direct memory reclamation reaches a certain point, it stops and an asynchronous reclamation thread is awakened. This asynchronous reclamation thread is then bound to a large CPU core to continue memory reclamation. Binding the asynchronous reclamation thread to a large CPU core can also reclaim memory relatively quickly, and compared to direct memory reclamation, it can reduce task blocking.
[0017] In one possible implementation, after the asynchronous reclamation thread based on the CPU large core has reclaimed memory to the high memory threshold (i.e., relatively abundant memory), the asynchronous reclamation thread is unbound from the CPU large core and bound to the CPU small core; memory is then reclaimed based on the asynchronous reclamation thread bound to the CPU small core.
[0018] In the above scheme, after the asynchronous reclamation thread based on the CPU big core quickly reclaims memory to the high memory waterline, the asynchronous reclamation thread can be switched from being bound to the CPU big core to being bound to the CPU small core, thereby minimizing the CPU resource occupation of the asynchronous reclamation thread when memory is relatively abundant.
[0019] In one possible implementation, the asynchronous reclamation thread stops running if the amount of free memory after reclamation is greater than a first memory threshold; the first memory threshold is higher than the high memory threshold.
[0020] In the above solution, by adding a first memory waterline, memory reclamation is triggered to stop, thus avoiding unnecessary memory reclamation processes that would consume CPU resources.
[0021] Secondly, this application provides an electronic device comprising at least a memory and one or more processors. The memory stores computer instructions, which, when executed by the one or more processors, cause the electronic device to perform the methods described in any of the first aspects above.
[0022] Thirdly, this application provides a chip system applied to an electronic device, the chip system including one or more processors, the processors being used to invoke computer instructions to cause the electronic device to perform any of the methods described in the first aspect above.
[0023] Fourthly, this application provides a computer-readable storage medium including computer instructions that, when executed on an electronic device, cause the electronic device to perform the method described in any of the first aspects above.
[0024] Fifthly, this application provides a computer program product that, when run on a computer, causes the computer to perform any of the methods described in the first aspect above. Attached Figure Description
[0025] Figure 1 A schematic diagram illustrating a conventional memory reclamation triggering mechanism provided in an embodiment of this application;
[0026] Figure 2 A schematic diagram illustrating the memory reclamation triggering mechanism provided in an embodiment of this application;
[0027] Figure 3 A schematic diagram of the principle of the memory reclamation method provided in the embodiments of this application. Figure 1 ;
[0028] Figure 4 A schematic diagram of the principle of the memory reclamation method provided in the embodiments of this application. Figure 2 ;
[0029] Figure 5 A flowchart illustrating the memory reclamation method provided in this application embodiment. Figure 1 ;
[0030] Figure 6 A schematic diagram of the principle of the memory reclamation method provided in the embodiments of this application. Figure 3 ;
[0031] Figure 7 A schematic diagram of the principle of the memory reclamation method provided in the embodiments of this application. Figure 4 ;
[0032] Figure 8 A flowchart illustrating the memory reclamation method provided in this application embodiment. Figure 2 ;
[0033] Figure 9 A hardware structure block diagram of a mobile phone provided in an embodiment of this application;
[0034] Figure 10 This is a software structure block diagram of a mobile phone provided in an embodiment of this application. Detailed Implementation
[0035] 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 limit the application. As used in the specification and appended claims of this application, the singular expressions "a," "the," "the," and "this" are intended to also 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). The term "and / or" is used to describe the relationship between related objects, indicating that three relationships can exist; for example, A and / or B can represent: A alone, A and B simultaneously, or B alone, where A and B can be singular or plural. The character " / " generally indicates that the preceding and following related objects are in an "or" relationship. In the description of the embodiments, unless otherwise stated, "multiple" means two or more.
[0036] 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. The terms "first," "second," "third," etc., are used for descriptive purposes only and should not be construed as indicating or implying relative importance or implicitly specifying the number of indicated technical features. Therefore, a feature defined with "first," "second," "third," etc., may explicitly or implicitly include one or more of that feature.
[0037] In the embodiments of this application, the words "exemplary," "for example," or "for instance" are used to indicate examples, illustrations, or explanations. Any embodiment or design described as "exemplary," "for example," or "for instance" in the embodiments of this application should not be construed as being more preferred or advantageous than other embodiments or designs. Specifically, the use of the words "exemplary," "for example," or "for instance" is intended to present the relevant concepts in a specific manner.
[0038] Using asynchronous memory reclamation threads is a common memory reclamation method in the industry. However, in some current solutions, electronic devices are prone to stuttering when using asynchronous memory reclamation threads. For example, the asynchronous memory reclamation thread can be Kswapd. Kswapd is a kernel thread responsible for performing memory reclamation when memory is insufficient. The asynchronous memory reclamation thread is not limited to Kswapd; it can be any other thread with memory reclamation capabilities.
[0039] It should be understood that asynchronous garbage collection threads consume CPU resources during operation. Therefore, if the operation of asynchronous garbage collection threads is not restricted, the following problems can easily occur during the use of mobile devices such as smartphones: asynchronous garbage collection threads remain under high load for extended periods, consuming excessive CPU resources, leading to insufficient CPU resources for critical applications or tasks on the electronic device, thus causing lag. For example, during a video call task performed by an instant messaging application on a mobile phone, excessive CPU consumption by Kswapd can cause video calls to lag, impacting the user experience.
[0040] However, excessively restricting the operation of asynchronous garbage collection threads can easily lead to low memory reclamation efficiency, resulting in insufficient memory and thus blocking the operation of applications or programs on electronic devices, which can also cause lag.
[0041] Currently, system kernels (such as the Linux kernel) primarily manage memory reclamation triggering mechanisms through three memory watermarks: the High Watermark, the Low Watermark, and the Minimum Watermark.
[0042] In some solutions, upon receiving a memory request, the electronic device checks the current amount of free memory. For example... Figure 1 As shown, when the current amount of free memory is below the low watermark, an asynchronous garbage collection thread (such as Kswapd) is triggered and CPU resources are randomly allocated to it. The asynchronous garbage collection thread can then run based on the allocated CPU resources to perform memory reclamation. When the current amount of free memory is below the minimum watermark, direct reclaim is triggered. The high watermark is the upper limit for stopping memory reclamation. That is, once memory reclamation reaches the high watermark, memory reclamation stops, and the asynchronous garbage collection thread stops running.
[0043] It should be understood that Direct Reclaim is a synchronous memory reclamation method. Specifically, when Direct Reclaim is triggered, the kernel begins scanning pages in memory to find those that can be reclaimed. This process is synchronous, meaning it blocks other memory allocation requests until enough memory is reclaimed to satisfy a new memory allocation request. In other words, after receiving a memory allocation request that triggers Direct Reclaim, other memory allocation requests are temporarily suspended until the reclamation operation is complete and sufficient memory space is released. This method can quickly and effectively release memory pages and alleviate memory pressure, but it may also cause system response delays because other ongoing memory allocation requests are blocked.
[0044] The inventors of this application discovered through research that using the above-described scheme to trigger asynchronous garbage collection threads for memory reclamation can easily lead to the stuttering problem described above. Therefore, this application proposes a memory reclamation method applied to electronic devices, aiming to alleviate stuttering issues during the use of electronic devices.
[0045] In this embodiment, the triggering mechanism for the asynchronous recycling thread has been improved.
[0046] One improvement is to set the high memory watermark as the memory watermark that triggers the awakening of asynchronous garbage collection threads. For ease of description, the memory watermark that triggers the awakening of asynchronous garbage collection threads can be referred to as the triggering memory watermark.
[0047] In some embodiments of this application, only the high watermark can be used as the triggering watermark. In other embodiments of this application, it is not limited to using only the high watermark as the trigger to wake up the asynchronous garbage collection thread. For example, the low watermark can also be retained as the triggering watermark to wake up the asynchronous garbage collection thread. That is, both the high and low watermarks can be used as triggering watermarks. There is no limitation on this; the main requirement is that the high watermark can be used as the triggering watermark.
[0048] Thus, upon receiving a memory allocation request, the electronic device can respond to the request and check the current amount of free memory. The electronic device can compare the current amount of free memory to the high watermark. If the current amount of free memory is below the high watermark but above the low watermark, the asynchronous garbage collection thread is activated.
[0049] Compared to Figure 1 The improved triggering mechanism proposed in this embodiment, compared to the traditional method, can wake up the asynchronous reclamation thread in advance based on the high memory watermark, instead of waiting until the amount of free memory falls below the low memory watermark. Essentially, by triggering the asynchronous reclamation thread to perform memory reclamation when free memory is relatively abundant, compared to waking up the asynchronous reclamation thread only when memory is significantly insufficient (e.g., below the low watermark) or severely insufficient, memory can be kept in a relatively abundant state as much as possible or with a higher probability. This reduces the possibility of application blocking due to insufficient memory and can alleviate the lag problem of electronic devices to some extent.
[0050] The second improvement is that, in addition to setting the high watermark as the trigger watermark, the method in this embodiment can also control the operation of the asynchronous recycling thread after waking it up when the current amount of free memory is lower than the high watermark but higher than the low watermark, so that the asynchronous recycling thread can consume relatively less CPU resources when it runs.
[0051] Specifically, the electronic device can control an asynchronous memory reclamation thread to run by binding to a small CPU core and / or based on a second parameter value set by a preset parameter, and reclaim memory during the process. The second parameter value is less than a first parameter value pre-set for the preset parameters. It should be understood that the CPU resources used by the electronic device when running based on the second parameter value are less than those used when running based on the first parameter value.
[0052] In some embodiments, the preset parameters may include priority parameters, where a first parameter value is a first priority and a second parameter value is a second priority; the CPU time slice corresponding to the second priority is less than the CPU time slice corresponding to the first priority; the CPU time slice is used to characterize the maximum duration for which the asynchronous garbage collection thread can run continuously on the CPU. Because the CPU time slice corresponding to the second priority is smaller, the asynchronous garbage collection thread occupies less CPU resources when running based on the second priority.
[0053] The solution in this embodiment triggers the asynchronous garbage collection thread to perform memory reclamation in advance when memory is relatively abundant. This ensures that the electronic device has relatively sufficient memory, reducing application blocking and lag caused by insufficient memory. Furthermore, by activating the asynchronous garbage collection thread in advance, the CPU resource consumption of the asynchronous garbage collection thread is controlled to be relatively low, thereby further mitigating electronic device lag.
[0054] The following section will describe the application scenarios of the methods in the embodiments of this application.
[0055] The method described in this application can be applied to any scenario requiring memory reclamation, such as video calls, instant messaging, and many other scenarios.
[0056] For example, in a video call scenario, a certain amount of memory is required during a video call. Therefore, the video call application can initiate a memory request. After the memory request is passed layer by layer to the memory manager in the kernel layer, the memory manager can execute the method in this embodiment to reclaim memory, thereby ensuring that the memory meets the needs of the video call.
[0057] For example, in an instant messaging scenario, when a user inputs an operation command into an instant messaging application on their phone, the application needs to consume memory to respond. Therefore, the instant messaging application can initiate a memory allocation request. After the memory allocation request is passed layer by layer to the memory manager in the kernel layer, the memory manager can execute the method in this embodiment to reclaim memory, thereby ensuring that the memory meets the user's operational needs for the instant messaging application.
[0058] The methods in the embodiments of this application will be described in more detail below with reference to the accompanying drawings.
[0059] It should be understood that upon receiving a memory request, the electronic device can compare the current amount of free memory with the memory watermark. The memory watermark in this embodiment includes at least a high watermark, a low watermark, and a minimum watermark.
[0060] In some embodiments, in addition to the three memory watermarks set in the traditional memory reclamation mechanism, an extra memory watermark is added, which can be referred to as the first memory watermark or the extra memory watermark. See also... Figure 2 This additional memory watermark is higher than the high watermark and is a new memory watermark used to trigger the cessation of memory reclamation.
[0061] Specifically, if the amount of free memory after reclamation exceeds the additional memory threshold, the asynchronous reclamation thread is stopped, i.e., memory reclamation is halted.
[0062] In some examples, the electronic device can analyze its current performance parameters to determine a suitable additional memory waterline. That is, the additional memory waterline can be one that matches the device's current state. In other examples, the additional memory waterline can be a pre-set, fixed waterline; there is no limitation on this.
[0063] It should be understood that in other embodiments, the electronic device may continue to use the high memory watermark as the memory watermark to trigger the cessation of memory reclamation, that is, stopping the asynchronous reclamation thread and thus ceasing memory reclamation once the memory watermark is exceeded. This application does not specifically limit the upper limit for triggering the cessation of memory reclamation in the embodiments.
[0064] In some embodiments, the comparison between the current amount of free memory and the memory waterline includes at least three cases, referred to as Case 1, Case 2 and Case 3.
[0065] Case 1 refers to a current free memory level below the High Watermark but above the Low Watermark. Case 2 refers to a current free memory level below the Low Watermark but above the Min Watermark. Case 3 refers to a current free memory level below the Min Watermark.
[0066] The following sections will describe the handling of the three scenarios mentioned above.
[0067] Case 1
[0068] As mentioned above, when the current amount of free memory is below the high watermark but above the low watermark (i.e., case 1), after waking up the asynchronous garbage collection thread, the electronic device can control the asynchronous garbage collection thread to consume relatively less CPU resources during operation.
[0069] In some embodiments, the high memory waterline used by the memory reclamation method in this application is greater than or higher than the initial high memory waterline built into the system. It should be understood that the initial high memory waterline is the high memory waterline built into the system's native memory reclamation mechanism. The high memory waterline in this application embodiment is adjusted upwards compared to the initial high memory waterline.
[0070] As mentioned above, in scenario 1, electronic devices can control the execution of asynchronous garbage collection threads, thereby reducing the CPU resources consumed by these threads during runtime. However, this results in relatively low memory reclamation efficiency. Therefore, by using a higher memory waterline, the memory reclamation time of the asynchronous garbage collection threads can be increased. This is equivalent to allocating more time for the asynchronous garbage collection threads when memory is plentiful, allowing them to reclaim a sufficient amount of memory more smoothly. This reduces the pressure on CPU resources from the asynchronous garbage collection threads while still ensuring the memory reclamation requirements are met.
[0071] For example, an electronic device can determine the initial increase in the high memory waterline based on the device's current state (such as current performance parameters), and then determine a high memory waterline that is adapted to the device's current state based on the increase. In other examples, the increase may also be a pre-set fixed range, which is not limited.
[0072] In some embodiments, the electronic device may perform a first processing on the asynchronous garbage collection thread, thereby enabling the asynchronous garbage collection thread after the first processing to consume relatively fewer CPU resources during runtime. That is, the CPU resources consumed by the asynchronous garbage collection thread before the first processing are greater than the CPU resources consumed by the asynchronous garbage collection thread after the first processing.
[0073] For example, the first process may include at least one of process 1 or process 2.
[0074] Solution 1:
[0075] In process 1, the electronic device can bind the awakened asynchronous garbage collection thread to a CPU small core, enabling the targeted allocation of CPU cores. Furthermore, the electronic device can control the asynchronous garbage collection thread to run on the bound CPU small core and reclaim memory during its execution.
[0076] In some embodiments, the electronic device can identify CPU little cores from the CPU cores and bind an asynchronous recycling thread to the identified CPU little cores, thereby achieving a specific allocation of CPU cores. For example, the electronic device can obtain the specification parameters and performance parameters of each core in the CPU, and compare these parameters with a preset first condition, determining the cores that meet the preset first condition as CPU little cores.
[0077] It should be understood that in traditional methods, after waking up a thread or process, CPU kernel resources are typically allocated randomly to the thread or process. Figure 1 In the traditional memory reclamation mechanism shown, when the current amount of free memory is lower than the low watermark, after waking up the asynchronous reclamation thread, CPU resources will be randomly allocated to the asynchronous reclamation thread. Therefore, to a certain extent or with a certain probability, a large CPU core will be allocated to the asynchronous reclamation thread.
[0078] The method in this application improves the CPU resource allocation mechanism after waking up the asynchronous garbage collection thread. After waking up the asynchronous garbage collection thread in advance by using the high memory watermark as the wake-up watermark, a CPU small core can be assigned to the asynchronous garbage collection thread. Subsequently, the asynchronous garbage collection thread can perform memory reclamation processing based on the assigned small core. Compared to the traditional method of randomly allocating CPU kernel resources to the asynchronous garbage collection thread, this method can reduce the CPU resources occupied by the asynchronous garbage collection thread during operation to a certain extent. Specifically, the electronic device (e.g., the memory manager in the electronic device) can bind the asynchronous garbage collection thread to a CPU small core by calling a preset interface and writing parameters that characterize the CPU small core to the interface.
[0079] Please see Figure 3 Taking the asynchronous memory reclamation thread Kswapd as an example, assuming a memory request is received at time t1, the electronic device determines that the current amount of free memory is below the high memory watermark but above the low memory watermark. If so, Kswapd can be woken up and bound to a small CPU core to reclaim memory. Reclamation continues until the amount of free memory reclaimed reaches the extra memory watermark (i.e., the amount of free memory after reclamation reaches the extra memory watermark), at which point reclamation stops. For example, time t2 is used to illustrate the point at which the extra memory watermark is reached.
[0080] For a more intuitive understanding of the memory reclamation process, please refer to [link / reference]. Figure 4 . Figure 4 The diagram illustrates that, in case 1, the asynchronous garbage collection thread is bound to a small CPU core, and the relevant garbage collection processing is based on the small core. Further details will not be elaborated here.
[0081] Process 2
[0082] It should be understood that the asynchronous garbage collection thread relies on certain parameters during its operation. These parameters include those related to the CPU resource utilization of the asynchronous garbage collection thread, which can be referred to as the first associated parameters. The first associated parameters are the preset parameters mentioned above. The values of these first associated parameters have a certain impact on the CPU resource utilization of the asynchronous garbage collection thread.
[0083] Therefore, in Case 1, the electronic device can perform Process 2 for the asynchronous return thread—that is, the electronic device can adjust the parameter values of at least some of the first associated parameters of the asynchronous recycling thread, thereby reducing the CPU resource consumption during the asynchronous recycling thread's operation based on the adjusted first associated parameter values. Specifically, the CPU resource consumption rate (which can be denoted as the second CPU resource consumption rate) during the asynchronous recycling thread's operation based on the adjusted first associated parameter values is less than the CPU resource consumption rate (which can be denoted as the first CPU resource consumption rate) during the asynchronous recycling thread's operation based on the unadjusted first associated parameter values.
[0084] In some embodiments, the first associated parameter may include the priority parameter of the asynchronous garbage collection thread. The first associated parameter is not limited to the priority parameter; any parameter that can affect the CPU resource utilization of the asynchronous garbage collection thread falls within the protection scope of the first associated parameter in this application embodiment.
[0085] Taking priority parameters as an example, the asynchronous garbage collection thread is pre-set or initially set to the first priority, which corresponds to the first CPU time slice. The first CPU time slice is the CPU time slice that the asynchronous garbage collection thread can allocate from the CPU based on the first priority. In case 1, the electronic device can control the asynchronous garbage collection thread to run based on the second priority, which is lower than the first priority. The second priority corresponds to the second CPU time slice. The second CPU time slice is the CPU time slice that the asynchronous garbage collection thread can allocate from the CPU based on the second priority. Specifically, the electronic device can downgrade the asynchronous garbage collection thread from the first priority to the second priority.
[0086] It should be understood that asynchronous garbage collection threads perform memory reclamation based on CPU time slices. Therefore, the size of the allocated CPU time slice affects the CPU resource utilization of the asynchronous garbage collection thread (i.e., CPU resource utilization rate). The larger the allocated CPU time slice, the more CPU resources the asynchronous garbage collection thread uses per unit time, resulting in a higher CPU resource utilization rate. Conversely, the smaller the allocated CPU time slice, the less CPU resources the asynchronous garbage collection thread uses per unit time, resulting in a lower CPU resource utilization rate.
[0087] Since the priority of asynchronous garbage collection threads affects the size of the CPU time slice they can be allocated, adjusting their priority can indirectly influence their CPU resource usage. Specifically, the size of the CPU time slice is positively correlated with the priority. That is, the higher the priority of the asynchronous garbage collection thread, the larger the CPU time slice it receives from the CPU, and the more CPU resources it uses per unit of time. Conversely, the lower the priority of the asynchronous garbage collection thread, the smaller the CPU time slice it receives from the CPU, and the less CPU resources it uses per unit of time.
[0088] Therefore, after downgrading the asynchronous garbage collection thread from the first priority to the second priority, the CPU time slice corresponding to the asynchronous garbage collection thread will be reduced from the first CPU time slice to the second CPU time slice. Thus, the asynchronous garbage collection thread runs based on the reduced second CPU time slice, which reduces its CPU resource consumption.
[0089] Case 2
[0090] In some embodiments, in case 2 (i.e., the current amount of free memory is lower than Low Watermark but higher than MinWatermark), the electronic device can still wake up the asynchronous recycling thread.
[0091] Essentially, both the high and low memory watermarks serve as trigger thresholds for memory reclamation. That is, there are two opportunities to wake up the asynchronous garbage collection thread. Specifically, upon receiving a memory allocation request, if the current amount of free memory falls under condition 1, then the asynchronous garbage collection thread is woken up. For example... Figure 2 As shown, this wake-up timing can be denoted as Timing 1. After receiving a memory allocation request, if the current amount of free memory falls under Case 2, the asynchronous recycling thread can also be woken up; this wake-up timing can be denoted as Timing 2.
[0092] In some embodiments, the processing performed on the asynchronous garbage collection thread differs depending on whether it is awakened at time 1 or time 2. In other words, different controls can be applied to the asynchronous garbage collection thread after it is awakened in case 1 and case 2. The control of the asynchronous garbage collection thread in case 1 is described above. The control of the asynchronous garbage collection thread in case 2 will be described below.
[0093] In scenario 2, after waking up the asynchronous garbage collection thread, the electronic device can improve the memory reclamation efficiency of the asynchronous garbage collection thread based on either method 1 or method 2. Method 1: After waking up the asynchronous garbage collection thread in scenario 2, the electronic device can perform a second process on the asynchronous garbage collection thread, thereby improving its memory reclamation efficiency. Method 2: The electronic device can also process the CPU, thereby improving the memory reclamation efficiency of the asynchronous garbage collection thread when it is running on the CPU.
[0094] The following text will first provide a detailed introduction to method 1.
[0095] Method 1
[0096] In Method 1, the electronic device can control an asynchronous memory reclamation thread to bind to a large CPU core and / or run based on a third parameter value (preset parameters), and reclaim memory during the process. The third parameter value is greater than the first parameter value. It should be understood that the memory reclamation efficiency of the electronic device running based on the third parameter value is higher than that running based on the first parameter value. This reduces, to some extent, the application blocking and lag issues caused by untimely memory reclamation.
[0097] In some embodiments, the preset parameters may include priority parameters. The third parameter is a third priority, and the first parameter is a first priority. The first priority is a priority preset for the asynchronous garbage collection thread. The CPU time slice corresponding to the third priority is greater than the CPU time slice corresponding to the first priority. Therefore, the memory reclamation efficiency is relatively higher when the asynchronous garbage collection thread runs based on the third priority.
[0098] In some embodiments, the above-mentioned control asynchronous recycling thread is bound to a large CPU core and / or runs based on the third parameter value of the preset parameters, which can be referred to as the second process.
[0099] It should be understood that in scenario 2, the current amount of free memory is already below the Low Watermark, which means that memory is clearly insufficient and memory is tight, so memory should be reclaimed quickly. Therefore, when memory is tight, electronic devices can perform secondary processing on the asynchronous reclamation thread after waking it up, in order to improve the efficiency of the asynchronous reclamation thread in reclaiming memory (i.e., memory reclamation efficiency).
[0100] In some embodiments, the low memory watermark used in the memory reclamation method of this application is lower than or below the initial low memory watermark. It should be understood that the initial low memory watermark is the low watermark inherent in the system's native memory reclamation mechanism. The low memory watermark in this application embodiment is lower than the initial high memory watermark.
[0101] In scenario 2, the asynchronous garbage collection thread undergoes a second processing step to improve its memory reclamation efficiency, which inevitably increases CPU resource usage. Therefore, by using a lower memory waterline, the time or space during which the asynchronous garbage collection thread consumes high CPU resources can be shortened. In other words, memory reclamation efficiency is improved only when memory is more critical, thus reducing overall CPU resource usage during the entire memory reclamation process.
[0102] For example, an electronic device can determine the initial reduction range of the low memory waterline based on the device's current state (such as current performance parameters), and then determine a low memory waterline that is adapted to the device's current state based on the reduction range. In other examples, the reduction range can also be a pre-set fixed range, which is not limited.
[0103] This second process differs from the first process performed on the asynchronous recycling thread in Case 1 described above. In some embodiments, the second process may include at least one of process 3 or process 4.
[0104] Process 3
[0105] In step 3, the electronic device can bind the awakened asynchronous garbage collection thread to a large CPU core, enabling the targeted allocation of CPU cores. Furthermore, the electronic device can control the asynchronous garbage collection thread to run on the bound large CPU core and reclaim memory during its execution.
[0106] In some embodiments, the electronic device can identify large CPU cores from the CPU kernels and bind an asynchronous recycling thread to the identified large CPU core, thereby achieving a specific allocation of CPU cores. For example, the electronic device can obtain the specification parameters and performance parameters of each core in the CPU, and compare these parameters with a preset second condition, determining the cores that meet the preset second condition as large CPU cores.
[0107] As mentioned above, for Figure 1 In the traditional memory reclamation mechanism shown, when the current amount of free memory is lower than the low watermark, after waking up the asynchronous reclamation thread, CPU resources will be randomly allocated to the asynchronous reclamation thread. Therefore, to a certain extent or with a certain probability, a small CPU core will be allocated to the asynchronous reclamation thread.
[0108] The method in this embodiment improves the CPU resource allocation mechanism after waking up the asynchronous garbage collection thread. When the current amount of free memory is below the low watermark but above the minimum watermark, after waking up the asynchronous garbage collection thread, a large CPU core can be assigned to it. Subsequently, the asynchronous garbage collection thread can perform memory reclamation based on the assigned large core. Compared to the traditional method of randomly allocating CPU kernel resources to the asynchronous garbage collection thread, this method can improve the memory reclamation efficiency of the asynchronous garbage collection thread to a certain extent.
[0109] Specifically, an electronic device (e.g., a memory manager in an electronic device) can bind an asynchronous recycling thread to a CPU core by calling a preset interface and writing parameters that characterize the CPU core to that interface.
[0110] Please see Figure 5 The memory reclamation method in the embodiments of this application will be described in conjunction with Case 1 and Case 2.
[0111] like Figure 5 As shown, after receiving a memory request, the electronic device can compare the current amount of free memory with the memory waterline.
[0112] In scenario 1 (where the current amount of free memory is below the high memory threshold but above the low memory threshold), the asynchronous memory reclamation thread is awakened and bound to a small CPU core. Subsequently, the asynchronous memory reclamation thread runs on the bound small CPU core and reclaims memory.
[0113] In scenario 2 (where the current amount of free memory is below the low memory threshold but above the minimum memory threshold), the asynchronous memory reclamation thread is awakened and bound to a large CPU core. Subsequently, the asynchronous memory reclamation thread runs on the bound large CPU core and reclaims memory.
[0114] Process 4
[0115] The asynchronous garbage collection thread relies on certain associated parameters during its operation, which can be referred to as the second associated parameter. This second associated parameter is the preset parameter mentioned above. The value of the second associated parameter has a certain impact on the memory reclamation efficiency of the asynchronous garbage collection thread. It should be understood that the second associated parameter can be the same as or different from the first associated parameter mentioned above; this is not a limitation.
[0116] Therefore, in case 2, the electronic device can perform processing 4 on the asynchronous return thread—that is, the electronic device can adjust the parameter values of at least some of the second associated parameters of the asynchronous reclamation thread, thereby improving memory reclamation efficiency when the asynchronous reclamation thread runs based on the adjusted parameter values of the second associated parameters. In other words, the memory reclamation efficiency (which can be denoted as the second memory reclamation efficiency) when the asynchronous reclamation thread runs based on the adjusted parameter values of the second associated parameters is higher than the memory reclamation efficiency (which can be denoted as the first memory reclamation efficiency) when the asynchronous reclamation thread runs based on the unadjusted parameter values of the second associated parameters.
[0117] In some embodiments, the second associated parameter may include the priority parameter of the asynchronous garbage collection thread. The second associated parameter is not limited to the priority parameter; any parameter that can affect the memory reclamation efficiency of the asynchronous garbage collection thread falls within the protection scope of the second associated parameter in this application.
[0118] Taking priority parameters as an example, the asynchronous recycling thread is pre-set or initially set to a first priority, which corresponds to a first CPU time slice. The first CPU time slice is the CPU time slice allocated to the asynchronous recycling thread from the CPU based on the first priority. In case 2, the electronic device can control the asynchronous recycling thread to run based on a third priority, which is higher than the first priority. The third priority corresponds to a third CPU time slice. The third CPU time slice is the CPU time slice that the asynchronous recycling thread can allocate from the CPU based on the third priority. Specifically, the electronic device can upgrade the asynchronous recycling thread from the first priority to the third priority.
[0119] As discussed above, the size of the allocated CPU time slice affects the CPU resource usage of the asynchronous garbage collection thread, and thus its memory reclamation efficiency. A larger allocated CPU time slice results in higher memory reclamation efficiency, while a smaller allocated CPU time slice leads to lower memory reclamation efficiency.
[0120] As mentioned above, the priority of the asynchronous garbage collection thread affects the size of the CPU time slice it can be allocated to. Therefore, adjusting the priority of the asynchronous garbage collection thread can indirectly affect its memory reclamation efficiency. Specifically, the size of the CPU time slice is positively correlated with the priority. That is, the higher the priority of the asynchronous garbage collection thread, the larger the CPU time slice it obtains from the CPU, and the higher the memory reclamation efficiency. Conversely, the lower the priority of the asynchronous garbage collection thread, the smaller the CPU time slice it obtains from the CPU, and the lower the memory reclamation efficiency.
[0121] Therefore, after upgrading the asynchronous garbage collection thread from the first priority to the third priority, the CPU time slice corresponding to the asynchronous garbage collection thread will increase from the first CPU time slice to the third CPU time slice. Thus, the asynchronous garbage collection thread, running on the increased third CPU time slice, can reclaim memory more quickly, improving memory reclamation efficiency.
[0122] In some embodiments, under case 2, after the memory reclaimed by the asynchronous reclamation thread following the second processing meets the third condition, the electronic device can perform a third processing on the asynchronous reclamation thread. The CPU resource utilization of the asynchronous reclamation thread after the third processing is reduced. The electronic device can perform memory reclamation based on the asynchronous reclamation thread following the third processing.
[0123] The third condition is used to characterize a state where memory is no longer excessively strained and is relatively abundant. For example, the third condition may include the amount of free memory after reclamation reaching a preset memory threshold or a high memory watermark. Exemplarily, the preset memory threshold may be less than the high watermark but greater than the low watermark, or the preset memory threshold may be higher than the high watermark. In this embodiment, the third condition is not specifically limited. Essentially, after memory becomes relatively abundant, controlling the asynchronous reclamation thread to minimize CPU resource usage ensures a relative balance between memory reclamation efficiency and CPU resource usage, thereby minimizing CPU resource consumption while meeting memory usage requirements.
[0124] Third processing of asynchronous recycling threads may include processing 5 or processing 6.
[0125] Process 5
[0126] After processing step 5, the asynchronous garbage collection thread is restored to its state before the second processing step. Therefore, electronic devices can perform memory reclamation based on the state of the asynchronous garbage collection thread before the second processing step, reducing CPU resource consumption.
[0127] Process 6
[0128] The asynchronous recycling thread after processing step 5 consumes less CPU resources compared to its state before the second processing step.
[0129] For example, during the execution of process 5, the electronic device can lower the priority of the asynchronous recycling thread. The specific principle can be found in the description of process 2 above, and will not be repeated here.
[0130] For example, during the execution of process 5, the electronic device can switch the asynchronous memory reclamation thread from being bound to a large CPU core to being bound to a small CPU core. For instance, if the asynchronous memory reclamation thread, based on the second process, has reclaimed memory to the high memory threshold, the asynchronous memory reclamation thread is unbound from the large CPU core and switched to be bound to the small CPU core. The electronic device can then reclaim memory based on the asynchronous memory reclamation thread bound to the small CPU core.
[0131] Please see Figure 6 Taking the asynchronous memory reclamation thread Kswapd as an example, assuming a memory request is received at time t3, the electronic device determines that the current amount of free memory is below the low memory threshold but above the minimum memory threshold (i.e., below the low memory threshold but above the minimum memory threshold). If so, Kswapd can be woken up and bound to a large CPU core. When memory reclamation reaches the high memory threshold (i.e., the amount of free memory after reclamation reaches the high memory threshold), for example, time t4 illustrates the point where memory reclamation reaches the high memory threshold. The electronic device can then switch Kswapd from being bound to a large CPU core to being bound to a small CPU core, and the asynchronous memory reclamation thread continues to reclaim memory based on the bound small core. This continues until memory reclamation reaches an additional memory threshold (i.e., the amount of free memory after reclamation reaches the additional memory threshold), at which point reclamation stops. For example, time t5 illustrates the point where memory reclamation reaches the additional memory threshold.
[0132] For a more intuitive understanding of the memory reclamation process, please refer to [link / reference]. Figure 7 . Figure 7 The diagram illustrates that, in scenario 2, after memory reclamation reaches the high memory threshold, the asynchronous reclamation thread is switched from being bound to a large CPU core to being bound to a small CPU core, and reclamation continues based on the small core. Further details are omitted here.
[0133] Next, we will introduce method 2 to improve the memory reclamation efficiency of asynchronous garbage collection threads in case 2.
[0134] Method 2
[0135] Specifically, when the current amount of free memory is below the low memory threshold but above the minimum memory threshold (i.e., case 2), the electronic device can wake up the asynchronous garbage collection thread and increase the CPU frequency. The asynchronous garbage collection thread then runs based on the increased CPU frequency.
[0136] Frequency boosting refers to increasing the operating frequency of the CPU.
[0137] It should be understood that after frequency increase, the CPU can complete more instruction cycles per unit time, thereby speeding up task processing. Therefore, running the asynchronous memory reclamation thread based on the frequency-increased CPU can improve the memory reclamation efficiency of the asynchronous reclamation thread, and to some extent reduce application blocking and stuttering caused by untimely memory reclamation.
[0138] Case 3
[0139] Specifically, when the current amount of free memory is below the minimum memory watermark, direct memory reclamation is triggered (e.g., ...). Figure 2 (As shown).
[0140] In some embodiments, if the amount of free memory is increased above the minimum memory threshold based on the direct memory reclamation, the direct memory reclamation is stopped, and the asynchronous reclamation thread is woken up. For example, after waking up the asynchronous reclamation thread, memory can be quickly reclaimed based on the asynchronous reclamation thread.
[0141] In some examples, after stopping direct memory reclamation and waking up the asynchronous reclamation thread, the electronic device can bind the asynchronous reclamation thread to a large CPU core, and then continue reclamation based on the asynchronous reclamation thread bound to the large CPU core. This allows for rapid memory reclamation based on the asynchronous reclamation thread.
[0142] In some examples, after stopping direct memory reclamation and waking up the asynchronous reclamation thread, the electronic device can increase the priority of the asynchronous reclamation thread, and then continue to reclaim memory based on the asynchronous reclamation thread with the increased priority. This allows for faster memory reclamation based on the asynchronous reclamation thread. For details on the priority increase process, please refer to the description in process 4 above, which will not be repeated here.
[0143] In some examples, after stopping direct memory reclamation and waking up the asynchronous reclamation thread, the electronic device can bind the asynchronous reclamation thread to a CPU small core, and continue to reclaim memory based on the asynchronous reclamation thread after binding to the CPU small core, or lower the priority of the asynchronous reclamation thread and control the asynchronous reclamation thread to reclaim memory based on the lowered priority.
[0144] In some embodiments, after the asynchronous reclamation thread, bound to a large CPU core, quickly reclaims memory to the high watermark, the electronic device can unbind the asynchronous reclamation thread from the large CPU core and bind it to a small CPU core. Memory reclamation can then continue based on the asynchronous reclamation thread bound to the small CPU core. Alternatively, the electronic device can lower the priority of the asynchronous reclamation thread, controlling it to continue reclamation based on the lowered priority. The specific processing principles can be found in the descriptions of Process 1 or Process 2 above, and will not be repeated here.
[0145] In some embodiments, combined with Figure 8 The memory reclamation method in the embodiments of this application is described.
[0146] Please see Figure 8 For cases 1 (current free memory is below the high memory threshold but above the low memory threshold) and 3 (current free memory is below the minimum memory threshold), please refer to the descriptions above; they will not be repeated here. For case 2 (current free memory is below the low memory threshold but above the minimum memory threshold), as described above, the electronic device can wake up the asynchronous memory reclamation thread and bind it to a large CPU core. The asynchronous reclamation thread then runs on the bound large CPU core and reclaims memory. When the amount of free memory after reclamation reaches the high memory threshold, the asynchronous reclamation thread is switched from being bound to a large CPU core to being bound to a small CPU core, and memory reclamation continues. Finally, when the amount of free memory after reclamation reaches the additional memory threshold (i.e., the first memory threshold), reclamation stops. That is, the asynchronous reclamation thread stops running.
[0147] For example, the aforementioned electronic devices may be mobile phones, tablets, desktops, laptops, handheld computers, notebook computers, ultra-mobile personal computers (UMPCs), netbooks, as well as cellular phones, personal digital assistants (PDAs), augmented reality (AR) / virtual reality (VR) devices, etc. This application does not impose special limitations on the specific form of the electronic device. The following mainly uses a mobile phone as an example to illustrate the solution in this application. It should be understood that the principle of implementing the solution in this application is the same for other electronic devices as for mobile phones, therefore, it will not be described in detail for different types of electronic devices.
[0148] For ease of understanding, a mobile phone is used as an example to illustrate the hardware structure and software system of the electronic device to which the method in the embodiments of this application is applied.
[0149] Please see Figure 9 This is a block diagram illustrating the hardware structure of a mobile phone in some embodiments.
[0150] See Figure 9 The mobile phone may include a processor 910, an external memory interface 920, an internal memory 921, a universal serial bus (USB) interface 930, a charging management module 940, a power management module 941, a battery 942, antenna 1, antenna 2, a mobile communication module 950, a wireless communication module 960, an audio module 970, a speaker 970A, a receiver 970B, a microphone 970C, a headphone jack 970D, a sensor 980, buttons 990, a motor 991, an indicator 992, a camera 993, a display screen 994, and a subscriber identification module (SIM) card interface 995, etc.
[0151] It is understood that the structures illustrated in the embodiments of the present invention do not constitute a specific limitation on the mobile phone. In other embodiments of this application, the mobile phone 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.
[0152] The processor 910 may include one or more processing units, such as an application processor (AP), a modem processor, a graphics processing unit (GPU), an image signal processor (ISP), a controller, memory, a video codec, a digital signal processor (DSP), a baseband processor, and / or a neural network processing unit (NPU). These different processing units may be independent devices or integrated into one or more processors.
[0153] In some embodiments, the mobile phone can perform a memory reclamation method through the processor 910, so that the memory resources in the mobile phone can better meet the application requirements and reduce the lag of the mobile phone.
[0154] The internal memory 921 can be used to store computer executable program code, which includes instructions. The processor 910 executes various mobile phone functions and data processing by running the instructions stored in the internal memory 921. The internal memory 921 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 mobile phone use (such as audio data, phonebook, etc.). Furthermore, the internal memory 921 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.
[0155] For example, the memory reclamation method in this application embodiment is used to reclaim memory pages that are no longer used or are used less frequently in the internal memory 921. By reclaiming these memory pages, the system can free up more memory space for use by other programs or processes.
[0156] The wireless communication function of a mobile phone can be achieved through antenna 1, antenna 2, mobile communication module 950, wireless communication module 960, modem processor, and baseband processor.
[0157] The mobile phone can perform audio functions, such as music playback and recording, through the audio module 970, speaker 970A, receiver 970B, microphone 970C, headphone jack 970D, and application processor.
[0158] Keypad 990 may include a power button, volume buttons, etc. Keypad 990 can be a mechanical keypad or a touch-sensitive keypad. The phone can receive keypad input and generate key signal inputs related to user settings and function control.
[0159] Motor 991 can generate vibration alerts. Motor 991 can be used for incoming call vibration alerts or for touch vibration feedback. Indicator 992 can be an indicator light, used to indicate charging status, battery level changes, or to indicate messages, missed calls, notifications, etc.
[0160] Camera 993 is an actual physical camera. In some embodiments of this application, the mobile phone may have multiple cameras 993; for example, multiple cameras 993 may include a front-facing camera and a rear-facing camera. There may be one or more front-facing cameras, and there may also be one or more rear-facing cameras.
[0161] The mobile phone can achieve its shooting function through the ISP, camera 993, video codec, GPU, display 994, and application processor. For example, applications in the mobile phone can access the camera 993 and control it to capture images.
[0162] The mobile phone implements its display function through a GPU, a display screen 994, and an application processor. The GPU is a microprocessor for image processing, connected to the display screen 994 and the application processor. The GPU is used to perform mathematical and geometric calculations for graphics rendering. The processor 910 may include one or more GPUs, which execute program instructions to generate or modify display information.
[0163] Display screen 994 is used to display images, videos, etc.
[0164] The SIM card interface 995 is used to connect the SIM card. The SIM card can be inserted into or removed from the SIM card interface 995 to achieve contact and separation with the mobile phone.
[0165] Mobile phone software systems can adopt layered architecture, event-driven architecture, microkernel architecture, microservice architecture, or cloud architecture. This application uses a layered architecture software system as an example to illustrate the software structure of a mobile phone. It should be noted that this application is not limited to the software structure of mobile phones.
[0166] Figure 10 This is a block diagram of a mobile phone software system provided in this application.
[0167] A layered architecture can divide a software system into several layers, each with a clear role and function. Layers communicate with each other through software interfaces. In some embodiments, a mobile phone's software system may include an application layer, an application framework layer, a system library layer, and a kernel layer (Linux Kernel layer).
[0168] The application layer can include a series of application packages. The application layer can include native system applications such as camera and gallery, as well as third-party applications, such as instant messaging applications (not shown in the image).
[0169] The application framework layer provides application programming interfaces (APIs) and programming frameworks for applications in the application layer. The application framework layer includes some predefined functions.
[0170] In some embodiments, the application framework layer may include a window manager, a content provider, etc.
[0171] The system library layer provides interfaces for triggering memory allocation, which can be simply referred to as memory allocation interfaces. For example, the memory allocation interface can be provided by the malloc function. The malloc function is a memory allocation function that requests a contiguous block of memory from the system and returns a pointer to the starting address of that memory block. Exemplarily, after the malloc function is called, it sends a request to the kernel layer to dynamically allocate memory.
[0172] The kernel layer is the layer between hardware and software. The kernel layer includes the memory manager. The memory manager is responsible for allocating, releasing, and managing physical and virtual memory within the operating system.
[0173] For example, the kernel layer provides a system call interface (not shown in the diagram). When a system library requests memory allocation, it can call the kernel layer's system call interface, thereby triggering the memory manager to perform memory allocation processing. For example, the application framework layer calls the system library.
[0174] The memory manager includes a memory reclamation module, which is used to perform memory reclamation using the methods described in the embodiments of this application. It should be understood that the embodiments of this application are equivalent to optimizing and improving the memory manager to enable it to perform memory reclamation using the methods described in the embodiments of this application, thereby reducing phone lag and improving user experience during the memory reclamation process.
[0175] This application also provides a chip system applied to an electronic device. The chip system includes one or more processors, which are used to invoke computer instructions to cause the electronic device to perform the functions or steps described in the above method embodiments.
[0176] This application also provides a computer-readable storage medium including computer instructions that, when executed on the electronic device, cause the electronic device to perform the various functions or steps described in the method embodiments.
[0177] This application also provides a computer program product that, when run on a computer, causes the computer to perform the functions or steps described in the above method embodiments.
[0178] Through the above description of the embodiments, those skilled in the art can clearly understand that, for the sake of convenience and brevity, only the division of the above functional modules is used as an example. In actual 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.
[0179] In the several embodiments provided in this application, it should be understood that the disclosed apparatus and methods can be implemented in other ways. For example, the apparatus embodiments described above are merely illustrative; for instance, the division of modules or units is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another apparatus, or some features may be ignored or not executed. Furthermore, the mutual coupling or direct coupling or communication connection shown or discussed may be through some interfaces; the indirect coupling or communication connection between apparatuses or units may be electrical, mechanical, or other forms.
[0180] The units described as separate components may or may not be physically separate. A component shown as a unit can be one or more physical units; that is, it can be located in one place or distributed in multiple different locations. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0181] 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.
[0182] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a readable storage medium. Based on this understanding, the technical solutions of the embodiments of this application, in essence, or the parts that contribute to the prior art, or all or part of the technical solutions, can be embodied in the form of a software product. This software product is stored in a storage medium and includes several instructions to cause a device (which may be a microcontroller, chip, etc.) or processor to execute all or part of the steps of the methods of the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0183] 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 reclamation method, characterized in that, The method includes: In response to a memory allocation request, if the current amount of free memory is below the high memory threshold but above the low memory threshold, the asynchronous garbage collection thread is woken up. The asynchronous memory reclamation thread is controlled to bind to a small CPU core and / or run based on a second priority, and reclaims memory during operation; wherein, the second priority is less than the first priority preset for the asynchronous memory reclamation thread; the CPU time slice corresponding to the second priority is less than the CPU time slice corresponding to the first priority; the CPU time slice is used to characterize the maximum duration for which the asynchronous memory reclamation thread can run continuously on the CPU.
2. The method according to claim 1, characterized in that, The method further includes: If the current amount of free memory is lower than the low memory threshold but higher than the minimum memory threshold, the asynchronous garbage collection thread will be woken up. The asynchronous memory reclamation thread is controlled to bind to a large CPU core and / or run based on the third priority, and memory is reclaimed during the operation; wherein the third priority is greater than the first priority, and the CPU time slice corresponding to the third priority is greater than the CPU time slice corresponding to the first priority.
3. The method according to claim 1, characterized in that, The method further includes: If the current amount of free memory is lower than the low memory threshold but higher than the minimum memory threshold, the asynchronous recycling thread is woken up and the CPU frequency is increased. The asynchronous recycling thread is run based on the CPU with increased frequency.
4. The method according to any one of claims 1-3, characterized in that, The low memory waterline is smaller than the initial low memory waterline, and / or the high memory waterline is larger than the initial high memory waterline.
5. The method according to any one of claims 1-4, characterized in that, The method further includes: If the current amount of free memory is below the minimum memory threshold, direct memory reclamation is triggered.
6. The method according to claim 5, characterized in that, The method further includes: If the amount of free memory is increased to above the minimum memory threshold based on the direct memory reclamation, the direct memory reclamation is stopped, and the asynchronous reclamation thread is woken up and bound to the CPU big core; An asynchronous memory reclamation thread, based on the CPU's large core, continues to reclaim memory.
7. The method according to claim 2 or 6, characterized in that, The method further includes: When the memory is reclaimed to the high memory threshold by the asynchronous reclamation thread based on the CPU large core, the asynchronous reclamation thread is unbound from the CPU large core and bound to the CPU small core; Memory is reclaimed based on an asynchronous recycling thread that is bound to the CPU small core.
8. The method according to any one of claims 1-7, characterized in that, The method further includes: If the amount of free memory after reclamation is greater than the first memory threshold, the asynchronous reclamation thread stops running; the first memory threshold is higher than the high memory threshold.
9. An electronic device, characterized in that, The electronic device includes at least a memory and one or more processors; the memory is used to store computer instructions, which, when executed by the one or more processors, cause the electronic device to perform the method as described in any one of claims 1-8.
10. A computer-readable storage medium, characterized in that, The computer-readable storage medium includes computer instructions that, when executed on an electronic device, cause the electronic device to perform the method as described in any one of claims 1-8.
11. 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-8.