Memory reclamation method and apparatus, electronic device, and storage medium
By actively reclaiming memory when the processor is idle, the problem of traditional memory reclamation mechanisms failing to provide memory in a timely manner is solved, achieving efficient memory management and avoiding device lag and power waste.
Patent Information
- Authority / Receiving Office
- WO · WO
- Patent Type
- Applications
- Current Assignee / Owner
- REALME MOBILE TELECOMM SHENZHEN CO LTD
- Filing Date
- 2025-11-18
- Publication Date
- 2026-05-21
AI Technical Summary
In existing technologies, when electronic devices are experiencing memory shortages, the speed of memory reclamation is slower than the speed of memory allocation, causing device lag. Furthermore, traditional memory reclamation mechanisms can only be triggered when the free memory falls below the low watermark threshold, failing to provide sufficient memory in a timely manner.
By actively reclaiming memory in electronic devices using a memory reclamation thread when the processor is idle, the number of memory reclamation cycles is increased, memory utilization is improved, and device lag is avoided.
It improves memory reclamation efficiency, keeps electronic devices at a high level of free memory, avoids device lag, ensures smooth operation, and reduces power consumption.
Smart Images

Figure CN2025135630_21052026_PF_FP_ABST
Abstract
Description
Memory reclamation methods and devices, electronic devices, storage media
[0001] This application claims priority to Chinese Patent Application No. 202411648177.2, filed on November 18, 2024, entitled “Method and Apparatus for Memory Recycling, Electronic Device, Storage Medium”, the entire contents of which are incorporated herein by reference. Technical Field
[0002] This application relates to the field of memory reclamation technology, specifically to a memory reclamation method and apparatus, electronic device, and storage medium. Background Technology
[0003] Electronic devices require a certain amount of memory space to run various programs. If the memory space is insufficient, the electronic device will experience lag.
[0004] In order to maintain the smooth operation of electronic devices, how to manage the memory space of electronic devices and improve the efficiency of memory space reclamation is an optimization direction in the industry. Summary of the Invention
[0005] This application discloses a memory reclamation method, apparatus, electronic device, and storage medium, which can perform memory reclamation when the processor of the electronic device is idle, thereby improving the memory reclamation efficiency of the electronic device.
[0006] The first aspect of this application discloses a memory reclamation method applied to an electronic device, the method comprising:
[0007] Obtain the operating information of the electronic device, including the load of the processor of the electronic device;
[0008] If the processor is determined to be idle based on its load, the memory in the electronic device is reclaimed via a memory reclamation thread.
[0009] A second aspect of this application discloses a memory reclamation device applied to an electronic device, the device further comprising:
[0010] An acquisition unit is used to acquire the operating information of the electronic device, the operating information including the load of the processor of the electronic device;
[0011] A recycling unit is used to reclaim memory in the electronic device via a memory recycling thread when it is determined that the processor is in an idle state based on the processor's load.
[0012] A third aspect of this application discloses an electronic device, comprising:
[0013] Memory containing executable program code;
[0014] A processor coupled to the memory;
[0015] The processor calls the executable program code stored in the memory to execute the memory reclamation method disclosed in the first aspect of the embodiments of this application.
[0016] A fourth aspect of this application discloses a computer-readable storage medium storing a computer program, wherein the computer program causes a computer to execute the memory reclamation method disclosed in the first aspect of this application.
[0017] The fifth aspect of this application discloses a computer program product that, when run on a computer, causes the computer to perform some or all of the steps of any method of the first aspect of this application.
[0018] The sixth aspect of this application discloses an application publishing platform for publishing computer program products, wherein when the computer program products are run on a computer, the computer performs some or all of the steps of any one of the methods of the first aspect of this application. Attached Figure Description
[0019] To more clearly illustrate the technical solutions in the embodiments of this application, the drawings used in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0020] Figure 1 is a flowchart illustrating a memory reclamation method disclosed in an embodiment of this application;
[0021] Figure 2 is a flowchart illustrating another memory reclamation method disclosed in an embodiment of this application;
[0022] Figure 3 is a flowchart illustrating another memory reclamation method disclosed in an embodiment of this application;
[0023] Figure 4 is a flowchart illustrating another memory reclamation method disclosed in an embodiment of this application;
[0024] Figure 5 is a schematic diagram of a memory recycling device disclosed in an embodiment of this application;
[0025] Figure 6 is a schematic diagram of the structure of an electronic device disclosed in an embodiment of this application. Detailed Implementation
[0026] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0027] It should be noted that the terms "first," "second," "third," and "fourth," etc., used in the specification and claims of this application are used to distinguish different objects, not to describe a specific order. The terms "comprising" and "having," and any variations thereof, in the embodiments of this application are intended to cover non-exclusive inclusion. For example, a process, method, system, product, or device that includes a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to these processes, methods, products, or devices.
[0028] This application discloses a screenshot method and apparatus, electronic device, and computer-readable storage medium, which can capture real-time screenshot images including static and dynamic effects, thereby improving the user's screenshot experience.
[0029] The technical solution of this application will be described in detail below with reference to specific embodiments.
[0030] To more clearly introduce the memory reclamation method disclosed in the embodiments of this application, we will first introduce the memory reclamation technology in related technologies.
[0031] In related technologies, electronic devices typically reclaim memory using the kswapd reclamation mechanism. The kswapd mechanism is a kernel thread in the Linux kernel that performs memory reclamation. This mechanism works by waking up the kswapd thread when the free memory in the electronic device falls below the low watermark threshold, and continuing this process until the free memory in the electronic device rises above the high watermark threshold.
[0032] As can be seen in related technologies, electronic devices only reclaim memory when the free memory in the electronic device is below the low watermark threshold, meaning that the available free memory in the electronic device is scarce. In practice, it has been found that reclaiming memory only when free memory is scarce may result in the memory reclamation speed being slower than the memory allocation speed, leading to insufficient memory being provided to memory requesters in a timely manner, and consequently causing the electronic device to lag.
[0033] To address this, this application provides a memory reclamation method to overcome the technical deficiencies in related technologies. Optionally, this application can be applied to various electronic devices that require memory reclamation, including but not limited to: portable electronic devices such as mobile phones and tablets, wearable devices such as smartwatches and smart bracelets, or desktop devices such as desktop computers and desktop TVs, without limitation.
[0034] Optionally, the electronic device can obtain its own operational information, which may include the processor load. If the processor load indicates it is idle, the electronic device can reclaim memory using a memory reclamation thread. Therefore, in this embodiment, the electronic device can reclaim memory as soon as the processor is determined to be idle, increasing the number of memory reclamation cycles and maintaining a high level of free memory, rather than waiting until insufficient free memory is available. This improves memory reclamation efficiency and minimizes the risk of insufficient free memory, ensuring smooth operation of the electronic device.
[0035] For example, after the electronic device opens the first application, the second application, and the third application in sequence, each application is used for a certain period of time; then it exits directly to the desktop of the electronic device and remains idle. After a certain period of time, the electronic device determines that the processor is in an idle state based on the processor load, and then the electronic device can reclaim the memory in the electronic device through the memory reclamation thread.
[0036] Furthermore, when a fourth application is launched at this point, since the idle memory has already been reclaimed during the processor's idle time, there are fewer dropped frames and the startup time is shorter, thus improving the smoothness of the electronic device's operation.
[0037] Based on this, the memory reclamation method and apparatus, electronic device and storage medium disclosed in the embodiments of this application will be described below.
[0038] Please refer to Figure 1, which is a flowchart illustrating a memory reclamation method disclosed in an embodiment of this application. Optionally, this method can be applied to the electronic device described above, or other execution entities. This application embodiment uses an electronic device as an example for description and should not be construed as limiting the scope of this application embodiment. Optionally, the method may include the following steps:
[0039] 102. Obtain the operating information of the electronic device, including the load of the electronic device's processor.
[0040] In this embodiment of the application, the electronic device can acquire its operating information through the operating information acquisition module. Optionally, the operating information acquisition module may include a performance monitoring unit (PMU), HWiNFO, etc., which are not limited here.
[0041] In another embodiment, the electronic device can obtain the electronic device's operation log and obtain the electronic device's operation information through the log information recorded in the operation log.
[0042] In this embodiment of the application, the operating information of the electronic device may include: operating information of one or more hardware components in the electronic device, operating information of one or more applications, operating information of one or more functions, and memory space of free memory, etc., which are not limited here.
[0043] Optionally, one or more hardware components may include: a central processing unit (CPU), a graphics processing unit (GPU), a memory module, and a screen, etc., without limitation.
[0044] Optionally, one or more applications may include: various applications that are built into the electronic device by default, or applications downloaded and installed from the Internet, without limitation.
[0045] Optionally, one or more functions may include various functions that an electronic device can perform, such as memory recycling function, screen on / off control function, etc., without limitation.
[0046] In this embodiment, the processor load of the electronic device is used to represent the busy level of the electronic device. Optionally, the processor load can be represented by the number of threads currently running on the processor. For example, the processor load is 4 threads, 8 threads, etc., and is not limited here.
[0047] In another embodiment, the processor load can be represented by the percentage of the processor currently in use. Exemplarily, the processor load could be 30%, 60%, etc., which are not selected here.
[0048] 104. If the processor is determined to be idle based on its load, the memory in the electronic device can be reclaimed through the memory reclamation thread.
[0049] In this embodiment of the application, when the electronic device obtains the load of the processor, it can determine whether the processor is in an idle state based on the load of the processor of the electronic device.
[0050] If the processor is idle, electronic devices can reclaim memory through a memory reclamation thread, instead of waiting until there is not enough free memory to reclaim memory, thus improving the efficiency of memory reclamation.
[0051] Furthermore, the fact that the processor is in an idle state indicates that it has sufficient capacity to run the memory reclamation thread to perform memory reclamation tasks without affecting other tasks. Therefore, memory reclamation can be performed without causing electronic devices to lag, ensuring the smooth operation of electronic devices.
[0052] In one alternative embodiment, the way an electronic device determines that the processor is in an idle state may include determining whether the processor's load is less than a load threshold.
[0053] The load threshold can be set by developers based on extensive development experience, or by users based on actual usage needs. Typical values may include 40%, 50%, or 2 threads, 4 threads, etc., without limitation. Optionally, the load threshold can be less than half of the processor's full load. For example, assuming the processor's full load is 100%, then half is 50%, and the load threshold could be 35%, 40%, etc., which are below 50%, without limitation.
[0054] Optionally, if the processor's load is less than a load threshold, the electronic device can determine that the processor is in an idle state; if the processor's load is not less than a load threshold, the electronic device can determine that the processor is not in an idle state.
[0055] By implementing the above method, electronic devices can determine whether the processor is in an idle state by judging whether the current processor load is less than the load threshold. This method only requires collecting the processor load and performing a simple threshold comparison, which is easy to implement and thus reduces the difficulty and cost of implementing the method.
[0056] Optionally, the aforementioned memory reclamation thread may include: a thread created by the electronic device solely for performing memory reclamation tasks when the processor is idle; or,
[0057] The kswapd thread mentioned earlier; or,
[0058] This refers to a thread that can perform memory reclamation tasks when the processor is idle and when the free memory of the electronic device is less than the space threshold; no specific limitation is made here.
[0059] In one alternative embodiment, the electronic device may create a memory reclamation thread before reclaiming memory in the electronic device via a memory reclamation thread.
[0060] Optionally, the electronic device may run a memory reclamation thread on a first core in the processor; optionally, the processor may also include a second core. Optionally, the power consumption of the first core is less than that of the second core, and / or the logical computing power of the first core is lower than that of the second core.
[0061] Optionally, the first core can be one of the first N cores in the processor, and the second core can be one or more cores in the processor other than the first core, where N is a positive integer and is not limited here. For example, the first core can be 0 to 2 cores or 0 to 3 cores in the processor, and is not limited here.
[0062] Understandably, the large cores in a processor, which have higher power consumption and stronger logical computing capabilities, are more suitable for performing more complex tasks in electronic devices, such as image optimization and running software, in order to ensure the performance ceiling of electronic devices.
[0063] By implementing the above method, electronic devices can run the memory reclamation thread using smaller cores with lower power consumption and weaker logical computing capabilities. This avoids the situation where the memory reclamation thread preempts the larger cores in the processor, preventing the processor from executing other more complex tasks. This ensures that the processor has strong logical processing capabilities while performing memory reclamation.
[0064] In this embodiment, the method by which an electronic device reclaims memory through a memory reclamation thread includes: reclaiming anonymous pages through the memory reclamation thread. Anonymous pages can be data that does not exist in the form of files, including heap, stack, and data segments, etc., and are not limited thereto.
[0065] By implementing the methods disclosed in the above embodiments, the electronic device can obtain its operating information. This operating information may include the processor load. If the processor load indicates it is idle, the electronic device can reclaim memory using a memory reclamation thread. Therefore, in this embodiment, the electronic device can reclaim memory as soon as the processor is determined to be idle, increasing the number of memory reclamation cycles and maintaining a high level of free memory, rather than waiting until insufficient free memory is available. This improves memory reclamation efficiency and minimizes the possibility of insufficient free memory, ensuring smooth operation of the electronic device.
[0066] Please refer to Figure 2, which is a flowchart illustrating another memory reclamation method disclosed in this application. Optionally, this method can be applied to the electronic device described above, or other execution entities. This application uses an electronic device as an example for description and should not be construed as limiting the scope of this application. Optionally, the method may include the following steps:
[0067] 202. Obtain the operating information of the electronic device, including the processor load and the free memory space of the electronic device.
[0068] In this embodiment of the application, the operating information of the electronic device collected by the electronic device may further include: the memory space of the free memory of the electronic device.
[0069] Free memory can include unused memory in electronic devices.
[0070] 204. If the processor is determined to be in an idle state based on the processor load, and the free memory space of the electronic device is less than the space threshold, then the memory in the electronic device is reclaimed through the memory reclamation thread; wherein, the space threshold is greater than the low watermark threshold of memory.
[0071] In this embodiment of the application, it is considered that when the processor is in an idle state, there may be a situation where the memory space of the electronic device is already sufficient. Forcibly reclaiming memory at this time would waste the power consumption of the electronic device.
[0072] Optionally, if the electronic device determines that the processor is in an idle state based on the processor's load, it can further determine whether the free memory space of the electronic device is less than a space threshold.
[0073] If the free memory space of an electronic device is less than the space threshold, it means that the current free memory space of the electronic device is not sufficient. In this case, the memory in the electronic device can be reclaimed through the memory reclamation thread.
[0074] It should be noted that the space threshold can be a low-level value of memory space set by the developer or user, and typical values may include: 200MB, 250MB, etc.
[0075] However, optionally, the space threshold can be greater than the memory low-water mark threshold. The low-water mark threshold is the minimum memory threshold required to trigger other reclamation mechanisms. For example, the low-water mark threshold can be the low-water mark threshold (low water level) that triggers the kswapd reclamation mechanism, as described earlier.
[0076] As mentioned earlier, when the free memory in an electronic device falls below the low watermark threshold of other reclamation mechanisms (such as kswapd) and triggers memory reclamation, the available free memory in the electronic device is already very scarce. However, in this embodiment, the space threshold can be greater than the low watermark threshold of the memory. This means that memory reclamation can be triggered before other memory reclamation mechanisms are triggered and before the free memory in the electronic device is very scarce. This can minimize the possibility of memory reclamation only occurring when the available free memory in the electronic device is scarce, leading to insufficient memory being provided to memory requesters and causing the electronic device to lag.
[0077] By implementing the above methods, electronic devices can perform memory reclamation only when the available free memory space is not sufficient, avoiding the situation where memory reclamation is forcibly performed when there is sufficient free memory, which would waste the power of the electronic device. In addition, memory reclamation can be triggered when other memory reclamation mechanisms have not yet triggered memory reclamation and the free memory in the electronic device is not yet scarce, avoiding the situation where the electronic device cannot provide enough memory to the memory requester in time due to the memory reclamation speed being slower than the memory request speed, thus preventing the electronic device from lag.
[0078] In another alternative embodiment, if it is determined from the processor load that the processor is not in an idle state, and / or the memory space of the electronic device's free memory is greater than or equal to a space threshold, the electronic device may execute step 202 again after waiting for a first duration.
[0079] Optionally, the first duration can be set by the developers based on extensive development experience, or by the users based on actual usage needs. Typical values may include 5 seconds, 6 seconds, etc., and are not limited here.
[0080] Optionally, during the initial waiting period, the electronic device can control the memory reclamation thread to enter a sleep state and then wake it up again after the initial waiting period. This can prevent the memory reclamation thread from preempting the processor during the waiting period, thereby improving the processor's utilization efficiency.
[0081] By implementing the above method, when an electronic device determines that it does not meet the conditions for memory reclamation based on the processor load and the available memory, it will wait for a certain period of time before reclaiming memory. This can avoid frequently triggering inefficient memory reclamation, thereby reducing the computational load and power consumption of the electronic device.
[0082] In an optional embodiment, the operating information of the electronic device may further include: the on / off state of the electronic device's memory reclamation function, and the on / off state of the electronic device's screen.
[0083] The memory reclamation function refers to the function of reclaiming memory through a memory reclamation thread. The on / off state of the memory reclamation function can include an "on" state and a "off" state. An "on" state means the electronic device allows memory reclamation through the memory reclamation thread; a "off" state means the electronic device does not allow memory reclamation through the memory reclamation thread. Optionally, the on / off state of the memory reclamation function can be switched by the user according to actual usage needs, or it can be switched automatically by the electronic device; this is not limited here.
[0084] Optionally, the on / off state of the screen of an electronic device may include a screen-on state and a screen-off state; wherein, the screen-on state refers to the state in which the screen of the electronic device is turned on, and the screen-off state may refer to the state in which the screen of the electronic device is turned off, without limitation.
[0085] Optionally, before the electronic device reclaims memory through the memory reclamation thread, if it is determined from the electronic device's operating information that the memory reclamation function of the electronic device is enabled and the screen of the electronic device is on, the memory reclamation thread of the electronic device can be woken up.
[0086] Understandably, if the electronic device's memory reclamation function is enabled, it means that memory reclamation is allowed through the memory reclamation thread; and if the screen of the electronic device is on, it means that the user is likely currently using the electronic device. Implementing the above method ensures that memory reclamation is only activated when the memory reclamation thread is enabled and the user is likely currently using the electronic device, thus confirming the need for memory reclamation and improving the accuracy of activating the memory reclamation thread.
[0087] In another alternative embodiment, if it is determined, based on the operating information of the electronic device, that the memory reclamation function of the electronic device is in a disabled state, and / or the screen of the electronic device is in a screen-off state, the electronic device can control the memory reclamation thread to yield the processor.
[0088] It is understandable that threads in electronic devices (including memory reclamation threads) run in the processor. Considering that the number of threads that a processor can execute in parallel at the same time is limited, when it is determined that the memory reclamation function of the electronic device is in a disabled state and / or the screen of the electronic device is in a screen-off state, that is, the electronic device will not wake up the memory reclamation thread, the memory reclamation thread is controlled to actively yield the processor so that other threads can use the processor, thereby improving the processor utilization efficiency.
[0089] As an optional implementation, if the electronic device determines that the processor is in an idle state based on the processor's load, it can further obtain the charging status information of the electronic device.
[0090] Optionally, if the electronic device is charging, the memory in the electronic device can be reclaimed through a memory reclamation thread.
[0091] Understandably, the processor may only be idle for a short time, and the user may continue to use the electronic device. In this case, prematurely reclaiming memory may accidentally close some programs or functions that the user is using.
[0092] If the processor is idle and it is further determined that the electronic device is charging, then the user is most likely not using the phone normally, and the processor will likely remain idle for a period of time. In this case, the memory in the electronic device can be reclaimed by a memory reclamation thread, which can prevent accidentally closing some programs or functions that the user is using, improve the intelligence and flexibility of the method, and improve the user experience.
[0093] In another alternative implementation, if the electronic device is not charging, the remaining battery power of the electronic device is obtained; if the remaining battery power is less than the battery power threshold, the memory in the electronic device is not reclaimed through the memory reclamation thread.
[0094] If the remaining battery power is greater than or equal to the battery power threshold, the memory in the electronic device will be reclaimed through the memory reclamation thread.
[0095] It is understandable that reclaiming memory in electronic devices through the memory reclamation thread consumes the device's power. Therefore, when the remaining power is low, memory reclamation can be avoided, thereby improving the battery life of electronic devices.
[0096] By implementing the methods disclosed in the above embodiments, the electronic device can obtain the operating information of the electronic device; wherein, the operating information of the electronic device may include the load of the processor of the electronic device; and if it is determined that the processor is in an idle state based on the processor load, the electronic device can reclaim the memory in the electronic device through a memory reclamation thread. As can be seen, in this embodiment, the electronic device can perform memory reclamation when the processor is determined to be idle, increasing the number of memory reclamation cycles and keeping the free memory of the electronic device at a high level, rather than waiting until the free memory is insufficient. This improves the efficiency of memory reclamation and minimizes the possibility of insufficient free memory, ensuring the smooth operation of the electronic device. Furthermore, memory reclamation can be performed only when the current free memory space is not very sufficient, avoiding forced memory reclamation when free memory is plentiful, which would waste the power of the electronic device. In addition, memory reclamation can be triggered when other memory reclamation mechanisms have not yet been triggered and the free memory in the electronic device is not yet strained, avoiding situations where the memory reclamation speed is slower than the memory allocation speed, resulting in insufficient memory being provided to the memory requester in time, thus causing the electronic device to lag. Finally, memory reclamation can be performed through a memory reclamation thread only when it is allowed and the user is likely currently using the electronic device, confirming the need for memory reclamation, thereby improving the accuracy of waking up the memory reclamation thread.
[0097] Please refer to Figure 3, which is a flowchart illustrating another memory reclamation method disclosed in this application. Optionally, this method can be applied to the electronic device described above, or other execution entities. This application uses an electronic device as an example for description and should not be construed as limiting the scope of this application. Optionally, the method may include the following steps:
[0098] 302. Obtain the operating information of the electronic device, including the processor load of the electronic device and the free memory space of the electronic device.
[0099] 304. If the processor is determined to be in an idle state based on the processor load, and the free memory space of the electronic device is less than the space threshold, then the memory in the electronic device is reclaimed through the memory reclamation thread.
[0100] In one optional embodiment, when the memory reclamation thread finishes its current memory reclamation, the electronic device can control the memory reclamation thread to enter a sleep state; and after waiting for a certain sleep time, the memory reclamation thread can be reawakened.
[0101] In other words, electronic devices can periodically wake up the memory reclamation thread to prevent the memory reclamation thread from occupying the processor even when it is not performing memory reclamation tasks, thereby improving the utilization efficiency of the processor.
[0102] Optionally, when the memory reclamation thread ends the current memory reclamation, the electronic device can also determine whether the reclamation space is less than the second reclamation threshold. The second reclamation threshold can be set by the developers based on extensive development experience or by the users based on actual usage needs. Typical values may include 256KB, 512KB, etc., and are not limited here.
[0103] Optionally, if the memory space reclaimed this time is less than the second reclamation threshold, the electronic device can increase the sleep duration of the memory reclamation thread in this sleep state.
[0104] Understandably, if the memory reclamation thread reclaims a small amount of memory in a given moment, it indicates that there is limited free memory available for reclamation in the electronic device, and a large amount of free memory is unlikely to become available again in the near future. To address this, the electronic device can increase the sleep duration of the memory reclamation thread when it is determined that the reclaimed memory is limited, thus avoiding frequent wake-ups for memory reclamation and reducing power consumption.
[0105] In one optional embodiment, the electronic device can increment a counter if it determines that the memory space reclaimed by the memory reclamation thread is less than a second reclamation threshold; and reset the counter if the reclaimed memory space is greater than or equal to the second reclamation threshold. The counter value represents the target number of consecutive memory reclamations by the memory reclamation thread that are less than the second reclamation threshold in the past.
[0106] In one embodiment, the electronic device can determine the sleep duration of the memory reclamation thread in this sleep state based on the target number of times.
[0107] Optionally, the electronic device can construct an exponential function with the first value as the base and the difference between the target number and the second value as the exponent, and calculate the calculation result corresponding to the exponential function; then, the calculation result is determined as the sleep duration of the memory reclamation thread in this sleep state.
[0108] The first and second values can be set by the developers based on extensive development experience. The first and second values can be the same or different, and there is no restriction here.
[0109] For example, assuming the first value is 2, the second value is 1, the target number of attempts is 3, and the sleep duration is in seconds; then the exponential function 2( 3-1 Therefore, we can obtain that the sleep duration of the memory reclamation thread in this sleep state is 4 seconds.
[0110] By implementing the above method, electronic devices can increase the sleep duration of the memory reclamation thread to a greater extent when there are many instances of historically fewer consecutive memory reclamations, thereby reducing the number of times the memory reclamation thread performs inefficient memory reclamation and thus saving power consumption of electronic devices. In addition, determining the sleep duration of the memory reclamation thread based on the target number of historical consecutive memory reclamations that are less than the second reclamation threshold also improves the flexibility of the method.
[0111] In another optional embodiment, after determining the sleep duration of the memory reclamation thread in the current sleep state based on the target number, the electronic device can determine whether the determined sleep duration of the memory reclamation thread in the current sleep state is greater than a third duration; the third duration can be the upper limit value of the sleep duration of the memory reclamation thread set by the developer or user, and typical values can include 5 minutes, 6 minutes, etc., which are not limited here.
[0112] Optionally, if the sleep duration of the memory reclamation thread in this sleep state is greater than the third duration, then the third duration can be determined as the sleep duration of the memory reclamation thread in this sleep state.
[0113] By implementing the above method, an upper limit can be set for the sleep duration of the memory reclamation thread, avoiding the situation where the memory reclamation thread sleeps for a long time and cannot perform memory reclamation work in a timely manner, thus leading to insufficient free memory in electronic devices. This improves the intelligence and compatibility of the method.
[0114] 306. If the currently reclaimed memory space is greater than or equal to the first reclamation threshold, then memory reclamation will stop.
[0115] In this embodiment of the application, during the process of reclaiming memory in the electronic device through the memory reclamation thread, the electronic device can determine whether the currently reclaimed memory space is greater than or equal to a first reclamation threshold. The first reclamation threshold can be set by the developers based on extensive development experience or by the users based on actual usage needs. Typical values may include 10M, 20M, etc., and are not limited here.
[0116] Optionally, if the currently reclaimed memory space is greater than or equal to the first reclamation threshold, the electronic device can stop reclamation of memory to avoid excessive memory reclamation, thereby preventing the interruption of some running programs or functions due to excessive memory reclamation, thus ensuring the normal operation of the electronic device.
[0117] In another embodiment, if the currently reclaimed memory space is less than the first reclamation threshold, the electronic device can determine whether it has already traversed all of its memory space; if not, the electronic device can reclaim the memory in the electronic device again through the memory reclamation thread.
[0118] In one alternative embodiment, the way an electronic device reclaims memory in the electronic device via a memory reclamation thread may include:
[0119] The electronic device determines whether there is locked memory in the memory of the electronic device. Locked memory is memory that cannot be reclaimed by the memory reclamation thread.
[0120] If it exists, the memory in the electronic device, excluding locked memory, will be reclaimed through the memory reclamation thread.
[0121] By implementing the above method, electronic devices can avoid reclaiming locked memory, thereby increasing the flexibility of the method.
[0122] Optionally, after the electronic device reclaims memory other than locked memory through the memory reclamation thread, if the currently reclaimed memory space is less than the first reclamation threshold, the electronic device can obtain the current usage status of the locked memory.
[0123] If the locked memory is currently unused, a request message can be output, which is used to request the reclaiming of the locked memory.
[0124] If a first instruction is detected that allows the reclamation of locked memory, the locked memory can be reclaimed through the memory reclamation thread.
[0125] By implementing the above method, electronic devices can request the reclamation of unused locked memory when the memory reclamation thread has only reclaimed a small amount of memory, thus improving the flexibility of the method.
[0126] By implementing the methods disclosed in the above embodiments, the electronic device can obtain its operating information; wherein, the operating information of the electronic device may include the load of the processor of the electronic device; furthermore, if it is determined that the processor is in an idle state based on the processor load, the electronic device can reclaim memory in the electronic device through a memory reclamation thread. It can be seen that, in this embodiment, the electronic device can perform memory reclamation when it is determined that the processor is in an idle state, increasing the number of memory reclamation operations and keeping the idle memory of the electronic device at a high level, rather than waiting until the idle memory is insufficient before performing memory reclamation, thereby improving the efficiency of memory reclamation and minimizing the possibility of insufficient idle memory, ensuring the smooth operation of the electronic device; and, if it is determined that the memory space reclaimed by the memory reclamation thread this time is relatively small, the electronic device can increase the sleep time of this memory reclamation thread to avoid frequently waking up the memory reclamation thread for memory reclamation, thereby reducing the power consumption of the electronic device; and, it can In cases where there have been many instances of consecutive memory reclamation with relatively low historical values, the sleep duration of the memory reclamation thread can be increased to reduce the number of inefficient memory reclamation operations, thereby saving power consumption of electronic devices. Furthermore, determining the sleep duration of the memory reclamation thread based on the target number of consecutive memory reclamation operations below the second reclamation threshold in the past improves the flexibility of this method. Additionally, if the currently reclamated memory space is greater than or equal to the first reclamation threshold, the electronic device can stop reclamation to avoid excessive memory reclamation, thus preventing interruptions to running programs or functions and ensuring the normal operation of the electronic device.
[0127] To more clearly describe the methods disclosed in the embodiments of this application, the following description is provided in conjunction with Figure 4, which is a schematic flowchart of another memory reclamation method disclosed in the embodiments of this application. Wherein:
[0128] 402. Create a memory reclamation thread and set the memory reclamation thread to run on the first core.
[0129] 404. Determine if the memory reclamation function is enabled; if yes, proceed to step 406; if no, proceed to step 408.
[0130] 406. Determine whether the screen of the electronic device is on; if yes, proceed to step 410; if no, proceed to step 408.
[0131] 408. Control the memory reclamation thread to yield the processor.
[0132] 410. Determine whether the processor load is less than the load threshold; if yes, proceed to step 412; if no, proceed to step 414.
[0133] 412. Determine whether the available memory space is less than the space threshold; if yes, proceed to step 416; if no, proceed to step 414.
[0134] 414. Control the memory reclamation thread to enter a sleep state, and re-execute step 404 after a certain sleep time.
[0135] 416. Traverse the memory space of the electronic device.
[0136] 418. Check again whether the free memory space is less than the space threshold; if so, proceed to step 420.
[0137] 420. Reclaim memory using a memory reclamation thread.
[0138] 422. Determine whether the memory space reclaimed this time is greater than or equal to the first reclamation threshold; if not, proceed to step 424.
[0139] 424. Determine whether the memory space of the electronic device has been traversed; if yes, proceed to step 426; if no, proceed to step 416.
[0140] 426. Determine whether the memory space reclaimed this time is less than the second reclamation threshold; if so, proceed to step 428.
[0141] 428. The sleep duration of the memory reclamation thread in this sleep state is determined based on the target number of consecutive memory reclamations in history that are less than the second reclamation threshold.
[0142] Please refer to Figure 5, which is a schematic diagram of a memory reclamation device disclosed in an embodiment of this application. Optionally, this device can be applied to the electronic device described above, or other execution entities. This application embodiment uses an electronic device as an example for description and should not be construed as limiting the scope of this application embodiment. Optionally, the device may include an acquisition unit 502 and a reclamation unit 504, wherein:
[0143] The acquisition unit 502 is used to acquire the operating information of the electronic device, including the load of the processor of the electronic device;
[0144] The recycling unit 504 is used to reclaim memory in an electronic device via a memory recycling thread when it is determined that the processor is in an idle state based on the processor's load.
[0145] By implementing the above-described apparatus, the electronic device can acquire its operational information, which may include the processor load. If the processor load indicates it is idle, the electronic device can reclaim memory using a memory reclamation thread. Therefore, in this embodiment, the electronic device can reclaim memory as soon as the processor is determined to be idle, increasing the number of memory reclamation cycles and maintaining a high level of free memory, rather than waiting until insufficient free memory is available. This improves memory reclamation efficiency and minimizes the risk of insufficient free memory, ensuring smooth operation of the electronic device.
[0146] As an optional implementation, the recycling unit 504 is also used to determine that the processor of the electronic device is in an idle state when the processor load is less than the load threshold, and to reclaim the memory in the electronic device through the memory recycling thread.
[0147] By implementing the above device, electronic devices can determine whether the processor is in an idle state by judging whether the current processor load is less than the load threshold. This method only requires collecting the processor load and performing a simple threshold comparison, which is easy to implement and thus reduces the difficulty and cost of implementing the method.
[0148] As an optional implementation, the operating information of the electronic device also includes: the memory space of the free memory of the electronic device; the recycling unit 504 is further configured to reclaim the memory in the electronic device through a memory recycling thread when it is determined that the processor is in an idle state based on the processor load and the memory space of the free memory of the electronic device is less than a space threshold, wherein the space threshold is greater than the low watermark threshold of the memory.
[0149] By implementing the above-mentioned device, electronic devices can perform memory reclamation only when the available free memory space is not sufficient, avoiding the situation where memory reclamation is forcibly performed when there is sufficient free memory, which would waste the power consumption of electronic devices. In addition, memory reclamation can also be triggered when other memory reclamation mechanisms have not yet triggered memory reclamation and the free memory in electronic devices is not yet scarce, avoiding the situation where the memory reclamation speed is slower than the memory allocation speed, which would prevent the electronic devices from being unable to provide enough memory to the memory requester in time, thus causing the electronic devices to lag.
[0150] As an optional implementation, the operating information of the electronic device also includes: the on / off state of the electronic device's memory reclamation function, and the on / off state of the electronic device's screen;
[0151] Furthermore, the device shown in Figure 5 may also include a wake-up unit (not shown), wherein:
[0152] The wake-up unit is used to wake up the memory reclamation thread of the electronic device before reclaiming memory in the electronic device through the memory reclamation thread. If the electronic device's memory reclamation function is enabled and the screen of the electronic device is on, based on the electronic device's operating information, the wake-up unit will wake up the memory reclamation thread of the electronic device.
[0153] By implementing the above device, memory reclamation can be performed through the memory reclamation thread when the user is likely to be using the electronic device and the electronic device is determined to have a need for memory reclamation, thereby improving the accuracy of waking up the memory reclamation thread.
[0154] As an optional implementation, the recycling unit 504 is also configured to reclaim memory in the electronic device through a memory recycling thread, and to stop reclaiming memory if the currently reclaimed memory space is greater than or equal to a first recycling threshold.
[0155] By implementing the above device, if the currently reclaimed memory space is greater than or equal to the first reclamation threshold, the electronic device can stop reclamation of memory to avoid excessive memory reclamation. This can prevent certain running programs or functions from being interrupted due to excessive memory reclamation, thereby ensuring the normal operation of the electronic device.
[0156] As an optional implementation, the recycling unit 504 is also used to control the memory recycling thread to enter a sleep state when the memory recycling thread ends the current memory recycling; and to increase the sleep duration of the memory recycling thread in the current sleep state if the memory space reclaimed this time is less than the second recycling threshold.
[0157] By implementing the above device, when the electronic device determines that the memory reclamation thread is reclaiming relatively little memory space, it can increase the sleep duration of the memory reclamation thread to avoid frequently waking up the memory reclamation thread to perform memory reclamation, thereby reducing the power consumption of the electronic device.
[0158] As an optional implementation, the sleep duration of the memory reclamation thread in this sleep state is determined based on the target number of consecutive memory reclamations in the past that are less than the second reclamation threshold.
[0159] By implementing the above-mentioned device, electronic devices can increase the sleep duration of the memory reclamation thread to a greater extent when there are many instances of historically fewer consecutive memory reclamations, thereby reducing the number of times the memory reclamation thread performs inefficient memory reclamation and thus saving power consumption of electronic devices. In addition, determining the sleep duration of the memory reclamation thread based on the target number of historical consecutive memory reclamations that are less than the second reclamation threshold also improves the flexibility of the method.
[0160] As an optional implementation, the apparatus shown in FIG5 may further include a creation unit (not shown), wherein:
[0161] A creation unit is used to create a memory reclamation thread before reclamation of memory in an electronic device is carried out by a memory reclamation thread; and to run the memory reclamation thread on a first core in a processor, the processor also including a second core, the first core having a lower power consumption than the second core.
[0162] By implementing the above device, electronic devices can run memory reclamation threads using small cores with lower power consumption and weaker logical computing capabilities. This avoids situations where the memory reclamation thread preempts the large cores in the processor, preventing the processor from executing other more complex tasks. This ensures that the processor has strong logical processing capabilities while performing memory reclamation.
[0163] Please refer to Figure 6, which is a schematic diagram of the structure of an electronic device disclosed in an embodiment of this application. As shown in Figure 6, the electronic device may include:
[0164] Memory 601 storing executable program code;
[0165] Processor 602 coupled to memory 601;
[0166] The processor 602 calls the executable program code stored in the memory 601 to execute the memory reclamation method disclosed in the above embodiments.
[0167] This application discloses a computer-readable storage medium storing a computer program that causes a computer to execute the memory reclamation methods disclosed in the above embodiments.
[0168] This application also discloses an application publishing platform, which is used to publish computer program products. When the computer program products are run on a computer, the computer performs some or all of the steps of the methods described in the above method embodiments.
[0169] It should be understood that the phrase "one embodiment" or "an embodiment" throughout the specification means that a specific feature, structure, or characteristic related to the embodiment is included in at least one embodiment of this application. Therefore, "in one embodiment" or "in an embodiment" appearing throughout the specification does not necessarily refer to the same embodiment. Furthermore, these specific features, structures, or characteristics can be combined in any suitable manner in one or more embodiments. Those skilled in the art should also recognize that the embodiments described in the specification are optional embodiments, and the actions and modules involved are not necessarily essential to this application.
[0170] In the various embodiments of this application, it should be understood that the sequence number of each process does not necessarily imply the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of this application.
[0171] The units described above as separate components may or may not be physically separate. The components shown as units may or may not be physical units; they can be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0172] 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.
[0173] If the aforementioned integrated units are implemented as software functional units and sold or used as independent products, they can be stored in a computer-accessible memory. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a memory and includes several requests to cause a computer device (which can be a personal computer, server, or network device, specifically a processor in the computer device) to execute some or all of the steps of the methods described in the various embodiments of this application.
[0174] Those skilled in the art will understand that all or part of the steps in the various methods of the above embodiments can be implemented by a program instructing related hardware. The program can be stored in a computer-readable storage medium, including read-only memory (ROM), random access memory (RAM), programmable read-only memory (PROM), erasable programmable read-only memory (EPROM), one-time programmable read-only memory (OTPROM), electrically-Erasable Programmable Read-Only Memory (EEPROM), compact disc read-only memory (CD-ROM) or other optical disc storage, disk storage, magnetic tape storage, or any other computer-readable medium capable of carrying or storing data.
[0175] The memory reclamation method, apparatus, electronic device, and storage medium disclosed in the embodiments of this application have been described in detail above. Specific examples have been used to illustrate the principles and implementation methods of this application. The descriptions of the above embodiments are only for the purpose of helping to understand the methods and core ideas of this application. At the same time, for those skilled in the art, there will be changes in the specific implementation methods and application scope based on the ideas of this application. Therefore, the content of this specification should not be construed as a limitation of this application.
Claims
1. A memory reclamation method, characterized by, Applied to electronic devices, the method includes: Obtain the operating information of the electronic device, including the load of the processor of the electronic device; If the processor is determined to be idle based on its load, the memory in the electronic device is reclaimed via a memory reclamation thread.
2. The method of claim 1, wherein, If the processor is determined to be in an idle state based on its load, the memory in the electronic device is reclaimed via a memory reclamation thread, including: If the processor's load is less than the load threshold, the processor of the electronic device is determined to be in an idle state, and the memory in the electronic device is reclaimed through the memory reclamation thread.
3. The method of claim 1, wherein, The operating information of the electronic device also includes: the memory space of the electronic device's free memory; If the processor is determined to be in an idle state based on its load, the memory in the electronic device is reclaimed via a memory reclamation thread, including: If the processor is determined to be in an idle state based on the processor's load, and the free memory space of the electronic device is less than a space threshold, then the memory in the electronic device is reclaimed through a memory reclamation thread, where the space threshold is greater than the low watermark threshold for memory.
4. The method of claim 1, wherein, The operating information of the electronic device also includes: the on / off state of the memory reclamation function of the electronic device, and the on / off state of the screen of the electronic device; And, prior to reclaiming memory in the electronic device via the memory reclamation thread, the method further includes: If, based on the operating information of the electronic device, it is determined that the memory reclamation function of the electronic device is enabled and the screen of the electronic device is on, then the memory reclamation thread of the electronic device is awakened.
5. The method of claim 1, wherein, The process of reclaiming memory in the electronic device via a memory reclamation thread includes: The memory in the electronic device is reclaimed through a memory reclamation thread, and memory reclamation is stopped if the currently reclaimed memory space is greater than or equal to a first reclamation threshold.
6. The method of claim 1, wherein, The process of reclaiming memory in the electronic device via a memory reclamation thread includes: When the memory reclamation thread finishes its current memory reclamation, it is controlled to enter a sleep state. If the memory space reclaimed this time is less than the second reclamation threshold, then the sleep duration of the memory reclamation thread in this sleep state will be increased.
7. The method of claim 6, wherein, The sleep duration of the memory reclamation thread in this sleep state is determined based on the target number of consecutive memory reclamations in the past that are less than the second reclamation threshold.
8. The method according to any one of claims 1 to 7, characterized in that, Prior to reclaiming memory in the electronic device via a memory reclamation thread, the method further includes: Create a memory reclamation thread; The memory reclamation thread runs on a first core in the processor, which also includes a second core, the power consumption of the first core being less than that of the second core.
9. The method of claim 1, wherein, The operating information of the electronic device also includes: the memory space of the electronic device's free memory; the method further includes: If it is determined from the processor's load that the processor is not in an idle state, and / or the free memory space of the electronic device is greater than or equal to a space threshold, then after waiting for a first duration, the step of obtaining the operating information of the electronic device is executed.
10. A memory reclamation apparatus, comprising: Applied to electronic devices, the device further includes: An acquisition unit is used to acquire the operating information of the electronic device, the operating information including the load of the processor of the electronic device; A memory reclamation unit is used to reclaim memory in the electronic device via a memory reclamation thread when it is determined that the processor is in an idle state based on the processor's load.
11. The apparatus of claim 10, wherein, The recycling unit is also used to determine that the processor of the electronic device is in an idle state when the processor load is less than the load threshold, and to reclaim the memory in the electronic device through a memory recycling thread.
12. The apparatus of claim 10, wherein, The operating information of the electronic device also includes: the memory space of the free memory of the electronic device; the recycling unit is further configured to reclaim the memory in the electronic device through a memory recycling thread when it is determined that the processor is in an idle state based on the processor's load and the memory space of the free memory of the electronic device is less than a space threshold, wherein the space threshold is greater than the low watermark threshold of the memory.
13. The apparatus of claim 10, wherein, The operating information of the electronic device also includes: the on / off state of the memory reclamation function of the electronic device, and the on / off state of the screen of the electronic device; the device also includes: The wake-up unit is used to wake up the memory reclamation thread of the electronic device if, based on the operating information of the electronic device, it is determined that the memory reclamation function of the electronic device is enabled and the screen of the electronic device is on, before the memory in the electronic device is reclaimed through the memory reclamation thread.
14. The apparatus of claim 10, wherein, The recycling unit is also used to reclaim memory in the electronic device through a memory recycling thread, and to stop reclaiming memory if the currently reclaimed memory space is greater than or equal to a first recycling threshold.
15. The apparatus of claim 10, wherein, The recycling unit is further configured to control the memory recycling thread to enter a sleep state when the memory recycling thread ends the current memory recycling; and to increase the sleep duration of the memory recycling thread in the current sleep state if the memory space reclaimed is less than the second recycling threshold.
16. The apparatus of claim 15, wherein, The sleep duration of the memory reclamation thread in this sleep state is determined based on the target number of consecutive memory reclamations in the past that are less than the second reclamation threshold.
17. The apparatus of any one of claims 10-16, wherein, The device further includes: A creation unit is configured to create a memory reclamation thread before reclamation of memory in the electronic device via the memory reclamation thread; and to run the memory reclamation thread on a first core in the processor, the processor further comprising a second core, the first core having a lower power consumption than the second core.
18. The apparatus of claim 10, wherein, The operating information of the electronic device also includes: the memory space of the free memory of the electronic device; the device is further configured to, after waiting for a first duration, execute the step of obtaining the operating information of the electronic device when it is determined from the load of the processor that the processor is not in an idle state, and / or the memory space of the free memory of the electronic device is greater than or equal to a space threshold.
19. An electronic device, comprising: The computer program product comprises a memory storing executable program code, and a processor coupled with the memory; wherein the processor invokes the executable program code stored in the memory to execute the method according to any one of claims 1-9.
20. A computer-readable storage medium storing a computer program, wherein the computer program comprises instructions that, when executed by a computer, cause the computer to perform the method of any one of claims 1-19. The computer program is executed by the processor to implement the method according to any one of claims 1-9.