Memory recovery method and device and storage medium
By distinguishing the memory recycling strategies of the foreground and background processes and optimizing memory management, the fluency problem of the foreground processes caused by frequent memory applications for background processes is solved, and the smoothness of the running of electronic devices and user experience is improved.
Patent Information
- Application Number
- CN202311840774.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-12-28
- Publication Date
- 2025-07-01
AI Technical Summary
In the memory recycling mechanism of the existing technology, it is impossible to effectively distinguish the priority of the foreground and background processes, resulting in the smooth running of the foreground applications being affected by the frequent memory applications of the background processes, affecting the user experience.
By judging the running type of the process, distinguishing between the foreground and the background process, targeting the scanning amount of the memory recovery process and the use of the processor core, giving priority to ensuring the memory requirements of the foreground process, and limiting the memory recovery frequency and resource usage of the background process.
It improves the smooth operation of electronic devices in the foreground application, reduces the reloading phenomenon caused by insufficient memory of the foreground process, and improves the overall user experience.
Smart Images

Figure CN120234129A_ABST
Abstract
Description
Technical Field
[0001] This application relates to computer software technology, and in particular, to a memory recycling method, device, and storage medium. Background Art
[0002] The Android mobile terminal uses the Linux kernel at the bottom layer. In the Linux kernel, the kernel recycling process (Kernel Swap Daemon, kswapd) is a kernel daemon thread. When allocating memory, if the available memory is lower than the set low watermark, the system will wake up the kswapd thread to recycle memory until the system's free memory reaches the set high watermark and there is memory required for this time, then stop the memory recycling work and enter the sleep state, so that the system is in a healthy memory state.
[0003] Currently, the recycling mechanisms of processes mainly include time-based recycling mechanisms and process priority-based recycling mechanisms, etc. Specifically, processes can be recycled according to the longest time interval, or processes can be recycled based on the height of the process priority, thus affecting the smooth running of the electronic device. Summary of the Invention
[0004] Embodiments of this application provide a memory recycling method, device, and storage medium, which can improve the smooth running of the electronic device.
[0005] The technical solution of the embodiments of this application is implemented as follows:
[0006] Embodiments of this application provide a memory recycling method, including:
[0007] When receiving a memory application of a first process, determining whether the current free memory meets the memory recycling condition;
[0008] When the current free memory meets the memory recycling condition, determining the running type of the first process, where the running type indicates whether the first process has a business interaction with the user;
[0009] Based on the running type of the first process, controlling a second process to recycle memory, where the second process is the process for recycling memory.
[0010] Embodiments of this application provide an electronic device, including:
[0011] A first determination unit, configured to determine whether the current free memory meets the memory recycling condition when receiving a memory application of a first process;
[0012] A second judgment unit, configured to judge the running type of the first process when the current free memory meets the memory recycling condition, where the running type indicates whether there is a service interaction between the first process and the user using the device;
[0013] A control unit, configured to control a second process to perform memory recycling based on the running type of the first process, where the second process is the process for performing memory recycling.
[0014] An embodiment of the present application provides an electronic device, including a processor, where the processor is configured to:
[0015] When receiving a memory application of a first process, judge whether the current free memory meets the memory recycling condition;
[0016] When the current free memory meets the memory recycling condition, judge the running type of the first process, where the running type indicates whether there is a service interaction between the first process and the user using the device;
[0017] Based on the running type of the first process, control a second process to perform memory recycling, where the second process is the process for performing memory recycling.
[0018] An embodiment of the present application provides a terminal device, including a memory, a processor, and a computer program stored on the memory and executable on the processor. When the processor executes the computer program, the steps in the above memory recycling method are implemented.
[0019] An embodiment of the present application provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the above memory recycling method is implemented.
[0020] The chip provided by the embodiment of the present application is used to implement the above memory recycling method. The chip includes: a processor, configured to call and run a computer program from a memory, so that a device installed with the chip executes the above memory recycling method.
[0021] For the first process that sends a memory application, in the case that the current free memory meets the memory recycling condition, that is, when memory recycling is required, the second process is controlled to perform memory recycling according to whether there is a service interaction between the first process and the user using the device, so as to be able to perform memory recycling targeted according to the service interaction situation of the first process, thereby optimizing the memory recycling process and improving the running fluency of the electronic device. Description of the Drawings
[0022] Figure 1 is an optional flowchart of the memory recycling method provided by the embodiment of the present application;
[0023] Figure 2 is an optional process schematic diagram of the memory recycling method provided by an embodiment of the present application;
[0024] Figure 3 is an optional structural schematic diagram of an electronic device provided by an embodiment of the present application;
[0025] Figure 4 is an optional schematic diagram of an idle window provided by an embodiment of the present application;
[0026] Figure 5 is a schematic diagram of the function of the kswapd process provided by an embodiment of the present application;
[0027] Figure 6 is an optional process schematic diagram of the memory recycling method provided by an embodiment of the present application;
[0028] Figure 7 is an optional process schematic diagram of the memory recycling method provided by an embodiment of the present application;
[0029] Figure 8 is an optional process schematic diagram of the memory recycling method provided by an embodiment of the present application;
[0030] Figure 9 is an optional process schematic diagram of the memory recycling method provided by an embodiment of the present application;
[0031] Figure 10 is an optional structural schematic diagram of an electronic device provided by an embodiment of the present application;
[0032] Figure 11 is an optional structural schematic diagram of an electronic device provided by an embodiment of the present application;
[0033] Figure 12 is an optional structural schematic diagram of an electronic device provided by an embodiment of the present application;
[0034] Figure 13 is an optional schematic structural diagram of an electronic device provided by an embodiment of the present application. Detailed implementation manners
[0035] In order to make the objectives, technical solutions and advantages of the present application clearer, the present application will be further described in detail below with reference to the accompanying drawings. The described embodiments should not be construed as limitations on the present application. All other embodiments obtained by those of ordinary skill in the art without creative efforts shall fall within the scope of protection of the present application.
[0036] Next, the embodiments of the memory recycling method and device provided by the embodiments of the present application will be described.
[0037] The memory recycling method provided by the embodiments of this application is as follows Figure 1 shown, including:
[0038] S101. When the electronic device receives a memory application from the first process, determine whether the current free memory meets the memory recycling condition.
[0039] In the embodiments of this application, when a first application program is running in the electronic device, the first application program is associated with the first process. The first process is used to apply for memory when the first application program performs page loading, so as to load the page based on the applied memory. Among them, the first application program can be a foreground application program that interacts with the user of the electronic device and is displayed on the display interface of the electronic device, or a background application program that is not displayed on the display interface of the electronic device but runs in the background of the electronic device and does not interact with the user of the electronic device.
[0040] When the electronic device receives a memory application from the first process, determine whether the current free memory meets the memory recycling condition.
[0041] In the embodiments of this application, the memory recycling condition includes one or more of the following conditions:
[0042] Condition 1: The free memory is less than the first waterline, and the first waterline is the memory threshold value used to trigger the second process. Among them, the second process is a process for guarding memory.
[0043] Condition 2: The free memory does not include continuous free memory with a size of the first memory amount, where the first memory amount is the memory amount applied for by the first process.
[0044] If the current free memory in the electronic device meets Condition 1, it means that the current free memory meets the memory threshold value for triggering the second process.
[0045] If the current free memory in the electronic device meets Condition 2, it means that the current free memory cannot meet the memory application requirements of the first process.
[0046] In the embodiments of this application, the second process can be the kswapd process, which is woken up when the free memory is less than the first waterline. The woken second memory performs memory recycling to increase the amount of free memory. If the free memory reaches the second waterline based on the memory recycled by the second process, the second process enters the sleep state to stop memory recycling. Among them, the first waterline can be defined as the low waterline, and the second waterline can be defined as the high waterline. It can be understood that the first waterline is less than the second waterline. For example, the first waterline is 40M and the second waterline is 80M; or for another example, the first waterline is 20M and the second waterline is 100M.
[0047] In the embodiments of the present application, the conditions for the second process to enter the sleep state may include, in addition to the free memory being not less than the second waterline, that the current free memory meets the current memory application requirements, that is, when the process memory allocation can be successfully performed, the second process enters the sleep state.
[0048] In the embodiments of the present application, the memory recovery conditions include, but are not limited to, at least one of the above condition 1 and condition 2, and may also include other conditions that can wake up the second process.
[0049] S102. When the current free memory of the electronic device meets the memory recovery conditions, the electronic device determines the running type of the first process, where the running type indicates whether the first process has a service interaction with the user using the device.
[0050] When the current free memory does not meet the memory recovery conditions, there is no need to perform memory recovery based on the second process. When the current free memory meets the memory recovery conditions, it is necessary to perform memory recovery based on the second process to meet the memory application requirements of the first process.
[0051] If it is necessary to perform memory recovery based on the second process, then determine the running type of the first process. In the embodiments of the present application, the running type of the first process may be the first type or the second type. The process of the first running type has a service interaction with the user using the device, and the process of the second running type does not have a service interaction with the user using the device. Among them, if the first application belongs to the foreground application, the running type of the first process is the first type, and the first process belongs to the foreground process; if the first application belongs to the background application, the running type of the first process is the second type, and the first process belongs to the background process.
[0052] If the first process belongs to the foreground process, the first process runs in the foreground. If the first process belongs to the background process, the first process is cached in the background.
[0053] S103. The electronic device controls the second process to perform memory recovery based on the running type of the first process, where the second process is the process for performing memory recovery.
[0054] After the electronic device determines the running type of the first process, it controls the second process to perform memory recovery based on the running type of the first process.
[0055] Optionally, the electronic device controls at least one of the following metrics of the second process based on the running type of the first process:
[0056] Metric 1, memory recovery speed;
[0057] Metric 2, memory recovery amount;
[0058] Metric 3, memory recovery load of the second process.
[0059] In the embodiments of the present application, for the first process that sends a memory application, when the current idle thread meets the memory recovery condition, that is, when memory recovery is required, the second process is controlled to perform memory recovery according to whether the first process has a service interaction with the user using the device, so that memory recovery can be targeted according to the service interaction situation of the first process, thereby optimizing the memory recovery process and improving the running fluency of the electronic device.
[0060] In some embodiments, the implementation of S102 for determining the running type of the first process includes:
[0061] S1021: Determine the first application identifier of the first application associated with the first process;
[0062] A unique identifier is assigned to each application running in the electronic device to distinguish different applications. When determining the running type of the first process, the electronic device determines the unique identifier of the first application to which the process is chained, that is, the application identifier.
[0063] S1022: Compare the first application identifier with the application identifiers in the first list, where the first list stores the application identifiers of the applications running on the display interface.
[0064] The electronic device matches the first application identifier corresponding to the first process with the application identifiers in the first list. The first list stores the application identifiers of the applications running on the display interface, that is, the first list includes the application identifiers of all foreground applications.
[0065] In the embodiments of the present application, when an application runs in the foreground, the electronic device adds the application identifier of the application to the first list. Among them, the application running in the foreground can be started in the foreground or switched from the background to the foreground.
[0066] In the embodiments of the present application, when an application is switched from the foreground to the background or closed in the foreground, the electronic device deletes the application identifier of the application from the first list.
[0067] In the embodiments of the present application, the first list can be stored in the kernel of the processor.
[0068] S1023: Determine the running type of the first process according to the comparison result.
[0069] The electronic device determines the type of the first process according to whether the first application identifier of the first application is included in the first list.
[0070] In some embodiments, the implementation of S1023 for determining the running type of the first process according to the comparison result includes:
[0071] When the application identifier in the first list includes the first application identifier, determine that the running type of the first process is the first running type, and the process of the first running type runs in the foreground; when the application identifier in the first list does not include the first application identifier, determine that the running type of the first process is the second running type, and the process of the second running type runs in the background.
[0072] If the first application is included in the first list, it indicates that the first application belongs to the foreground application, and the running type of the first process is the first running type, that is, the first process belongs to the foreground process. If the first application is not included in the first list, it indicates that the first application belongs to the background application, and the running type of the first process is the second running type, that is, the first process belongs to the background process.
[0073] In one example, the first list includes the following application identifiers: application identifier 1, application identifier 2, application identifier / 3, application identifier 4; if the first application identifier corresponding to the current first process is application identifier 2, the first process belongs to the foreground process; if the first application identifier corresponding to the current first process is application identifier 6, the first process belongs to the background process.
[0074] In the embodiments of the present application, by comparing the first application identifier of the first application associated with the first process with the application identifiers included in the first list, it is possible to simply and quickly determine whether the current process belongs to the foreground process or the background process, improving the practicality of the memory recycling scheme.
[0075] In practical applications, the electronic device can also determine whether the running type of the first process is the first running type or the second running type through the running parameters of the first process or the running parameters of the first application. The embodiments of the present application do not limit the method for determining the running type of the first process.
[0076] In some embodiments, the implementation of S103 for controlling the second process to perform memory recycling based on the running type of the first process includes:
[0077] S1031, if the running type of the first process is the first running type, accelerate the memory recycling of the second process, and the process of the first running type runs in the foreground;
[0078] S1032, if the running type of the first process is the second running type, limit the speed of the memory recycling of the second process, and the process of the second running type runs in the background.
[0079] In the memory recycling method provided in the embodiments of the present application, it can be as Figure 2As shown in the figure, for the first process, determine the running type 200 of the first process. If the running type 200 of the first process is a foreground process, then execute S201 to accelerate the memory recycling of the second process. If the running type 200 of the first process is a background process, then execute S202 to limit the memory recycling of the second process.
[0080] In the memory recycling method provided by the embodiments of the present application, for foreground processes and background processes, different memory recycling control methods are used for memory recycling. Among them, for foreground processes, their memory recycling is accelerated, and for background processes, their memory recycling is restricted. Thus, by controlling memory recycling, the priority of foreground processes relative to background processes is increased, the operation of foreground processes is preferentially ensured, and the running fluency of the electronic device is improved.
[0081] In some embodiments, the implementation of accelerating the memory recycling of the second process in S1031 includes:
[0082] Run the second process in the first core of the processor, and the processing capacity of the first core is stronger than that of the second core in the processor.
[0083] In the embodiments of the present application, as Figure 3 shown, the processor 301 in the electronic device 300 includes a first core 3011 and a second core 3012. Among them, the processing capacity of the first core 3011 is higher than that of the second core 3012. Among them, the processing capacity of the core can be measured by processing speed, processing frequency, etc.
[0084] The number of cores included in the processor here can be greater than or equal to 2. The first core and the second core are any two cores among the cores included in the processor, and the processing capacity of the first core and the processing capacity of the second core are relative.
[0085] If the second process runs in the first core, the load on the processor occupied is small, so that the memory recycling efficiency of the second process is high. If the second process runs in the second core, the load on the processor occupied is large, so that the memory recycling efficiency of the second process is low.
[0086] In the embodiments of the present application, if the first process belongs to a foreground process, then control the second process to run in the first core of the processor, thereby accelerating the memory recycling speed.
[0087] In some embodiments, the implementation of accelerating the memory recycling of the second process in S1031 includes:
[0088] Adjust the set first scan recycling amount to a second scan recycling amount, and the second scan recycling amount is greater than the first scan recycling amount;
[0089] The second process is controlled to perform memory reclaim using the second scan reclaim amount as a single scan reclaim amount.
[0090] In an embodiment of the present application, if the first process is a foreground process, the size of the single scan recovery amount of the second process is increased, and the memory recovery speed of the second process is accelerated and the memory recovered by the second process is relatively large.
[0091] In some embodiments, the electronic device further implements:
[0092] The second scan recovery amount is estimated based on the memory application amount of one or more processes of the first running type within a first period of time.
[0093] In the embodiment of the present application, for the foreground process, the second scan recovery amount used in a single time when the second process performs memory recovery can be predicted by the memory application amount of the foreground process within a period of time.
[0094] Here, the electronic device can estimate the current memory request amount based on the memory request amount of the foreground process in the first time through a set prediction algorithm or prediction model, so as to meet the running requirements of the foreground process as much as possible, recover the memory required by the first process as soon as possible, and improve the memory recovery speed.
[0095] In practical applications, the second scan recovery amount can also be determined based on the current memory application amount of the first process, that is, the first memory amount. In one example, the second scan recovery amount is the first memory amount. In one example, the second scan recovery amount is N times the first memory amount, where N is greater than 1.
[0096] In some embodiments, the implementation of limiting the speed of memory reclaiming of the second process at S1032 includes:
[0097] The second process is run in a second core of a processor, wherein the processing capability of a first core in the processor is stronger than the processing capability of the second core.
[0098] If the second process runs in the first kernel, the load of the processor occupied is small, so that the memory recovery efficiency of the second process is high. If the second process runs in the second kernel, the load of the processor occupied is large, so that the memory recovery efficiency of the second process is low.
[0099] In an embodiment of the present application, if the first process is a background process, the second process is controlled to run in the second core of the processor, thereby reducing the memory recovery speed.
[0100] In some embodiments, the implementation of limiting the speed of memory reclaiming of the second process at S1032 includes:
[0101] Adjust the set first scan recovery amount to a third scan recovery amount, where the third scan recovery amount is less than the first scan recovery amount;
[0102] Control the second process to perform memory recovery with the third scan recovery amount as the single scan recovery amount.
[0103] In the embodiments of the present application, if the first process is a background process, reduce the size of the single scan recovery amount of the second process, adjust it from the set first scan recovery amount to the third scan recovery amount, reduce the memory recovery speed of the second process and make the memory recovered by the second process relatively small.
[0104] In the embodiments of the present application, for a background process, the third scan recovery amount used once by the second process during memory recovery can be predicted based on the memory application amount of the background process within a period of time.
[0105] Here, the electronic device can estimate the current memory application amount based on the set prediction algorithm or prediction model and the memory application amount of the background process within the second time, so as to reduce the amount of memory required to be recovered from the second process to the first process while meeting the operation requirements of the background process, and reduce the impact of the memory recovery of the second memory on the foreground process.
[0106] In practical applications, the third scan recovery amount can also be determined based on the current memory application amount of the first process, that is, the first memory amount. In one example, the third scan recovery amount is the first memory amount. In one example, the second scan recovery amount is one Nth of the first memory amount, where N is greater than 1.
[0107] In some embodiments, the implementation of throttling the memory recovery of the second process in S1032 includes:
[0108] Determine the memory recovery amount of the second process;
[0109] When the memory recovery amount is greater than or equal to the set recovery amount threshold and the current time is within the set time window, control the second process to be in a sleep state.
[0110] In the embodiments of the present application, to limit the memory recovery process triggered by the memory application of the background process, a time window is set. If the memory recovery amount of the second memory is greater than the set recovery amount threshold and during this time window, the second process is not woken up and the second process does not perform memory recovery.
[0111] Here, the starting time of the time window is when the memory recovery amount of the second process is greater than or equal to the set recovery amount threshold or the current free memory is greater than the set third waterline. Here, the third waterline is greater than the first waterline and less than the second waterline, or the third waterline is less than the first waterline. Among them, if the starting time of the time window is when the memory recovery amount of the second process is greater than or equal to the set recovery amount threshold, the second process can perform memory recovery after being awakened and enter the sleep state after the memory recovery amount reaches the recovery amount threshold. If the starting time of the time window is when the current free memory is greater than the set third waterline, and the third waterline is greater than the first waterline and less than the second waterline, the second process can perform memory recovery after being awakened and enter the sleep state after the memory recovery amount reaches the recovery amount threshold. If the starting time of the time window is when the current free memory is greater than the set third waterline, and the third waterline is less than the first waterline, when the current free memory meets the memory recovery condition, the second process is not awakened and thus remains in the sleep state continuously.
[0112] After the time window is started, if there is a background process applying for memory, even if the current memory-based allocation makes the free memory meet the memory recovery condition, the second process is still not awakened.
[0113] In an example, as Figure 4 shown, during the time window 401, when a background process applies for memory at time T1, the second process is not awakened. If another background process applies for memory at time T2, the second process is still not awakened.
[0114] In the embodiment of the present application, by controlling the awakening of the second process in the case of the application of the background process through the time window, the second process is prevented from being awakened. Especially when the amount of free memory is close to the first waterline, the second process is prevented from being frequently awakened, so that the resources of the processor are inclined to the foreground process, ensuring the smoothness of the running program.
[0115] Next, the memory recovery method provided by the embodiment of the present application will be described.
[0116] The Android mobile terminal uses the Linux kernel at the bottom layer. In the Linux kernel, the Kernel Swap Daemon (kswapd) is a kernel daemon thread. When allocating memory, if the available memory is lower than the set low watermark, the system will wake up the kswapd thread to perform memory recycling until the system's free memory reaches the set high watermark and there is memory for this demand, then it stops the memory recycling work and enters the sleep state, so that the system is in a healthy memory state. Among them, kswapd is an asynchronous recycling thread. By way of example, if the WeChat process is allocating memory and finds that the free memory (such as only 90M) is lower than the low watermark (such as 100M) that wakes up kswapd, it will wake up the kswapd thread to start memory recycling until the free memory is not lower than the high watermark (such as 150M). However, the WeChat process can directly take away from the free memory, and at this time, the memory allocation of the WeChat process itself is not affected.
[0117] If the free memory enters direct memory recycling, the process of allocating memory needs to wait synchronously, which will affect performance. Therefore, it is necessary to wake up kswapd for asynchronous memory recycling when it is lower than the low watermark, so as to promptly restore the system to a healthy state. For example, if the WeChat process allocates memory and only finds that the free memory is very low (such as only 20M) lower than the minimum watermark, the WeChat process cannot directly allocate memory from the free memory this time (it is necessary to leave some free memory for the system to use urgently, otherwise the system will have problems), and it needs to perform memory recycling actions by itself. That is, the memory allocation of WeChat this time is blocked synchronously at the memory allocation because it needs to wait for the completion of this memory recycling before the allocation can be completed.
[0118] For example, when the user is using the WeChat application, WeChat needs to allocate a continuous 16KB of memory (order is 2, that is, 2 to the power of 2 pages, and each page is 4KB). If the free memory of the system is 20M at this time, and assuming the set low watermark and high watermark of the system are 40MB and 100MB respectively, then because the free memory is lower than the set low watermark of 40MB, the system will wake up kswapd to perform memory recycling until the free memory is not lower than the high watermark of 100MB and there is free continuous 16KB of memory.
[0119] Even if the free memory is higher than the high watermark of 100M, if due to memory fragmentation, the 100M of free memory is all scattered 4KB pages, it still cannot meet the current page memory, and memory recycling will still be carried out until there is continuous 16KB of memory for this demand.
[0120] The waterlines involved in the kswapd process are as Figure 5 shown, including: the min waterline 501, the low waterline 502, and the high waterline 503. If, at node 504, the free memory is lower than the low waterline, the kswapd process is awakened. At node 505, the free memory is between the low waterline and the min waterline, and the memory consumption speed slows down due to the kswapd process reclaiming memory in the background. If, at node 506, the free memory is lower than the min waterline, the memory allocation process simultaneously performs direct memory reclamation (directrealaim). If, at node 507, the free memory is higher than the high waterline, the kswapd process sleeps. Among them, the memory allocation process ends the memory reclamation when it receives memory that meets the current memory application requirements.
[0121] It should be noted that the free memory reclamation can stop when it reaches the high waterline, but there may be some continuous memory requirements. If there is still no memory of this continuous size available after reaching the high waterline, the free memory reclamation still cannot stop.
[0122] In the embodiments of this application, the process of the system triggering kswapd memory reclamation is as Figure 6 shown, including:
[0123] S601: Receive a memory allocation request;
[0124] S602: Allocate memory from the free memory;
[0125] Among them, before performing memory allocation, first determine whether the current free memory is lower than the low waterline. If the free memory is lower than the low waterline, the memory allocation fails. If the free memory is higher than the low waterline, memory is allocated from the free memory.
[0126] S603: Determine whether the memory allocation is successful?
[0127] If the memory allocation is successful, end the current memory allocation process; if the memory allocation fails, execute S604.
[0128] S604: Enter the memory allocation slow path.
[0129] S605: Wake up the kswapd process.
[0130] Perform memory reclamation based on the awakened kswapd process.
[0131] S606: If the free memory is not lower than the high waterline and there is memory of the required order size to meet the current needs, the kswapd process enters the sleep state.
[0132] In addition, apart from the kswapd thread, another path is that when free is less than the min watermark, the process of allocating memory will directly enter direct reclaim, that is, it will perform memory reclaim by itself. For example, assume that the WeChat process is allocating memory. At this time, the free memory is insufficient and less than the low watermark. It will wake up the kswapd thread to perform asynchronous memory reclaim, and then try again to see if the allocation is successful. If the allocation is still unsuccessful, it will also enter the memory reclaim path by itself, that is, direct reclaim.
[0133] The overall process of memory reclaim is as Figure 7 shown, including: the fast path of memory allocation 701, the slow path of memory allocation based on kswapd 702, and the slow path of memory allocation based on direct reclaim 703.
[0134] In the fast path of memory allocation 701, it includes:
[0135] S7011, receiving a memory allocation request;
[0136] S7012, allocating memory from free memory;
[0137] The allocation result includes successful memory allocation and failed memory allocation. If the memory allocation fails, it indicates that the free memory is less than the low watermark, and S7013 is executed.
[0138] S7013, entering the slow path of memory allocation.
[0139] Among them, first enter the slow path of memory allocation based on kswapd 702.
[0140] In the slow path of memory allocation based on kswapd 702, it includes:
[0141] S7021, waking up the kswapd process.
[0142] After waking up the kswapd process, the kswapd process performs memory reclaim.
[0143] S7022, trying to allocate memory again.
[0144] Trying to allocate memory again - judging whether the free memory meets the memory requirements. The result of the re - allocation is successful memory allocation and failed memory allocation. If the memory allocation fails, S7023 is executed.
[0145] S7023, the kswapd process synchronously tidies up fragmented memory.
[0146] While executing S7023, if the free memory is less than the min watermark, enter the slow path of memory allocation based on direct reclaim 703.
[0147] In the memory allocation slow path 703 based on direct recycling, it includes:
[0148] S7031. The process of allocating memory synchronizes memory recycling.
[0149] After the process of allocating memory synchronizes memory recycling, it attempts to allocate memory. The allocation results are successful memory allocation and failed memory allocation. If the memory allocation fails, then execute S7032.
[0150] S7032. The process of allocating memory synchronizes fragmented memory reorganization.
[0151] After the process of allocating memory synchronizes fragmented memory reorganization, it attempts to allocate memory. The allocation results are successful memory allocation and failed memory allocation. If the memory allocation fails, then execute S7033.
[0152] S7033. The process of allocating memory triggers COM to kill the process.
[0153] After triggering COM to kill the process, it attempts to allocate memory. The allocation results are successful memory allocation and failed memory allocation. If the memory allocation fails, then there is no displayed page.
[0154] In the above solution, there are the following disadvantages:
[0155] Disadvantage 1. It treats the memory recycling requests of processes with different priorities such as foreground and background processes without discrimination. For example, the background process continuously has the need to apply for memory, which may frequently trigger kswapd to perform memory recycling. Kswpad is relatively active and may even preempt the CPU with the foreground process, affecting the smooth experience of using the foreground application.
[0156] Disadvantage 2. Many processes apply for 1 memory page, that is, 4KB at a time, but the system mostly scans 32 pages, that is, 128KB for recycling each time; an obvious disadvantage on mobile terminals is that if the background process continuously has memory allocation behavior, it may continuously trigger memory recycling, then it may cause the pages of the foreground process to be more easily recycled and generate re-fault (reloading) when used again, affecting performance.
[0157] The embodiment of the present application provides a memory allocation method, and performs memory recycling through the following optimization points:
[0158] 1. Distinguish between foreground and background processes. Each APP has a uid when installed in Android. When switching the application to the foreground, the uid of the previous foreground is passed to the kernel. Let the kernel identify which processes belong to the foreground process, otherwise they are background processes.
[0159] Such as Figure 8As shown, when the application (APP) is running, the foreground APP UID is sent to the kernel through the Android Framework. The kernel stores the foreground APP UID in memory management, and the kernel checks whether the process is a foreground process based on the foreground APP UID stored in memory management.
[0160] 2. Reduce the kswapd load caused by background processes and reduce their CPU resource contention with foreground processes. When kswapd memory reclamation is triggered by a background process, the amount of memory reclaimed per scan by the kswapd process can be reduced, and at the same time, the kswapd process is restricted to the small CPU core. Conversely, if it is a foreground process, the amount of memory reclaimed per scan can be increased to reduce the probability of subsequent memory reclamation.
[0161] Among them, reducing the amount of memory reclaimed per scan of the integration can be understood as follows: for the kswapd memory reclamation triggered by a background process, instead of reclaiming 128KB per scan, for example, changing it to reclaim 4KB per time can reduce the load of waking up kswapd by the background process, and can also improve the situation where some memory that the foreground process may still need is easily reclaimed by scanning.
[0162] Restricting the memory reclamation thread of the kswapd process on the small CPU core mainly results in slower operation. The same task will be completed faster on the large core. However, if it is a background process, it does not affect the user experience, and it doesn't matter whether it is a little faster or slower. Instead, the CPU large core resources need to be ceded to the more needy foreground processes.
[0163] Increasing the amount of memory reclaimed per scan to reduce the probability of subsequent memory reclamation can be understood as follows: if a foreground process needs to reclaim 512KB of memory when allocating memory, if 128KB is reclaimed per scan, it needs to enter this path 4 times. But if it is increased to reclaim 128KB per time, it only needs to enter this memory reclamation path once. There is no need to repeat entering this path subsequently.
[0164] The specific technical implementation is to determine whether the currently triggered memory reclamation is a foreground process or a background process, and update different single - time memory scan values respectively.
[0165] 3. Throttle the memory reclamation of the kswapd process caused by background processes. For example, within a certain time window, if the memory reclaimed by the kswapd process triggered by a background process exceeds the set value, within this time window, kswapd no longer accepts the wake - up of unimportant background processes.
[0166] The memory reclamation method provided by the embodiments of this application can be as Figure 9 shown, including:
[0167] S901. The first process triggers the kswapd process to perform memory reclamation.
[0168] S902. Determine whether the first process is a foreground process.
[0169] If it is, execute S903; if not, the first process is a background process, and execute S904.
[0170] S903. Foreground recycling acceleration.
[0171] The ways of foreground recycling acceleration include:
[0172] S9031. Increase the amount of memory recycled in a single scan and S9032. Restrict the kswapd process to the big core of the CPU.
[0173] Among them, the increased amount of memory recycled in a single scan can be adjusted to twice the original amount of memory recycled in a single scan.
[0174] S904. Background recycling current limiting.
[0175] The background recycling current limiting includes one or more of S8041 to S8043:
[0176] S9041. Reduce the amount of memory recycled in a single scan;
[0177] The reduced amount of memory recycled in a single scan can be adjusted to 1 / N of the original amount of memory recycled in a single scan, where N>1.
[0178] S9042. Limit the amount of memory recycled by the kswapd process within a time window.
[0179] For example: the amount of memory recycled by the kswapd process within a 1s window is not greater than 600M.
[0180] S9043. Restrict the kswapd process to the small core of the CPU.
[0181] Among them, the processing power of the big core is higher than that of the small core. The processing power of the CPU core can be measured based on indicators such as processing speed and processing frequency.
[0182] The memory recycling method provided by the embodiments of the present application can obtain the following benefits:
[0183] 1). It is possible to distinguish priorities according to foreground and background in kernel memory management. Limit the memory load of the kswapd process awakened by background application processes, and improve the fluency experience of foreground applications.
[0184] 2). Dynamically adjust the amount of memory recycled by kswapd in a single scan according to the priority of the process that currently awakens kswapd memory recycling, reduce the refault caused by the premature recycling of foreground process memory by background processes, and improve the fluency experience of foreground application processes.
[0185] An electronic device according to an embodiment of the present application, such as Figure 10 shown, the electronic device 1000 includes:
[0186] A first judgment unit 1001, configured to judge whether the current free memory meets the memory recycling condition when receiving a memory application of a first process;
[0187] A second judgment unit 1002, configured to judge the running type of the first process when the current free memory meets the memory recycling condition, and the running type indicates whether the first process has a service interaction with the user using the service;
[0188] A control unit 1003, configured to control a second process to perform memory recycling based on the running type of the first process, and the second process is a process for performing memory recycling.
[0189] In some embodiments, the second judgment unit 1002 is further configured to:
[0190] Determine a first application identifier of a first application associated with the first process;
[0191] Compare the first application identifier with the application identifiers in a first list, and the application identifiers of the applications running on the display interface are stored in the first list;
[0192] Determine the running type of the first process according to the comparison result.
[0193] In some embodiments, the second judgment unit 1002 is further configured to:
[0194] When the application identifier in the first list includes the first application identifier, determine that the running type of the first process is a first running type, and the process of the first running type runs in the foreground;
[0195] When the application identifier in the first list does not include the first application identifier, determine that the running type of the first process is a second running type, and the process of the second running type runs in the background.
[0196] In some embodiments, the control unit 1003 is further configured to:
[0197] If the running type of the first process is the first running type, accelerate the memory recycling of the second process, and the process of the first running type runs in the foreground;
[0198] If the running type of the first process is the second running type, limit the speed of the memory recycling of the second process, and the process of the second running type runs in the background.
[0199] In some embodiments, the control unit 1003 is further configured to:
[0200] Run the second process in the first core of the processor, where the processing capability of the first core is stronger than that of the second core in the processor.
[0201] In some embodiments, the control unit 1003 is further configured to:
[0202] Adjust a set first scan reclaim amount to a second scan reclaim amount, where the second scan reclaim amount is greater than the first scan reclaim amount;
[0203] Control the second process to perform memory reclaim with the second scan reclaim amount as the single scan reclaim amount.
[0204] In some embodiments, the control unit 1003 is further configured to:
[0205] Estimate the second scan reclaim amount based on the memory application amounts of one or more processes of the first running type within a first period of time.
[0206] In some embodiments, the control unit 1003 is further configured to:
[0207] Run the second process in the second core of the processor, where the processing capability of the first core in the processor is stronger than that of the second core.
[0208] In some embodiments, the control unit 1003 is further configured to:
[0209] Adjust a set first scan reclaim amount to a third scan reclaim amount, where the third scan reclaim amount is less than the first scan reclaim amount;
[0210] Control the second process to perform memory reclaim with the third scan reclaim amount as the single scan reclaim amount.
[0211] In some embodiments, the control unit 1003 is further configured to:
[0212] Determine the memory reclaim amount of the second process;
[0213] When the memory reclaim amount is greater than or equal to a set reclaim threshold and the current time is within a set time window, control the second process to be in a sleep state.
[0214] In the embodiments of the present application, the above-mentioned first judgment unit, second judgment unit, and control unit can be implemented by a processor in an electronic device.
[0215] Those skilled in the art should understand that the relevant descriptions of the above terminal devices in the embodiments of the present application can be understood with reference to the relevant descriptions of the memory recycling method in the embodiments of the present application.
[0216] Figure 11 FIG. is a schematic structural diagram of an optional electronic device implemented as a terminal device provided by an embodiment of the present application. As Figure 11 shown, an embodiment of the present application provides an electronic device 1100, including an electronic chip 1101, and the electronic chip can be implemented as the memory recycling method described in one or more of the above embodiments.
[0217] An embodiment of the present application provides an electronic device. Figure 12 FIG. is a schematic structural diagram of another optional electronic device implemented as a terminal device provided by an embodiment of the present application. As Figure 12 shown, an embodiment of the present application provides an electronic device 1200, including:
[0218] a processor 1201 and a storage medium 1202 storing executable instructions of the processor 1201. The storage medium 1202 operates depending on the processor 1201 through a communication bus 1203. When the instructions are executed by the processor 1201, the memory recycling method executed in one or more of the above embodiments is executed.
[0219] It should be noted that in actual application, each component in the terminal is coupled together through a communication bus 1203. It can be understood that the communication bus 1203 is used to realize the connection and communication between these components. The communication bus 1203 includes not only a data bus, but also a power bus, a control bus, and a status signal bus. However, for the sake of clear illustration, in Figure 12 all kinds of buses are labeled as the communication bus 1203.
[0220] An embodiment of the present application provides a computer storage medium, and the computer-readable storage medium is used to store a computer program, and the computer program causes a computer to execute the steps of the memory recycling method described in one or more of the above embodiments.
[0221] Schematic structural diagram of an electronic device 1300 provided by an embodiment of the present application. Figure 13 The electronic device 1300 shown includes a processor 1310. The processor 1310 is configured to:
[0222] When receiving a memory application of a first process, determine whether the current free memory meets the memory recycling condition;
[0223] When the current free memory meets the memory recycling condition, determine the running type of the first process, and the running type indicates whether the first process has a service interaction with a user.
[0224] Based on the running type of the first process, control the second process to perform memory recycling, where the second process is the process for performing memory recycling.
[0225] In an embodiment of the present application, the processor 1310 may call and run a computer program from the memory to implement the voltage calibration method in the embodiment of the present application.
[0226] Optionally, as Figure 13 shown, the electronic device 1300 may further include a memory 1320. Among them, the processor 1310 may call and run a computer program from the memory 1320 to implement the voltage calibration method in the embodiment of the present application.
[0227] Among them, the memory 1320 may be a separate device independent of the processor 1310, or may be integrated in the processor 1310.
[0228] Optionally, as Figure 13 shown, the electronic device 1300 may further include a transceiver 1330. The processor 1310 may control the transceiver 1330 to communicate with other devices. Specifically, it may receive signals sent by other devices. Here, the transceiver may include at least two antennas.
[0229] It can be understood that the transceiver includes multiple physical paths for receiving or transmitting signals. The physical elements on one or more physical paths for transmitting signals constitute a transmitter.
[0230] Optionally, the electronic device 1300 may implement the corresponding processes implemented by the electronic device in each method of the embodiments of the present application. For the sake of brevity, details are not described herein again. It should be understood that the processor in the embodiments of the present application may be an integrated circuit chip with signal processing capabilities. In the implementation process, the steps of the above method embodiments may be completed by the integrated logic circuit in the hardware of the processor or by instructions in the form of software. The above processor may be a general-purpose processor, a digital signal processor (DSP), an application specific integrated circuit (ASIC), a field programmable gate array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components. It can implement or execute the various methods, steps, and logic block diagrams disclosed in the embodiments of the present application. The general-purpose processor may be a microprocessor or the processor may also be any conventional processor, etc. The steps of the method disclosed in combination with the embodiments of the present application may be directly embodied as being executed and completed by the hardware decoding processor, or by a combination of hardware and software modules in the decoding processor. The software module may be located in a mature storage medium in the art such as a random access memory, a flash memory, a read-only memory, a programmable read-only memory, or an electrically erasable programmable memory, a register, etc. The storage medium is located in the memory, and the processor reads the information in the memory and combines its hardware to complete the steps of the above method.
[0231] It can be understood that the memory in the embodiments of the present application can be a volatile memory or a non-volatile memory, or can include both volatile and non-volatile memories. Among them, the non-volatile memory can be a read-only memory (ROM), a programmable read-only memory (PROM), an erasable programmable read-only memory (EPROM), an electrically erasable programmable read-only memory (EEPROM), or a flash memory. The volatile memory can be a random access memory (RAM), which is used as an external cache. By way of example but not limitation, many forms of RAM are available, such as static random access memory (SRAM), dynamic random access memory (DRAM), synchronous dynamic random access memory (SDRAM), double data rate synchronous dynamic random access memory (DDR SDRAM), enhanced synchronous dynamic random access memory (ESDRAM), synchlink dynamic random access memory (SLDRAM), and direct rambus random access memory (DR RAM). It should be noted that the memory of the systems and methods described herein is intended to include but not be limited to these and any other suitable types of memory.
[0232] It should be understood that the above-mentioned memory is by way of example but not limitation. For example, the memory in the embodiments of the present application can also be a static random access memory (SRAM), a dynamic random access memory (DRAM), a synchronous dynamic random access memory (SDRAM), a double data rate SDRAM (DDR SDRAM), an enhanced SDRAM (ESDRAM), a synch link DRAM (SLDRAM), and a direct rambus random access memory (Direct Rambus RAM, DR RAM), and so on. That is to say, the memory in the embodiments of the present application is intended to include but not be limited to these and any other suitable types of memory.
[0233] The embodiments of the present application also provide a computer-readable storage medium for storing a computer program.
[0234] Optionally, the computer-readable storage medium can be applied to the terminal device in the embodiments of the present application, and the computer program enables the computer to execute the corresponding processes implemented by the terminal device in the various methods of the embodiments of the present application. For the sake of brevity, it will not be elaborated here.
[0235] The embodiments of the present application also provide a computer program product including computer program instructions.
[0236] Optionally, the computer program can be applied to the terminal device in the embodiments of the present application. When the computer program runs on the computer, it enables the computer to execute the corresponding processes implemented by the terminal device in the various methods of the embodiments of the present application. For the sake of brevity, it will not be elaborated here.
[0237] Those of ordinary skill in the art can realize that the units and algorithm steps of the examples described in combination with the embodiments disclosed herein can be implemented by electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are executed in a hardware or software manner depends on the specific application and design constraints of the technical solution. Professional technicians can use different methods for each specific application to implement the described functions, but such implementation should not be considered to exceed the scope of the present application.
[0238] Those skilled in the art can clearly understand that for the convenience and brevity of description, the specific working processes of the systems, devices, and units described above can refer to the corresponding processes in the foregoing method embodiments and will not be elaborated here.
[0239] In several embodiments provided by the present application, it should be understood that the disclosed systems, devices, and methods can be implemented in other ways. For example, the device embodiments described above are merely illustrative. For example, the division of the units is only a logical function division. In actual implementation, there may be other division methods. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the displayed or discussed couplings or direct couplings or communication connections to each other can be through some interfaces. The indirect couplings or communication connections of the devices or units can be in an electrical, mechanical, or other form.
[0240] The units described as separate components may or may not be physically separated. The components displayed as units may or may not be physical units, that is, they can be located in one place, or distributed to multiple network units. Some or all of the units can be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0241] In addition, in each embodiment of the present application, each functional unit may be integrated into one processing unit, may exist physically alone for each unit, or two or more units may be integrated into one unit.
[0242] If the above-mentioned function is implemented in the form of a software functional unit and sold or used as an independent product, it may be stored in a computer-readable storage medium. Based on such an understanding, the technical solution of the present application, in essence, or the part that contributes to the prior art, or a part of the technical solution, may be embodied in the form of a software product. The computer software product is stored in a storage medium and includes several instructions for causing a computer device (which may be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the methods described in each embodiment of the present application. The foregoing storage medium includes: various media such as USB flash drives, mobile hard disks, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical discs that can store program codes.
[0243] The above is only the specific implementation manner of the present application, but the protection scope of the present application is not limited thereto. Any person skilled in the art within the technical scope disclosed by the present application can easily think of changes or substitutions, which should all be covered by the protection scope of the present application. Therefore, the protection scope of the present application should be subject to the protection scope of the claims.
Claims
1. A memory recycling method, characterized in that, The method includes: When a memory application of a first process is received, determining whether the current free memory meets the memory recovery condition; When the current free memory meets the memory recovery condition, determining the running type of the first process, where the running type indicates whether the first process has a service interaction with a user; Based on the running type of the first process, controlling a second process to perform memory recovery, where the second process is the process for performing memory recovery.
2. The method according to claim 1, wherein The determining the running type of the first process includes: Determining a first application identifier of a first application associated with the first process; Comparing the first application identifier with the application identifiers in a first list, where the first list stores the application identifiers of the applications running on a display interface; Determining the running type of the first process according to the comparison result.
3. The method according to claim 2, characterized in that The determining the running type of the first process according to the comparison result includes: When the application identifier in the first list includes the first application identifier, determining that the running type of the first process is a first running type, and the process of the first running type runs in the foreground; When the application identifier in the first list does not include the first application identifier, determining that the running type of the first process is a second running type, and the process of the second running type runs in the background.
4. The method according to any one of claims 1 to 3, characterized in that, The controlling the second process to perform memory recovery based on the running type of the first process includes: If the running type of the first process is the first running type, accelerating the memory recovery of the second process, where the process of the first running type runs in the foreground; If the running type of the first process is the second running type, limiting the speed of the memory recovery of the second process, where the process of the second running type runs in the foreground.
5. The method according to claim 4, characterized in that, The accelerating the memory recovery of the second process includes: Running the second process in a first core of a processor, where the processing capacity of the first core is stronger than that of a second core in the processor.
6. The method according to claim 4, wherein The accelerating the memory recovery of the second process includes: Adjusting a set first scan recovery amount to a second scan recovery amount, where the second scan recovery amount is greater than the first scan recovery amount; Controlling the second process to perform memory recovery with the second scan recovery amount as the single scan recovery amount.
7. The method according to claim 6, characterized in that, The method further includes: Estimating the second scan recovery amount based on the memory application amounts of one or more processes of the first running type within a first time.
8. The method according to claim 4, characterized in that, The limiting the speed of the memory recovery of the second process includes: Running the second process in a second core of a processor, where the processing capacity of a first core in the processor is stronger than that of the second core.
9. The method according to claim 4, characterized in that The limiting the speed of the memory recovery of the second process includes: Adjusting a set first scan recovery amount to a third scan recovery amount, where the third scan recovery amount is less than the first scan recovery amount; Controlling the second process to perform memory recovery with the third scan recovery amount as the single scan recovery amount.
10. The method according to claim 4, wherein The limiting the speed of the memory recovery of the second process includes: Determining the memory recovery amount of the second process; When the amount of memory reclaimed is greater than or equal to the set reclamation threshold and the current time is within the set time window, control the second process to be in a sleep state.
11. An electronic device, characterized in that, Including: A first judgment unit configured to judge whether the current free memory meets the memory reclamation condition when receiving a memory application of the first process; A second judgment unit configured to judge the running type of the first process when the current free memory meets the memory reclamation condition, where the running type indicates whether the first process has a service interaction with the user; A control unit configured to control the second process to perform memory reclamation based on the running type of the first process, where the second process is the process for performing memory reclamation.
12. An electronic device, comprising a processor, characterized in that, The processor is configured to: Judge whether the current free memory meets the memory reclamation condition when receiving a memory application of the first process; Judge the running type of the first process when the current free memory meets the memory reclamation condition, where the running type indicates whether the first process has a service interaction with the user; Control the second process to perform memory reclamation based on the running type of the first process, where the second process is the process for performing memory reclamation.
13. A terminal device, comprising a memory, a processor, and a computer program stored on the memory and executable on the processor, characterized in that, When the processor executes the computer program, it implements the steps in the memory reclamation method according to any one of claims 1 to 10.
14. A storage medium stores an executable program, characterized in that, When the executable program is executed by the processor, it implements the memory reclamation method according to any one of claims 1 to 10.