Memory recovery method, electronic equipment and computer readable storage medium
By waking up the kswapd thread in a smartphone and calling it to recycle memory according to the CPU utilization, the problems of insufficient memory space and excessive computing resources are solved, and the stability and fluency of device performance are achieved.
Patent Information
- Application Number
- CN202311666736.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-12-06
- Publication Date
- 2025-06-13
AI Technical Summary
In smartphones, as tasks increase, the memory space is occupied, resulting in insufficient remaining available memory space, affecting device performance, and existing memory recycling methods may lead to excessive use of computing resources, resulting in lag and heat.
By waking up the sleeping kernel in the electronic device to swap the daemon kswapd thread, and calling the kswapd thread to recycle memory when the CPU utilization is less than or equal to the preset threshold, avoiding the use of additional computing resources when the CPU utilization is high.
It effectively avoids lag and heating caused by excessive use of computing resources, ensures stability of equipment performance, and improves fluency.
Smart Images

Figure CN120144265A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the technical field of terminals, and in particular, to a memory recycling method, an electronic device, and a computer-readable storage medium. Background Art
[0002] In the operating system of a smart phone, multiple tasks can run, which is a multi-task system. Multiple running tasks share the memory space in the smart phone, and the memory space stores the data required for task running.
[0003] Since the memory space is limited, as the number of running tasks increases, the occupied memory space will also increase. When the remaining available memory space is insufficient, the smart phone will recycle the occupied memory space to ensure the stable operation of the tasks in the current operating system. How the smart phone performs memory recycling has become an urgent problem to be solved. Summary of the Invention
[0004] In view of this, this application provides a memory recycling method, an electronic device, and a computer-readable storage medium, which can avoid affecting the performance of the electronic device when both memory resources and computing resources are relatively tight.
[0005] In a first aspect, this application provides a memory recycling method applied to an electronic device. The method includes: when the remaining available storage space of the electronic device meets the memory recycling condition, waking up the kernel swap daemon thread kswapd thread in the sleep state, and in response to the CPU utilization rate of the electronic device being less than a first preset threshold, calling the kswapd thread to recycle memory.
[0006] In some embodiments, when the remaining available storage space of the electronic device meets the memory recycling condition, the electronic device can first wake up the kernel swap daemon thread kswapd thread in the sleep state, then determine the CPU utilization rate of the central processing unit CPU of the electronic device, and in response to the CPU utilization rate of the electronic device being less than a first preset threshold, call the kswapd thread to recycle memory.
[0007] In still other embodiments, when the remaining available storage space of the electronic device is greater than a preset threshold C, it means that the remaining available storage space of the electronic device meets the memory recycling condition.
[0008] Optionally, when the CPU utilization rate of the electronic device is equal to the first preset threshold, the electronic device can call the kswapd thread to recycle memory.
[0009] In the above implementation, when the CPU utilization rate of the electronic device is less than the first preset threshold, the kswapd thread is called to reclaim memory. In this way, it can be avoided that when the current CPU utilization rate is relatively high, the electronic device calling the kswapd thread to reclaim memory will further occupy computing resources, thus preventing the CPU utilization rate from continuing to increase, and then effectively avoiding the phenomenon of the electronic device being stuck and overheating due to excessive occupation of computing resources.
[0010] In a possible implementation of the first aspect, the memory reclamation method further includes: in response to the CPU utilization rate of the electronic device being greater than the first preset threshold, killing the application programs in the electronic device. When the CPU utilization rate of the electronic device is greater than the first preset threshold, the electronic device may consider that calling the kswapd thread to reclaim memory at this time will cause the electronic device to become stuck. Therefore, the electronic device can kill the application programs running in the background in the electronic device, causing the application programs running in the background in the electronic device to terminate, thereby releasing the occupied memory space, and this process will not cause the CPU utilization rate to increase further.
[0011] Optionally, when the CPU utilization rate of the electronic device is equal to the first preset threshold and the electronic device does not call the kswapd thread to reclaim memory, the electronic device can kill the application programs in the electronic device.
[0012] In a possible implementation of the first aspect, the method further includes: in response to the CPU utilization rate of the electronic device being greater than the first preset threshold, controlling the kswapd thread to be in a sleep state. When the CPU utilization rate of the electronic device is greater than the first preset threshold, the electronic device may consider that calling the kswapd thread to reclaim memory at this time will cause the electronic device to become stuck. Therefore, the electronic device can control the kswapd thread to be in a sleep state, so that the kswapd thread does not occupy the computing resources of the electronic device, thereby preventing the CPU utilization rate from increasing and improving the fluency of the electronic device.
[0013] Optionally, when the CPU utilization rate of the electronic device is equal to the first preset threshold and the electronic device does not call the kswapd thread to reclaim memory, the electronic device can control the kswapd thread to be in a sleep state.
[0014] In a possible implementation of the first aspect, the kswapd thread supports reclaiming memory using multiple reclaiming methods. The multiple reclaiming methods include a first reclaiming method, and the computing resources occupied by the first reclaiming method are greater than those occupied by other reclaiming methods among the multiple reclaiming methods. When the CPU utilization rate is greater than a second preset threshold, the reclaiming method for the kswapd thread to reclaim memory does not include the first reclaiming method; when the CPU utilization rate is less than the second preset threshold, the reclaiming method for the kswapd thread to reclaim memory includes the first reclaiming method; wherein, the second preset threshold is less than the first preset threshold.
[0015] Optionally, when the CPU utilization rate is equal to the second preset threshold, the reclaiming method for the kswapd thread to reclaim memory does not include the first reclaiming method, or the reclaiming method for the kswapd thread to reclaim memory includes the first reclaiming method. When the CPU utilization rate is equal to the second preset threshold, whether the reclaiming method for the kswapd thread to reclaim memory includes the first reclaiming method can be specifically determined according to the actual application scenario.
[0016] In the above implementation, when the CPU utilization rate is greater than the second preset threshold, it indicates that the utilization rate of the electronic device is relatively high, and too many computing resources of the electronic device are occupied. At this time, the reclaiming method for the kswapd thread to reclaim memory does not include the first reclaiming method, which can effectively reduce the computing resources occupied by the operation of the kswapd thread, thereby avoiding the situation of the electronic device running stuck and getting hot. Correspondingly, when the CPU utilization rate is less than the second preset threshold, the reclaiming method for the kswapd thread to reclaim memory includes the first reclaiming method, which can effectively improve the reclaiming efficiency of the kswapd thread.
[0017] In a possible implementation of the first aspect, the kswapd thread supports reclaiming memory using multiple reclaiming methods. The multiple reclaiming methods include a first reclaiming method, and the computing resources occupied by the first reclaiming method are greater than those occupied by other reclaiming methods among the multiple reclaiming methods. The method further includes: in response to the CPU utilization rate of the electronic device being greater than the first preset threshold, calling the kswapd thread to reclaim memory; when the CPU utilization rate of the electronic device is greater than the first preset threshold, the reclaiming method for the kswapd thread to reclaim memory does not include the first reclaiming method.
[0018] In the above process, when the CPU utilization rate of the electronic device is greater than the first preset threshold, the electronic device can consider that the current CPU utilization rate of the electronic device is relatively high, that is, too many computing resources of the electronic device are occupied. Therefore, the electronic device calls the kswapd thread that does not include the first reclaiming method to implement memory reclaiming, which can effectively reduce the computing resources occupied by the operation of the kswapd thread, thereby avoiding the situation of the electronic device running stuck and getting hot.
[0019] In a possible implementation of the first aspect, the multiple recycling methods include memory page compression, writing to the swap partition, dirty page writeback, and direct release.
[0020] In some embodiments, the computing resources occupied by memory page compression are greater than those occupied by any one of the recycling methods of writing to the swap partition, dirty page writeback, and direct release. Therefore, the first recycling method described above can be memory page compression.
[0021] In a possible implementation of the first aspect, the method further includes: creating a kswapd thread.
[0022] In a possible implementation of the first aspect, the method further includes: obtaining the remaining available storage space of the internal memory of the electronic device; in response to the remaining available storage space being greater than a third preset threshold, controlling the kswapd thread to enter the sleep state.
[0023] In some examples, the third preset threshold is preset threshold C.
[0024] In the above embodiments, when the remaining available storage space of the internal memory of the electronic device is greater than the third preset threshold, it indicates that the remaining available storage space of the internal memory of the electronic device is sufficient to support the operation of other applications. Therefore, the electronic device can control the kswapd thread to enter the sleep state to reduce the occupation of computing resources by the kswapd thread.
[0025] In a possible implementation of the first aspect, the method further includes: determining that the current scenario of the electronic device is included in the whitelist; wherein, the whitelist is used to configure the application scenarios applicable to the memory recycling method.
[0026] When the current scenario of the electronic device is in the whitelist, the electronic device can apply the memory recycling method to implement memory recycling, thereby improving the performance of the electronic device.
[0027] In a possible implementation of the first aspect, killing applications in the electronic device includes: the electronic device calling the application framework layer of the electronic device to find background applications that meet the conditions, and the background applications that meet the conditions include at least one of the following: applications with a usage frequency lower than the threshold, applications with a priority lower than the threshold, and applications with a startup time lower than the threshold; releasing the memory occupied by the background applications that meet the conditions.
[0028] In a second aspect, the present application provides an electronic device, which includes a memory and one or more processors; the memory is coupled to the processor; the memory is used to store computer program code, and the computer program code includes computer instructions; when the processor executes the computer instructions, the electronic device is caused to execute the method described in the first aspect and any possible design thereof.
[0029] In a third aspect, the present application provides a computer-readable storage medium, including computer instructions, which when running on an electronic device, cause the electronic device to execute the method described in the first aspect and any possible design thereof above.
[0030] In a fourth aspect, the present application provides a computer program product, which when running on an electronic device, causes the electronic device to execute the method described in the first aspect and any possible design thereof above.
[0031] In a fifth aspect, the present application provides a device, which is included in an electronic device and has a function of implementing the behavior of the electronic device in any of the methods described in the first aspect and possible implementations above. This function can be implemented by hardware or by hardware executing corresponding software. The hardware or software includes at least one module or unit corresponding to the above function. For example, a calculation module or unit, an allocation module or unit, a recycling module or unit, a storage module or unit, etc.
[0032] In a sixth aspect, an embodiment of the present application provides a chip system, which includes a processor and may further include a memory for implementing any of the methods provided in the first aspect above. The chip system may be composed of chips or may include chips and other discrete devices.
[0033] It can be understood that the electronic device described in the second aspect and any possible design thereof above, the computer-readable storage medium described in the third aspect, and the computer program product described in the fourth aspect are all used to execute the corresponding methods provided above. Therefore, the beneficial effects that can be achieved can refer to the beneficial effects in the corresponding methods provided above, and will not be elaborated here. Description of the Drawings
[0034] Figure 1 It is a schematic diagram of the occupation of computing resources provided by an embodiment of the present application;
[0035] Figure 2 It is a schematic flowchart of a memory recycling method provided by an embodiment of the present application;
[0036] Figure 3 It is a schematic structural diagram of an electronic device provided by an embodiment of the present application;
[0037] Figure 4 It is a software structure block diagram of an electronic device provided by an embodiment of the present application;
[0038] Figure 5 It is a schematic flow chart of a memory recycling method provided by an embodiment of the present application Figure 1 ;
[0039] Figure 6 It is a comparison of computing resource occupancy provided by an embodiment of the present application Figure 1 ;
[0040] Figure 7 It is a schematic flow chart of a memory recycling method provided by an embodiment of the present application Figure 2 ;
[0041] Figure 8 It is a comparison of computing resource occupancy provided by an embodiment of the present application Figure 2 ;
[0042] Figure 9 It is a schematic flow chart of a memory recycling method provided by an embodiment of the present application Figure 3 。 Detailed implementation manners
[0043] Hereinafter, the terms "first" and "second" are only used for descriptive purposes and cannot be construed as indicating or implying relative importance or implicitly specifying the quantity of the indicated technical features. Thus, features defined with "first" and "second" may explicitly or implicitly include one or more of such features. In the description of this embodiment, "at least one" means one or more, and "a plurality" means two or more. "And / or" describes the association relationship of associated objects and indicates that three relationships may exist. For example, A and / or B may represent: A exists alone, A and B exist simultaneously, and B exists alone, where A and B may be singular or plural. The character " / " generally represents an "or" relationship between the associated objects before and after. "At least one (item)" or similar expressions thereof refer to any combination of these items, including any combination of single item (item) or plural items (items). For example, at least one (item) of a, b, or c may represent: a, b, c, a - b, a - c, b - c, or a - b - c, where a, b, and c may be single or multiple.
[0044] It should be noted that in the embodiments of the present application, words such as "exemplary" or "for example" are used to give examples, illustrations or explanations. Any embodiment or design solution described as "exemplary" or "for example" in the present application should not be construed as being more preferred or more advantageous than other embodiments or design solutions. Rather, the use of words such as "exemplary" or "for example" is intended to present relevant concepts in a specific manner.
[0045] To better understand the embodiments of the present application, the related technologies provided by the embodiments of the present application will be introduced first.
[0046] 1. Internal memory and external memory:
[0047] The internal memory can be used to provide the memory space required for an application to run during its operation in an electronic device. The external memory can be used to store the program files of various installed applications in the electronic device.
[0048] 2. Structure of the memory space:
[0049] The memory management subsystem uses a three-level structure of node, zone, and page to describe the memory space. Among them, the number of nodes can correspond to the number of cores of the central processing unit (CPU). A node is further divided into zones, and each zone is used to store pages with similar characteristics. A page is the smallest unit of the memory space, and the reclaimable object in the embodiments of the present application is also a page, which can be called a memory page.
[0050] 3. Computing resources:
[0051] Computing resources can refer to CPU resources. Generally, CPU utilization is used to represent the usage of CPU resources. CPU utilization represents the situation where CPU resources are occupied at any given time. Generally, the size of CPU utilization is measured in percentages. For example, a CPU utilization of 100% means that all CPU resources are occupied, and another example, a CPU utilization of 0 means that no CPU resources are occupied.
[0052] When an application is running, the electronic device can allocate an exclusive memory space for the application in the internal memory. Obviously, the more applications are running, the larger the memory space occupied in the internal memory. Since the available memory space in the internal memory of the electronic device is limited, when the remaining available memory space is insufficient, not only can the unstarted applications not be started normally because they cannot apply for the required memory space to run, but also the started applications may experience running lags because they cannot further apply for the required memory space to run.
[0053] In response to the above phenomenon, the electronic device monitors the remaining available memory space in the internal memory and triggers a memory recycling task when it detects that the remaining available memory space is insufficient.
[0054] Currently, common recycling methods mainly include: background memory recycling and direct memory recycling.
[0055] Among them, background memory recycling is asynchronous memory recycling. During the process of performing background memory recycling, the operation of other threads is not affected. In some scenarios, the memory recycling performed by the kernel swap daemon (kswapd) process is a type of background memory recycling method. Among them, since the kswapd process is directly started by the kernel itself, the kswapd process can actually be called a kernel thread. Therefore, it is also referred to as the kswapd thread in the following text. For example, when the electronic device is powered on, the kernel of the electronic device creates a kswapd thread corresponding to each node to recycle the memory pages in each node. Then, when the remaining available memory space in the internal memory is insufficient, the electronic device uses the kswapd thread to recycle the memory. The process of the kswapd thread recycling the memory is asynchronous and does not block the execution of other threads.
[0056] Among them, direct memory recycling is synchronous memory recycling. During the process of performing direct memory recycling, other threads will be blocked, that is, they cannot be scheduled by the CPU, which will affect their normal operation. For example, when an application installed in the electronic device is starting up, the related process of the application requests memory from the memory management in the kernel. At this time, when the remaining available memory space in the internal memory is less than the memory space requested by this process, direct memory recycling will be triggered.
[0057] Since direct memory recycling will cause the threads in other processes to be blocked and unable to operate normally, resulting in the problem of the electronic device becoming stuck. Therefore, when currently solving the problem of insufficient remaining available memory space in the internal memory, it is necessary to improve the recycling effect of the kswapd thread as much as possible to avoid triggering direct memory recycling.
[0058] Furthermore, direct memory recycling means that the electronic device directly recycles the occupied memory, regardless of whether the application corresponding to this memory is running in the foreground or background, nor whether the corresponding application is in a non-running state. Therefore, when direct memory recycling is triggered, the applications running on the electronic device may be affected. Therefore, when currently solving the problem of insufficient remaining available memory space in the internal memory, it is necessary to improve the recycling effect of the kswapd thread as much as possible to avoid triggering direct memory recycling.
[0059] However, during the operation of the kswapd thread, it needs to occupy the computing resources of the electronic device to complete the memory recycling process. That is, the process of implementing background memory recycling using the kswapd thread is regarded as a process of exchanging computing resources for memory resources. In the case where multiple application processes simultaneously request computing resources and memory resources, it is possible that both the computing resources and memory resources of the electronic device are very tight. When the remaining available memory space in the internal memory is insufficient, the electronic device wakes up the kswapd thread so that the kswapd thread occupies computing resources and starts memory recycling. Since the computing resources are also in a very tight situation and the operation of the kswapd thread requires computing resources, the operation of the kswapd thread will further exacerbate the tightness of the computing resources, resulting in an increase in the lag of the electronic device and worse performance.
[0060] As Figure 1 shown, when the kswapd thread of the electronic device is not woken up, Thread 1 and Thread 2 are already running on the CPU. At this time, when the kswapd thread is woken up so that the kswapd thread occupies computing resources to recycle memory, it can be seen that Figure 1 the remaining available computing resources in the shown CPU are insufficient. In this case, if the electronic device detects an operation by the user to open an application, such as detecting an operation where the user clicks on the icon corresponding to the navigation application, due to insufficient available computing resources, it will cause the problem that the electronic device cannot normally start the navigation application, or the electronic device lags when starting the navigation application, affecting the smooth operation of the electronic device and bringing a bad operation experience to the user.
[0061] Therefore, an embodiment of the present application provides a memory recycling method applied to an electronic device. When the remaining available memory space in the internal memory of the electronic device is insufficient, the electronic device can wake up the kswapd thread in the sleep state. After that, as Figure 2 shown, the electronic device can determine the current CPU utilization rate, and when the CPU utilization rate is less than a preset threshold A (i.e., the "first preset threshold" mentioned above), the electronic device calls the kswapd thread to recycle memory. In this way, it can be avoided that when the current CPU utilization rate is relatively high, the electronic device calling the kswapd thread to recycle memory will further occupy computing resources, thereby avoiding the continuous increase of the CPU utilization rate, and then effectively avoiding the lag phenomenon caused by the excessive occupation of computing resources by the electronic device.
[0062] Among them, when the current CPU utilization rate is greater than the preset threshold A, the electronic device can control the kswapd thread to enter the sleep state, thus avoiding the waste of computing resources caused by the long-term operation of the kswapd thread.
[0063] To better understand the embodiments of the present application, the electronic device provided by the embodiments of the present application will be introduced first.
[0064] The electronic device may specifically be an electronic device with computing functions such as a mobile phone, a tablet computer, a smart screen, a laptop computer, a vehicle-mounted device, a wearable device (such as a smart watch), an ultra-mobile personal computer (UMPC), a netbook, a personal digital assistant (PDA), an artificial intelligence device, etc. The embodiments of the present application do not limit the specific type of the electronic device or the operating system installed thereon.
[0065] Next, the hardware structure of the electronic device will be introduced.
[0066] Figure 3 FIG. shows a schematic structural diagram of an electronic device 100. The electronic device 100 may include a processor 110, an external memory interface 120, an internal memory 121, a universal serial bus (USB) interface 130, a charging management module 140, a power management module 141, a battery 142, an antenna 1, an antenna 2, a mobile communication module 150, a wireless communication module 160, an audio module 170, a speaker 170A, a receiver 170B, a microphone 170C, a headphone interface 170D, a sensor module 180, a button 190, a motor 191, an indicator 192, a camera 193, a display screen 194, and a subscriber identification module (SIM) card interface 195, etc.
[0067] It can be understood that the structure schematically shown in the embodiments of the present invention does not constitute a specific limitation on the electronic device 100. In other embodiments of the present application, the electronic device 100 may include more or fewer components than shown in the figure, or combine certain components, or split certain components, or have different component arrangements. The components shown in the figure may be implemented in hardware, software, or a combination of software and hardware.
[0068] The processor 110 may include one or more processing units. For example, the processor 110 may include a CPU, an application processor (AP), a modem processor, a graphics processing unit (GPU), an image signal processor (ISP), a controller, a memory, a video codec, a digital signal processor (DSP), a baseband processor, and / or a neural-network processing unit (NPU), etc. Among them, different processing units may be independent devices or integrated in one or more processors.
[0069] Among them, the controller may be the nerve center and command center of the electronic device 100. The controller may generate operation control signals according to the instruction operation code and timing signal to complete the control of fetching and executing instructions.
[0070] A memory may also be provided in the processor 110 for storing instructions and data. In some embodiments, the memory in the processor 110 is a cache memory. This memory may save the instructions or data that the processor 110 has just used or recycled. If the processor 110 needs to use the instruction or data again, it can be directly called from the memory. This avoids repeated accesses, reduces the waiting time of the processor 110, and thus improves the efficiency of the system.
[0071] The internal memory 121 may be used to store computer-executable program code, and the executable program code includes instructions. The processor 110 executes various functional applications and data processing of the electronic device 100 by running the instructions stored in the internal memory 121. The internal memory 121 may include a program storage area and a data storage area. Among them, the program storage area may store an operating system, application programs required for at least one function (such as a sound playback function, an image playback function, etc.). The data storage area may store data created during the use of the electronic device 100 (such as audio data, a phone book, etc.). In addition, the internal memory 121 may include high-speed random access memory and may also include non-volatile memory, such as at least one disk storage device, a flash memory device, a universal flash storage (UFS), etc.
[0072] Since the memory set in the processor 110 is a place where the processor 110 temporarily stores data, it holds data waiting to be processed or data that has already been processed. The data in the memory set in the processor 110 has a relatively high probability of being read or used by the processor 110. Therefore, as the processing progress of the processor 110 updates or replaces these data, the need to recycle the memory space in the memory set in the processor 110 is not high. The internal memory 121 stores computer-executable program codes involved during the operation of the application in the electronic device. The probability that most of the data stored in the internal memory 121 is read or used by the processor 110 is relatively low. Therefore, the background memory recycling method in the embodiments of this application is mainly proposed for the internal memory 121.
[0073] The external memory interface 120 can be used to connect an external memory card, such as a Micro SD card, to expand the storage capacity of the electronic device 100. The external memory card communicates with the processor 110 through the external memory interface 120 to implement the data storage function. For example, files such as music and videos are saved in the external memory card.
[0074] In some embodiments, the processor 110 may include one or more interfaces. The interfaces may include an inter-integrated circuit (I2C) interface, an inter-integrated circuit sound (I2S) interface, a pulse code modulation (PCM) interface, a universal asynchronous receiver / transmitter (UART) interface, a mobile industry processor interface (MIPI), a general-purpose input / output (GPIO) interface, a subscriber identity module (SIM) interface, and / or a universal serial bus (USB) interface, etc.
[0075] The charging management module 140 is used to receive a charging input from a charger. Among them, the charger can be a wireless charger or a wired charger. While the charging management module 140 charges the battery 142, it can also supply power to the electronic device through the power management module 141.
[0076] The power management module 141 is used to connect the battery 142, the charging management module 140, and the processor 110. The power management module 141 receives the inputs from the battery 142 and / or the charging management module 140, and supplies power to the processor 110, the internal memory 121, the external memory, the display screen 194, the camera 193, the wireless communication module 160, etc.
[0077] The wireless communication function of the electronic device 100 can be implemented by the antenna 1, the antenna 2, the mobile communication module 150, the wireless communication module 160, the modulation and demodulation processor, and the baseband processor, etc. In some embodiments, the antenna 1 of the electronic device 100 is coupled to the mobile communication module 150, and the antenna 2 is coupled to the wireless communication module 160, so that the electronic device 100 can communicate with the network and other devices through wireless communication technologies.
[0078] The antenna 1 and the antenna 2 are used to transmit and receive electromagnetic wave signals. Each antenna in the electronic device 100 can be used to cover a single or multiple communication frequency bands. Different antennas can also be multiplexed to improve the utilization rate of the antennas. In some other embodiments, the antenna can be used in combination with a tuning switch.
[0079] The mobile communication module 150 can provide solutions for wireless communications including 2G / 3G / 4G / 5G, etc. applied to the electronic device 100. The mobile communication module 150 can include at least one filter, switch, power amplifier, low noise amplifier (LNA), etc. The mobile communication module 150 can receive electromagnetic waves from the antenna 1, filter, amplify, etc. the received electromagnetic waves, and transmit them to the modulation and demodulation processor for demodulation.
[0080] The mobile communication module 150 can also amplify the signal modulated by the modulation and demodulation processor, and convert it into electromagnetic waves through the antenna 1 and radiate it out. In some embodiments, at least some functional modules of the mobile communication module 150 can be provided in the processor 110. In some embodiments, at least some functional modules of the mobile communication module 150 and at least some modules of the processor 110 can be provided in the same device.
[0081] The wireless communication module 160 may provide solutions for wireless communications applied to the electronic device 100, including wireless local area networks (WLANs) (such as wireless fidelity (Wi-Fi) networks), Bluetooth (BT), global navigation satellite systems (GNSSs), frequency modulation (FM), near field communication (NFC), infrared technology (IR), etc.
[0082] The wireless communication module 160 may be one or more devices integrating at least one communication processing module. The wireless communication module 160 receives electromagnetic waves via the antenna 2, performs frequency modulation and filtering processing on the electromagnetic wave signals, and sends the processed signals to the processor 110. The wireless communication module 160 may also receive the signals to be sent from the processor 110, perform frequency modulation and amplification on them, and convert them into electromagnetic waves through the antenna 2 for radiation.
[0083] The electronic device 100 realizes the display function through the GPU, the display screen 194, and the application processor, etc. The GPU is a microprocessor for image processing, connected to the display screen 194 and the application processor. The GPU is used to execute mathematical and geometric calculations for graphics rendering. The processor 110 may include one or more GPUs, which execute program instructions to generate or change the display information.
[0084] The display screen 194 is used to display images, videos, etc. The display screen 194 includes a display panel. In some embodiments, the electronic device 100 may include one or N display screens 194, where N is a positive integer greater than 1.
[0085] The electronic device 100 may realize the shooting function through the ISP, the camera 193, the video codec, the GPU, the display screen 194, and the application processor, etc.
[0086] The ISP is used to process the data fed back by the camera 193. The camera 193 is used to capture static images or videos. In some embodiments, the electronic device 100 may include one or N cameras 193, where N is a positive integer greater than 1.
[0087] The digital signal processor is used to process digital signals. In addition to processing digital image signals, it can also process other digital signals. For example, when the electronic device 100 selects a frequency point, the digital signal processor is used to perform Fourier transform on the frequency point energy, etc.
[0088] Video codecs are used to compress or decompress digital videos. The electronic device 100 can support one or more video codecs. In this way, the electronic device 100 can play or record videos in multiple encoding formats, such as: Moving Picture Experts Group (MPEG) 1, MPEG2, MPEG3, MPEG4, etc.
[0089] The NPU is a neural-network (NN) computing processor. By drawing on the structure of biological neural networks, such as the transmission pattern between human brain neurons, it can quickly process input information and can also continuously self-learn. Through the NPU, applications such as intelligent cognition of the electronic device 100 can be realized, such as: image recognition, face recognition, speech recognition, text understanding, etc.
[0090] The electronic device 100 can implement audio functions through the audio module 170, speaker 170A, receiver 170B, microphone 170C, headphone jack 170D, and application processor, etc. Such as music playback, recording, etc.
[0091] The audio module 170 is used to convert digital audio information into an analog audio signal for output, and is also used to convert analog audio input into digital audio signals. The speaker 170A is used to convert audio electrical signals into sound signals. The receiver 170B is used to convert audio electrical signals into sound signals. The microphone 170C is used to convert sound signals into electrical signals. The headphone jack 170D is used to connect wired headphones.
[0092] Among them, the sensor module 180 can include a pressure sensor, a gyroscope sensor, a barometric pressure sensor, a magnetic sensor, an acceleration sensor, a distance sensor, a proximity light sensor, a fingerprint sensor, a temperature sensor, a touch sensor, an ambient light sensor, a bone conduction sensor, etc.
[0093] The keys 190 include a power-on key, volume keys, etc. The keys 190 can be mechanical keys or touch keys.
[0094] The motor 191 can generate a vibration prompt.
[0095] The indicator 192 can be an indicator light, which can be used to indicate the charging state, battery level change, and can also be used to indicate messages, missed calls, notifications, etc.
[0096] The SIM card interface 195 is used to connect a SIM card. The electronic device 100 can support 1 or N SIM card interfaces, where N is a positive integer greater than 1.
[0097] The software system of the electronic device 100 may adopt a layered architecture, an event-driven architecture, a microkernel architecture, a microservices architecture, or a cloud architecture. In the embodiments of the present invention, taking the operating system with a layered architecture as an example, the software structure of the electronic device 100 will be exemplarily described.
[0098] The layered architecture divides the software into several layers, and each layer has a clear role and division of labor. The layers communicate with each other through software interfaces. In some embodiments, the operating system is divided into four layers, from top to bottom, namely the application layer, the application framework layer, the runtime and system libraries, and the kernel layer.
[0099] In some embodiments, Figure 4 is the software structure block diagram of the electronic device 100 in the embodiments of the present invention. It can be understood that Figure 4 only shows the part of the structure related to the background memory management method provided by the embodiments of the present application, Figure 4 The schematic diagram shown does not constitute a specific limitation on the electronic device 100. The electronic device 100 may include more or fewer structural components than those shown in the figure.
[0100] It should be noted that in the embodiments of the present application, an operating system is taken as an example for illustration. As long as the functions implemented by each functional module in other operating systems are similar to those of the embodiments of the present application, the solutions of the present application can also be implemented.
[0101] The application layer may include a series of application packages. As Figure 4 shown, the application packages may include applications such as a camera, a gallery, a calendar, a call, a map, a navigation, a WLAN, a Bluetooth, music, a video, a short message, etc.
[0102] The application framework layer provides application programming interfaces (APIs) and programming frameworks for the applications in the application layer. The application framework layer includes some predefined functions. For example, it may include a process manager, a whitelist management module, an application killing module, a phone manager, a notification manager, etc. The embodiments of the present application do not impose any restrictions on this.
[0103] Among them, the process manager is used to create processes and threads, and abort processes and threads.
[0104] The whitelist management module is configured with a whitelist. The whitelist management module is used to execute the process ① as shown in Figure 4 when the scenario specified in the whitelist occurs: send an enabling instruction to the memory recycling module to determine to enable the memory recycling method provided by the embodiments of the present application.
[0105] For example, in the case where the white list includes the scenario of starting a game application, if the white list management module monitors an event that a user touches the icon of the game application (abbreviated as the game application start event), it can determine that the current scenario is the scenario of starting the game application. Since the scenario of starting the game application is included in the white list, the white list management module can send an enable instruction to the memory recovery module, so that the memory recovery module determines to execute the memory recovery method provided by the embodiments of the present application, ensuring that the utilization rates of the internal memory and the CPU in the electronic device can provide conditions for the normal start of the game application. If the white list management module monitors an event that a user touches the icon of the calendar application (abbreviated as the calendar application start event), it can determine that the current scenario is the scenario of viewing the calendar. Since the white list management module determines that the scenario of viewing the calendar is not included in the white list, the memory recovery module does not execute the memory recovery method provided by the embodiments of the present application.
[0106] In some embodiments, the electronic device can manage the specific application programs included in the white list, or in other words, manage the specified scenarios in the white list, by adding or deleting the application package names of the application programs in the configuration file corresponding to the white list management module (i.e., the above-mentioned white list). After that, the white list management module can determine whether the application package name of the started application program is included in the white list. If the application package name of the currently started application program is included in the white list, it is determined that the memory recovery method provided by the embodiments of the present application takes effect, that is, the memory recovery module executes the memory recovery method provided by the embodiments of the present application. If the application package name of the currently started application program is not included in the white list, it is determined that the memory recovery method provided by the embodiments of the present application does not take effect, that is, the memory recovery module does not execute the memory recovery method provided by the embodiments of the present application.
[0107] In some embodiments, the white list management module can determine whether the memory recovery method provided by the embodiments of the present application takes effect or not by writing to the sysfs file (an interface provided by the virtual file system (system file system)).
[0108] In some embodiments, the application killing module is used to kill application programs. Killing an application program means terminating the running of an application program that is running in the foreground or background of the electronic device.
[0109] In some embodiments, the application killing module is used when the memory recovery module determines to achieve memory recovery through the application killing method (such as Figure 4In process ②), based on the interaction between all running applications and the user, determine the application that has the least impact on the user experience (e.g., the application that the user uses the least frequently) as the application to be killed, and close the application to be killed to achieve memory space recovery. For example, the electronic device is currently running a music application, a calendar application, and a social application. Among them, the social application is running in the foreground and interacting with the user; the music application is running in the background and playing music; the calendar application is also running in the background. Therefore, when performing application killing, the application killing module can close the calendar application that has less impact on the user experience to achieve memory space recovery.
[0110] In some other embodiments, the application killing module can also, when the memory recovery module determines that memory can be recovered through application killing (such as Figure 4 in process ②)), select the application to be killed according to the classification level of the applications running in the background. For example, message applications are the first type of applications, system service applications are the second type of applications, and other applications are the third type of applications. The killing level of the third type of applications is the highest, the killing level of the second type of applications is the second, and the killing level of the first type of applications is the lowest. When there are three types of applications running in the background on the electronic device, the application killing module can select the application to be killed from the third type of applications. When there are the first type of applications and the second type of applications running in the background on the electronic device, the application killing module can select the application to be killed from the second type of applications.
[0111] The kernel layer is the layer between the hardware and the software. Generally, the kernel layer can include common drivers such as a display driver, a camera driver, an audio driver, a sensor driver, etc. In the embodiments of the present application, as Figure 4 shown, the kernel layer includes a central processor pressure sensing module and a memory recovery module.
[0112] The central processor pressure sensing module is used to calculate the CPU utilization rate. In some examples, the central processor pressure sensing module can calculate the CPU utilization rate based on the windows-assist load tracing (WALT) algorithm.
[0113] The memory reclaim module is used to reclaim memory when the remaining available memory space in the internal memory of an electronic device is insufficient. In the memory reclaim module, there is a memory waterline stored, and the memory waterline includes a high waterline and a low waterline. The memory reclaim module can detect the size of the remaining available memory space in the internal memory. When the remaining available memory space is lower than the low waterline of the memory waterline, it indicates that the current remaining available memory space in the internal memory is relatively tight and there is a possibility of insufficiency. When the remaining available memory space is higher than the high waterline of the memory waterline, it indicates that the current remaining available memory space in the internal memory is sufficient and there is no possibility of insufficiency.
[0114] In some embodiments, when the remaining available memory space in the internal memory is insufficient, the memory reclaim module can reclaim memory by the method provided in the embodiments of the present application.
[0115] In some embodiments, the memory reclaim module may include a kswapd thread. When the remaining available memory space is lower than the low waterline of the memory waterline, the memory reclaim module can wake up the kswapd thread to execute the memory reclaim process, ensuring that the remaining available memory space can be restored to a level higher than the high waterline of the memory waterline, so that the electronic device can have enough memory space to run new application programs and ensure the smooth operation of the electronic device.
[0116] In some embodiments, during the process of the memory reclaim module performing memory reclaim, it first sends a pressure sensing instruction to the central processing unit pressure sensing module ( Figure 4 process ③). After that, the central processing unit pressure sensing module calculates the CPU utilization rate and sends the calculated CPU utilization rate to the memory reclaim module ( Figure 4 process ④). In this way, when the CPU utilization rate is greater than a preset threshold A, the memory reclaim module can control the kswapd thread to enter the sleep state, avoiding the problem that when the CPU utilization rate is relatively high, the kswapd thread further occupies the computing resources of the CPU, resulting in a further increase in the CPU utilization rate and a decline in the performance of the electronic device.
[0117] In some examples, the kernel layer may further include a kernel scheduling module for providing a call interface for one module to call another module. For example, the memory reclaim module can call the interface provided by the kernel scheduling module, such as a so-called cpu pressure sensing interface, to implement Figure 4 process ③ and process ④ to obtain the CPU utilization rate.
[0118] It should be noted that since the kernel call module is only used to provide an interface for facilitating data transmission between two modules, therefore, it is not shown in Figure 4 this figure.
[0119] The memory recovery method provided by the embodiments of the present application will be described below with reference to the accompanying drawings.
[0120] After the kswapd thread in the electronic device is created, it always exists in the electronic device. When the remaining available memory space in the internal memory of the electronic device is sufficient, the kswapd thread is in a dormant state, that is, a state of stopping running. At this time, the kswapd thread cannot be called to execute the memory recovery process. The electronic device can wake up the kswapd thread when the remaining available memory space in the internal memory of the electronic device is insufficient, that is, when the kswapd thread is in a running state. At this time, the kswapd thread can be called to execute the memory recovery process.
[0121] In some examples, there is a memory waterline preset in the electronic device. The electronic device can monitor the size of the remaining available memory space in the internal memory. When the remaining available memory space is lower than the low waterline of the memory waterline, it indicates that the current remaining available memory space in the internal memory is relatively tight and there is a possibility of insufficiency. At this time, the electronic device can wake up and call the kswapd thread to execute the memory recovery process to ensure that the remaining available memory space can be restored to a level higher than the high waterline of the memory waterline, so that the electronic device can have enough memory space to run new application programs and ensure the smooth operation of the electronic device.
[0122] In some scenarios, after the kswapd thread is awakened, the electronic device can execute the Figure 5 memory recovery method as shown. Figure 5 The flow chart of a memory recovery method provided by the embodiments of the present application Figure 1 is as follows. The memory recovery method includes:
[0123] Step S501: The electronic device determines the CPU utilization rate.
[0124] The electronic device can calculate the CPU utilization rate based on the WALT algorithm. The WALT algorithm predicts the CPU utilization rate of the current time window according to the CPU usage in the previous N consecutive time windows before the current time window, where N is a positive integer.
[0125] Optionally, the CPU can be a single-core CPU. Or, to improve the performance of the CPU, the CPU is designed as a multi-core architecture. A CPU can include multiple cores. The CPU is a multi-core CPU. The cores included in the CPU are independent of each other, and each core can process tasks in parallel. For example, one core can execute one task, while another core executes another different task. Therefore, the processing efficiency of the multi-core designed CPU is higher.
[0126] In some embodiments, when the CPU is a single-core CPU, the electronic device can directly calculate the CPU utilization rate of the CPU in each time window according to the following formula (1):
[0127]
[0128] Where U represents the utilization rate of the CPU within the window time T, T represents the time length of the time window, delta represents the running time of the tasks processed by the CPU on the CPU in this time window, cur freq represents the running frequency of the CPU, and maxfreq represents the highest frequency at which the CPU can run.
[0129] In the above formula, T represents the time length of the time window, and delta represents the running time of the tasks processed by the CPU on the CPU in this time window. That is to say, the value of delta is less than the value of T, and both the value of delta and the value of T are greater than 0. Therefore, the calculated value is greater than 0 and less than or equal to 1. cur freq represents the running frequency of the CPU, and the value of cur freq is greater than or equal to 0 and less than or equal to the value of max freq. Thus, the calculated value is greater than or equal to 0 and less than or equal to 1. Thus, 0 ≤ U ≤ 1.
[0130] After calculating the CPU utilization rate of each time window, the electronic device can predict the CPU utilization rate of the current time window based on the CPU utilization rates of multiple time windows before the current time window.
[0131] As an example, the average value of the CPU utilization rates of multiple time windows before the current time window can be used as the CPU utilization rate of the current time window, and the predicted CPU utilization rate of the current time window can be used as the CPU utilization rate of the single-core CPU. For example, the CPU utilization rates corresponding to five consecutive time windows are 50%, 60%, 70%, 65%, and 60% respectively. Then, for the sixth time window, that is, the CPU utilization rate corresponding to the current time window is (50% + 60% + 70% + 65% + 60%) / 5 = 61%. The electronic device can use 61% as the CPU utilization rate of the single-core CPU.
[0132] As another example, after predicting the CPU utilization rate corresponding to the current time window, the electronic device can select the CPU utilization rate with the largest value from the predicted CPU utilization rate corresponding to the current time window and the CPU utilization rate corresponding to the previous time window of the current time window as the CPU utilization rate of the single-core CPU. As shown in the above example, the CPU utilization rates corresponding to five consecutive time windows are 50%, 60%, 70%, 65%, and 60% respectively. The electronic device can calculate that the CPU utilization rate corresponding to the sixth time window is 61%. After that, the electronic device can obtain that the CPU utilization rate corresponding to the previous time window of the current time window is 60%. After comparison, the electronic device can determine 61% with a larger value as the CPU utilization rate of the single-core CPU.
[0133] In some embodiments, when the CPU is a multi-core CPU, the electronic device can calculate the utilization rate of each core respectively, and then determine the CPU utilization rate of the multi-core CPU according to the utilization rates of multiple cores. For example, the average value of the utilization rates of multiple cores can be used as the CPU utilization rate of the multi-core CPU.
[0134] Optionally, the utilization rate of the core can be calculated with reference to the above formula (1), which will not be elaborated here.
[0135] In this application, when the CPU is a multi-core CPU, the computing capabilities (or processing capabilities) of each core in the CPU may be different. The core with stronger computing power can be used to process tasks with heavier loads, thereby improving the processing efficiency, while the core with weaker computing power can be used to process tasks with lighter loads, thereby reducing the computing power consumption. In this way, the processing performance of the CPU can be further improved. In some examples, in order to distinguish cores with different computing capabilities, the cores included in the CPU can be divided into big cores, medium cores, and small cores based on the computing capabilities of the cores. Big cores usually have higher computing capabilities and faster computing speeds, medium cores are the second, with lower computing capabilities and computing speeds, and small cores have the lowest computing capabilities and computing speeds. The computing speed can refer to the amount of data processed per unit time, and the computing speed is in a direct proportional relationship with the computing power.
[0136] Taking the CPU of an electronic device composed of 3 small cores, 4 medium cores, and 1 big core as an example, the calculation process of the CPU utilization rate is described. If the electronic device calculates that the utilization rates of the three small cores are 81%, 90%, and 79% respectively, the utilization rates of the four medium cores are 84%, 91%, 80%, and 76% respectively, and the utilization rate of the big core is 85% based on the above formula (1) in the embodiment, then the utilization rate of the multi-core CPU can be (81% + 90% + 79% + 84% + 91% + 80% + 76% + 85%) / 8 = 83.25%.
[0137] Understandably, since the purpose of calculating the CPU utilization rate in the embodiments of the present application is to determine whether to use the kswapd thread to implement recycling, the cores participating in the utilization rate calculation can be the cores that allow the kswapd thread to run. For example, based on the example in the above example, the kswapd thread is prohibited from running on the large cores, so the electronic device can calculate the utilization rate of the multi-core CPU based on the utilization rates of the three small cores and the four medium cores. The calculated utilization rate of the multi-core CPU is (81% + 90% + 79% + 84% + 91% + 80% + 76%) / 7 = 83%.
[0138] The CPU utilization rate can represent the tension degree of the current computing resources of the electronic device. The closer the CPU utilization rate is to 100%, the higher the tension degree of the current computing resources of the electronic device. On the contrary, it indicates that the tension degree of the current computing resources of the electronic device is lower. Therefore, after calculating the CPU utilization rate, the electronic device can decide on the specific method for recycling memory. As shown in steps S502 and S503:
[0139] Step S502: In response to the CPU utilization rate being less than the preset threshold B (i.e., the "first preset threshold" mentioned above), the electronic device calls the kswapd thread to recycle memory.
[0140] The object that the electronic device uses the kswapd thread to recycle is the memory page. The specific recycling methods adopted include one or more of memory page compression, writing to the swap partition, dirty page writeback, and direct release.
[0141] Among them, memory page compression refers to the process in which the kswapd thread compresses multiple memory pages into one memory page. For example, compressing 3 pages into 1 page. Writing to the swap partition refers to the process in which the kswapd thread temporarily stores cold memory pages in the swap partition of the external memory. Cold memory pages include, but are not limited to, memory pages that the process does not need temporarily. Dirty page writeback refers to the process in which the kswapd thread writes back the dirty pages to the disk to make the dirty pages become clean pages, and then recycles the clean pages. Direct release refers to the process in which the kswapd thread releases some inactive memory pages in the memory.
[0142] When the CPU utilization rate is less than the preset threshold B, the electronic device can run the kswapd thread so that the kswapd thread uses one or more of the above-mentioned means to implement memory recycling. When the CPU utilization rate is greater than the preset utilization threshold B, the electronic device can adopt the following step S503 to achieve more efficient memory recycling.
[0143] Step S503: In response to the CPU utilization rate being greater than the preset threshold B, the electronic device kills the application programs.
[0144] In some embodiments, when the CPU utilization rate is greater than a preset threshold B, it indicates that the computing resources of the current electronic device are relatively tight. At this time, enabling the kswapd thread to reclaim memory will further exacerbate the tightness of the computing resources of the electronic device, resulting in the inability to run other new applications or affecting the currently running applications. Therefore, in order not to affect the operation of other applications, when the CPU utilization rate is greater than the preset threshold B, the electronic device can directly kill applications and release the memory occupied by the applications to be killed. The process of reclaiming memory by killing applications on the electronic device has high processing efficiency, so memory can be reclaimed without occupying or occupying very little computing resources.
[0145] In some other embodiments, when the CPU utilization rate is greater than the preset threshold B, the electronic device can also control the kswapd thread to enter the sleep state again to prevent the electronic device from occupying some computing resources due to running the kswapd thread.
[0146] It should be noted that when the CPU utilization rate is equal to the preset threshold B, the electronic device can execute step S502 described above, or the electronic device can also execute step S503 described above. Specifically, the corresponding execution steps of the electronic device should be set according to the needs of the actual application scenario, and the case where the CPU utilization rate is equal to the preset threshold B is not limited here.
[0147] In some embodiments, the electronic device can use the application killing module to implement application killing. The electronic device can kill applications that meet one or more of the following conditions: applications that are not frequently used by the user, applications with low priority, and applications that take a short time to start.
[0148] Among them, applications that are not frequently used by the user can refer to applications with a low number of uses or frequencies within a period of time, or applications that have not been used within a period of time, etc. Applications that are not frequently used by the user can be alternatively described as applications with a usage frequency lower than a threshold. Applications that are not frequently used by the user can also be alternatively described as the applications that the user has used the least recently. If an application has not been used by the user recently, the possibility of being used by the user in the future is also very small. Therefore, when memory needs to be reclaimed, the electronic device can select the application that has been used the least recently as the application to be killed to reclaim the memory space corresponding to the application to be killed.
[0149] In one example, the application killing module can determine the application that the user is least likely to use in the future (or the application that is not frequently used) as the application to be killed based on the interaction situation between all the applications running in the background and the user. The interaction situation may include, but is not limited to, the number of interactions and the interaction frequency. For example, when the number of interactions between the user and the application is lower than the threshold, or when the interaction frequency between the user and the application is lower than the threshold, the electronic device can determine that the application is the application that the user is least likely to use in the future.
[0150] In another example, the background applications are classified according to the impact on the user experience. The first category is the message application, the second category is the key system service application, and the third category is other applications. The electronic device can select the application to be killed in the order of the third category, the second category, and the first category. After the electronic device classifies the background applications according to the impact on the user experience, it can determine the application that the user is least likely to use in the future from the classified category groups as the application to be killed. For example, the electronic device classifies the background applications according to the impact on the user experience to obtain the first category group, the second category group, and the third category group. Then, the electronic device selects the application to be killed according to the least recently used (LRU) algorithm among the multiple applications included in the third category group.
[0151] For example, the electronic device calculates the duration between the time point when the multiple applications in the third category group were last used and the current time point, and arranges the multiple applications in the third category group in ascending order of the duration to obtain the LRU list. In this way, in the LRU list, the applications arranged near the tail of the LRU list were used earlier, and the applications near the head of the LRU list were used more recently. Then, when killing applications, selecting the applications to be killed from the tail of the LRU list can ensure that the applications that the user is least likely to use in the future are selected as the applications to be killed.
[0152] In yet another example, the application killing module can determine the application with a priority lower than the threshold as the application to be killed. Among them, the application with a low priority may refer to the application with a lower running priority. The running priority is related to the running order. The application with a lower running priority runs after the application with a higher running priority runs. Generally, the application with a low priority is an application that does not need to run.
[0153] Exemplarily, the electronic device can use the low memory killer daemon (lmkd) process to kill the processes or threads corresponding to the most unnecessary background applications running on the electronic device, so as to implement application killing.
[0154] In another example, the electronic device can directly select the application with a shorter startup time as the application to be killed. For example, the electronic device can directly select the application with a startup time lower than a threshold as the application to be killed. Among them, the application with a shorter startup time can refer to the application with a startup time less than or equal to a preset startup threshold. The startup time can include the sum of the time for loading the application and initializing the application. The preset startup threshold can be set as needed without limitation.
[0155] In some other examples, the electronic device can determine the application to be killed from two aspects: the usage frequency of the application and the startup time of the application. For example, the usage frequency of application M is high and the startup time is long, the usage frequency of application N is not high and the startup time is short, and the usage frequency of application O is low but the startup time is also long. At this time, the electronic device can select application N as the application to be killed. In this way, application killing can be achieved with minimal impact on user usage.
[0156] As Figure 6 shown, before the electronic device reclaims memory, thread 1 and thread 2 are running on the large core of the CPU, thread 3 and thread 4 are running on the middle core, and thread 5, thread 6, and thread 7 are running on the small core. When the electronic device determines that the CPU utilization rate is greater than the preset threshold B, the thread corresponding to the application to be killed determined by the electronic device is thread 2. As Figure 6 shown, after the electronic device kills the application to be killed, thread 2 no longer runs, and the computing resources and memory space it occupies are released. In this way, it can be ensured that both the computing resources and the memory space are sufficient. That is to say, through this process, while ensuring the fluency of the electronic device, the memory recovery of the electronic device can also be achieved.
[0157] In some embodiments, since the usage status of the memory space of the electronic device is constantly changing, before the electronic device uses the application killing module to implement application killing, it can also query the current remaining available memory space of the electronic device. If the current remaining available memory space is sufficient, that is, greater than the preset threshold C, the application killing module can not perform application killing. In this way, miskilling of applications can be avoided.
[0158] By such as Figure 5In the process shown, after the kswapd thread is awakened, it can be determined whether the electronic device should continue to run the kswapd thread to implement memory recycling through the CPU utilization rate, or whether it is more efficient to implement memory recycling through application killing.
[0159] In some scenarios, after the kswapd thread is awakened, the electronic device can also configure the memory recycling permission of the kswapd thread to limit the awakened kswapd thread to run in a reasonable or memory recycling manner that does not occupy too many computing resources. Specifically, this method can refer to Figure 7 shown. Figure 7 The flowchart of a memory recycling method provided by an embodiment of the present application Figure 2 This memory recycling method can be executed by Figure 3 the electronic device shown, and this method may include:
[0160] Step S701: The electronic device determines the CPU utilization rate.
[0161] Specifically, reference can be made to the embodiments related to step S501, which will not be elaborated here.
[0162] Step S702: The electronic device determines whether the CPU utilization rate is greater than a preset threshold D (i.e., the "first preset threshold" mentioned above).
[0163] In some embodiments, when the CPU utilization rate is greater than the preset threshold D, it indicates that more computing resources of the CPU are occupied. At this time, the computing efficiency of the CPU decreases, and then the operating performance of the electronic device will decrease. Moreover, when the currently remaining available memory space of the electronic device is tight, the electronic device needs to call the kswapd thread to implement memory recycling, and the process of the kswapd thread recycling memory will further occupy computing resources. To avoid the kswapd thread occupying too many computing resources, the electronic device can prohibit the kswapd thread from executing the recycling method that requires more computing resources.
[0164] In some examples, the kswapd thread supports multiple memory recycling methods. For example, the kswapd thread supports using recycling method 1, recycling method 2, recycling method 3, and recycling method 4 to recycle memory. Among them, the computing resources occupied by recycling method 4 (i.e., the "first recycling method" mentioned above) are greater than those occupied by any of recycling method 3, recycling method 2, and recycling method 1. Then, the electronic device can prohibit the kswapd thread from using recycling method 4 to recycle memory. In this way, the computing resources occupied by the kswapd thread for memory recycling can be reduced, thereby avoiding the situation where the electronic device becomes stuck or overheats when calling the kswapd thread to recycle memory when the computing resources are relatively tight.
[0165] Exemplarily, the recovery methods specifically adopted by the kswapd thread include one or more of memory page compression, writing to the swap partition, dirty page writeback, and direct release. Since the computing resources consumed by the kswapd thread to perform compression operations for memory recovery are relatively high compared to the computing resources consumed by the kswapd thread to perform operations such as writing to the swap partition, dirty page writeback, or direct release for memory recovery, the electronic device can execute step S703:
[0166] Step S703: The electronic device prohibits the kswapd thread from performing memory page compression operations.
[0167] Therefore, by prohibiting the kswapd thread from performing memory page compression operations, the electronic device can effectively reduce the computing resources consumed when the kswapd thread runs.
[0168] After that, the electronic device executes step S704:
[0169] Step S704: The electronic device calls the kswapd thread to recover memory.
[0170] Among them, when the electronic device executes step S704 after executing step S703, the kswapd thread is prohibited from performing memory page compression operations. The kswapd thread can perform memory recovery through other methods except memory page compression operations. For example, the electronic device can call the kswapd thread to perform memory recovery by writing to the swap partition, dirty page writeback, or direct release.
[0171] In some embodiments, after the electronic device executes step S702, when the CPU utilization rate is less than the preset threshold D, it indicates that less computing resources of the CPU are occupied. At this time, there are more remaining computing resources of the electronic device. Then, in this case, after the electronic device executes step S702, it can directly execute step S704. At this time, the kswapd thread is not prohibited from performing memory page compression operations.
[0172] It should be noted that in the above process, when the electronic device determines that the CPU utilization rate is equal to the preset threshold D, the electronic device can prohibit the kswapd thread from performing memory page compression operations, or the electronic device can also directly call the kswapd thread to recover memory.
[0173] In some examples, such as Figure 8As shown, when the operation of compressing memory pages by the kswapd thread is not prohibited, Thread 1, Thread 2, and the kswapd thread are running on the large core of the CPU, Thread 3 and Thread 4 are running on the medium core, and Thread 5, Thread 6, and Thread 7 are running on the small core. When the electronic device determines that the CPU utilization rate is greater than the preset threshold D, the electronic device disables the operation of the kswapd thread to compress memory pages. After that, the kswapd thread running on the large core is the one with the operation of compressing memory pages disabled. Obviously, the kswapd thread with the operation of compressing memory pages disabled occupies fewer computing resources than the kswapd thread without the operation of compressing memory pages disabled. Therefore, through the above method, when the remaining available memory space is insufficient, the electronic device can, when the remaining available computing resources are insufficient, achieve memory recycling through the kswapd thread with the operation of compressing memory pages disabled. In this way, while achieving memory recycling of the electronic device, it is possible to avoid the situation of the electronic device freezing due to insufficient computing resources and ensure the performance of the electronic device.
[0174] The following, as Figure 9 shown, Figure 9 is a schematic flowchart of a memory recycling method provided by an embodiment of the present application Figure 3 and this memory recycling method can be executed by the Figure 3 electronic device shown, including:
[0175] Step S901: The electronic device creates a kswapd thread.
[0176] In some embodiments, when the electronic device is powered on, it can create a kswapd thread for memory recycling. Exemplarily, the electronic device can create a kswapd thread for each zone, and each kswapd thread is used to recycle the memory in the corresponding zone. Or the electronic device can establish a kswapd thread for the entire internal memory or multiple zones, and this kswapd thread is used to recycle the memory in multiple zones of the internal memory.
[0177] Step S902: The electronic device controls the kswapd thread to enter the sleep state.
[0178] In some embodiments, when there is one kswapd thread corresponding to the internal memory, after the kswapd thread is created, in order to prevent the kswapd thread from running continuously in the electronic device and thus occupying the computing resources of the electronic device for a long time, therefore, after the kswapd thread is created and starts running, when there is a relatively large amount of remaining available memory space in the internal memory of the electronic device, the electronic device can determine that there is no need for the kswapd thread to implement memory recycling and control the kswapd thread to enter the sleep state, that is, execute S902, thereby reducing the occupation of unnecessary computing resources.
[0179] In some examples, when there is one kswapd thread corresponding to each zone, the electronic device checks the balance state of the remaining available memory space of each zone. If the remaining available memory space of the current zone is already in a balanced state, that is, the remaining available memory space of the current zone is higher than the high watermark, the electronic device can control the kswapd thread corresponding to this zone to enter the sleep state, that is, execute S902. If the remaining available memory space of the current zone is not in a balanced state, that is, the remaining available memory space of the current zone is lower than the high watermark, the electronic device can recycle memory through the kswapd thread corresponding to this zone.
[0180] In some other examples, when there is one kswapd thread corresponding to multiple zones, the electronic device checks the balance state of the remaining available memory space of each zone. If the remaining available memory space of all zones among the multiple zones is already in a balanced state, that is, the remaining available memory space of each zone among the multiple zones is higher than the high watermark, the electronic device can control the kswapd thread corresponding to these multiple zones to enter the sleep state, that is, execute S902. If there is any zone among the multiple zones whose remaining available memory space is not in a balanced state, that is, there is any zone among the multiple zones whose remaining available memory space is lower than the high watermark, the electronic device can recycle memory through the kswapd thread corresponding to the multiple zones.
[0181] In some other embodiments, after the electronic device creates the kswapd thread, it can also directly control the created kswapd thread to enter the sleep state, that is, execute step S902.
[0182] In still some other embodiments, the electronic device can control the kswapd thread to enter the sleep state according to a preset period T1, that is, after creating the kswapd thread, the electronic device can control the kswapd thread to enter the sleep state according to the preset period T1.
[0183] Step S903: The electronic device wakes up the kswapd thread.
[0184] In some embodiments, when there is one kswapd thread corresponding to the internal memory, if the remaining available memory space in the internal memory of the electronic device is less than the preset threshold C, the electronic device can wake up the kswapd thread, that is, execute S903.
[0185] In some other embodiments, when there is one kswapd thread corresponding to each zone, if the remaining available memory space of the current zone is not in a balanced state, that is, the remaining available memory space of the current zone is below the high water mark, the electronic device can wake up the kswapd thread corresponding to this zone, that is, execute S903.
[0186] In some other embodiments, the electronic device can control the kswapd thread to enter the sleep state according to the preset period T1. After the kswapd thread enters the sleep state, after a preset duration T2, the electronic device wakes up the kswapd thread. That is to say, the electronic device periodically executes step S902, and after the kswapd thread enters the sleep state, it executes step S903 after a predetermined duration. For example, the electronic device can control the kswapd thread to enter the sleep state every 50 ms. After the kswapd thread enters the sleep state, after 30 ms, the electronic device wakes up the kswapd thread.
[0187] In some embodiments, after the electronic device wakes up the kswapd thread, it can execute the following steps S904 and S905 to further confirm whether the electronic device needs to run the kswapd thread to reclaim memory, so as to avoid the situation of miswaking of the kswapd thread.
[0188] Step S904: The electronic device determines the current remaining available memory space.
[0189] Step S905: The electronic device determines whether the current remaining available memory space is less than the preset threshold C.
[0190] In some embodiments, since the size of the remaining available memory space in the internal memory of the electronic device changes with the operation of the electronic device, the following scenario may exist: when the remaining available memory space in the internal memory of the electronic device is less than the preset threshold C, the electronic device wakes up the kswapd thread. After the kswapd thread is woken up, since a task has ended its operation, a part of the memory space occupied in the internal memory of the electronic device is released, resulting in the remaining available memory space in the internal memory of the electronic device being not less than the preset threshold C. At this time, it indicates that the remaining available memory space in the internal memory of the electronic device is sufficient and there is no need to recycle memory anymore. To ensure that the electronic device does not continue to call the kswapd thread to recycle memory and occupy computing resources, the electronic device can re-control the kswapd thread to enter the sleep state at this time, that is, when the current remaining available memory space is not less than the preset threshold C, step S902 is re-executed.
[0191] In addition, through step S904 and step S905, the electronic device can also avoid the situation where the electronic device accidentally wakes up the kswapd thread and causes the computing resources to be occupied.
[0192] When the electronic device determines that the current remaining available memory space is less than the preset threshold C, it indicates that the remaining available memory space in the internal memory of the current electronic device is relatively small. The electronic device can perform the following memory recycling operations:
[0193] Step S906: The electronic device determines the CPU utilization rate.
[0194] Specifically, reference can be made to the embodiments related to step S501, which will not be elaborated here.
[0195] Step S907: The electronic device determines whether the CPU utilization rate is greater than the preset threshold E (i.e., the "first preset threshold" mentioned above).
[0196] When the CPU utilization rate is greater than the preset threshold E, it indicates that a relatively large amount of computing resources of the current electronic device are occupied. If the kswapd thread continues to run to perform the memory recycling operation, it will cause the computing resources of the electronic device to be further occupied and result in a lag problem. Therefore, the electronic device can perform the following step S908:
[0197] Step S908: The electronic device kills the application programs.
[0198] By killing the application programs, the memory space can be efficiently released. Specifically, reference can be made to the embodiments related to step S503, which will not be elaborated here.
[0199] In some embodiments, during the process of the electronic device detecting and killing an application, the kswapd thread can be controlled to enter the sleep state, and after the electronic device finishes step S908, it is determined whether the kswapd thread needs to be woken up based on the size of the remaining available memory space in the internal memory of the electronic device. In this way, the occupation of computing resources by the kswapd thread can be reduced, thereby ensuring the smoothness of the electronic device.
[0200] After step S907, when the electronic device determines that the CPU utilization rate is not greater than the preset threshold E, it indicates that the computing resources of the current electronic device are less occupied and there are more remaining computing resources. Invoking the kswapd thread by the electronic device to perform the memory recycling operation will not cause the situation that the remaining computing resources of the electronic device are too few.
[0201] In some embodiments, considering that the operation of the kswapd thread will occupy a certain amount of computing resources of the electronic device, there may be a situation where the electronic device invokes the kswapd thread to recycle memory, resulting in insufficient remaining computing resources of the electronic device to support the electronic device to run other applications. Therefore, the electronic device can ensure that the electronic device can both run the kswapd thread and have sufficient remaining computing resources for the electronic device to run other applications through the following process (step S909 - step S911).
[0202] Step S909: The electronic device determines whether the CPU utilization rate is greater than the preset threshold F (i.e., the "second preset threshold" mentioned above).
[0203] After the electronic device determines in step S907 that the CPU utilization rate is not greater than the preset threshold E, the electronic device then executes step S909. Therefore, the preset threshold E in step S907 is greater than the preset threshold F in step S909.
[0204] In step S907, when the electronic device determines that the CPU utilization rate is not greater than the preset threshold E, it indicates that the computing resources of the current electronic device are less occupied and there are more remaining computing resources. At this time, by the electronic device judging in step S909 whether the CPU utilization rate is greater than the preset threshold F, it can be further confirmed whether the load of the electronic device can still ensure that the electronic device has more remaining computing resources when running the kswapd thread, thereby ensuring the smoothness of the electronic device.
[0205] When it is determined that the CPU utilization rate is greater than the preset threshold F, it means that the remaining computing resources of the electronic device can run the kswapd thread, but after running the kswapd thread, the remaining computing resources are not enough to ensure that the electronic device can perform other tasks, that is, it cannot ensure the smoothness of the electronic device. At this time, the electronic device can execute step S910:
[0206] Step S910: The electronic device prohibits the kswapd thread from performing memory page compression operations.
[0207] Specifically, reference may be made to the embodiments related to step S703, which will not be elaborated here.
[0208] It can be understood that step S910 can also be to disable the kswapd thread from performing one or more other memory reclaiming means. For example, prohibiting the kswapd thread from performing dirty page writing operations, or prohibiting the kswapd thread from performing memory page compression operations and dirty page writing operations. All memory reclaiming means adopted by the kswapd thread can be prohibited, and other possibilities will not be elaborated here.
[0209] After the electronic device prohibits the kswapd thread from performing memory page compression operations, the electronic device will not consume a large amount of computing resources when running the kswapd thread again, that is, perform step S911:
[0210] Step S911: The electronic device invokes the kswapd thread to reclaim memory.
[0211] In some embodiments, when the CPU utilization rate is less than or equal to the preset threshold F, it indicates that the computing resources of the CPU are less occupied. At this time, the electronic device has more remaining computing resources. Then, when the electronic device determines in step S909 that the CPU utilization rate is less than or equal to the preset threshold F, it can directly perform step S911.
[0212] Exemplarily, the preset threshold E is 90%, and the preset threshold F is 80%. Taking the current situation where the kswapd thread is prohibited from running on the big cores and can only run on 3 small cores and 4 big cores as an example. When the average CPU utilization rate of the 7 cores (3 small cores and 4 big cores) of the electronic device is greater than 90%, the electronic device performs the operation of killing applications. When the average CPU utilization rate of the 7 cores (3 small cores and 4 big cores) of the electronic device is greater than 80% and less than or equal to 90%, the electronic device prohibits the kswapd thread from performing memory page compression operations. When the CPU utilization rate of the 7 cores (3 small cores and 4 big cores) of the electronic device is less than or equal to 80%, the electronic device can directly run the kswapd thread without any disabled operations.
[0213] It can be understood that the preset threshold E and the preset threshold F can be set according to the total computing resources of different electronic devices, the computing resources required for the kswapd thread to run, and the minimum amount of computing resources that need to be idle for the electronic device to run smoothly.
[0214] For example, if the total computing resources are 1000 MB, the computing resources required for the kswapd thread without any disabled operations to run are 150 MB, the computing resources required for the kswapd thread with the memory page compression operation disabled to run are 100 MB, and the minimum amount of idle computing resources required for the electronic device to run smoothly is 250 MB, then the threshold E can be set to 75% and the threshold F can be set to 65%. Then, when the CPU utilization rate of the electronic device is 76%, it can be known that the remaining computing resources of the electronic device are 240 MB, and at this time, the electronic device cannot run smoothly. At this time, the electronic device can kill applications to release the memory space occupied by the internal memory of the electronic device and release some computing resources. If the CPU utilization rate of the electronic device is 65% and the remaining computing resources of the electronic device are 350 MB, and the kswapd thread without any disabled operations starts to run, then the remaining computing resources of the electronic device are reduced to 200 MB, and the smoothness of the electronic device cannot be guaranteed. If the remaining computing resources of the electronic device are 350 MB and the kswapd thread with the memory page compression operation disabled starts to run, then the remaining computing resources of the electronic device are reduced to 250 MB, and the smoothness of the electronic device can be guaranteed.
[0215] In some embodiments, after performing step S911, the electronic device may re - execute step S904 to determine whether to control the kswapd thread to enter the sleep state.
[0216] It can be understood that except for the relationship that threshold E is greater than threshold F between the additionally described threshold E and threshold F, the thresholds A, B, C, and D involved in the embodiments of the present application are proposed in different embodiments respectively. Therefore, there is no restrictive relationship between any two of them.
[0217] Among them, in their respective embodiments, threshold A, threshold B, threshold D, and threshold E actually all correspond to the first preset threshold mentioned above. The application scenarios corresponding to different embodiments may be different. Therefore, the first preset threshold may have different situations. Therefore, in order to distinguish the first preset thresholds in different embodiments, the first preset threshold was described as threshold A, threshold B, threshold D, and threshold E above. Threshold F corresponds to the second preset threshold mentioned above, and threshold C corresponds to the third preset threshold mentioned above.
[0218] It should be noted that the processing of data collection, storage, use, processing, transmission, and disclosure involved in the embodiments of the present application all comply with the provisions of relevant laws and regulations.
[0219] The embodiments of the present application further provide a computer-readable storage medium, which includes computer instructions. When the computer instructions run on the above-mentioned electronic device, the electronic device is enabled to execute each function or step in the above-mentioned method embodiments.
[0220] The embodiments of the present application further provide a computer program product, including a computer program. When the computer program runs on an electronic device, the electronic device is enabled to execute each function or step in the above-mentioned method embodiments.
[0221] Through the description of the above embodiments, those skilled in the art can clearly understand that for the convenience and simplicity of description, only the division of the above function modules is used as an example. In actual applications, the above functions can be allocated to different function modules according to needs, that is, the internal structure of the device is divided into different function modules to complete all or part of the functions described above.
[0222] In several embodiments provided by the present application, it should be understood that the disclosed device and method can be implemented in other ways. For example, the device embodiments described above are merely illustrative. For example, the division of the module or unit 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 device, or some features can be ignored or not executed. Another point is that the displayed or discussed coupling or direct coupling or communication connection between each other can be through some interfaces. The indirect coupling or communication connection of the device or unit can be in an electrical, mechanical or other form.
[0223] The unit described as a separated component may or may not be physically separated. The component displayed as a unit may be a physical unit or multiple physical units, that is, it may be located in one place, or may be distributed to multiple different places. Some or all of the units can be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0224] In addition, in each embodiment of the present application, each functional unit can be integrated in a processing unit, or each unit can exist physically alone, or two or more units can be integrated in one unit. The above integrated unit can be implemented in the form of hardware or in the form of a software functional unit.
[0225] When the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a readable storage medium. Based on such an understanding, the technical solution of the embodiments of the present 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. The software product is stored in a storage medium and includes several instructions for causing a device (which can be a single-chip microcomputer, a chip, etc.) or a processor to execute all or part of the steps of the methods described in the embodiments of the present application. The foregoing storage medium includes: various media that can store program codes, such as USB flash drives, mobile hard disks, read only memory (ROM), random access memory (RAM), magnetic disks, or optical discs.
[0226] The above content is only the specific implementation manner of the present application, but the protection scope of the present application is not limited thereto. Any changes or substitutions within the technical scope disclosed in the present application should 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, applied to an electronic device, including: When the remaining available storage space of the electronic device meets the memory recycling condition, waking up the kernel swap daemon thread kswapd thread in the sleep state; In response to the CPU utilization rate of the electronic device being less than a first preset threshold, calling the kswapd thread to recycle memory.
2. The method according to claim 1, characterized in that, the method further includes: In response to the CPU utilization rate of the electronic device being greater than the first preset threshold, killing the application programs in the electronic device.
3. The method according to claim 1 or 2, characterized in that, the method further includes: In response to the CPU utilization rate of the electronic device being greater than the first preset threshold, controlling the kswapd thread to be in the sleep state.
4. The method according to any one of claims 1-3, characterized in that, the kswapd thread supports recycling memory by multiple recycling methods, the multiple recycling methods include a first recycling method, and the computing resources occupied by the first recycling method are greater than the computing resources occupied by other recycling methods in the multiple recycling methods; When the CPU utilization rate is greater than a second preset threshold, the recycling method for the kswapd thread to recycle memory does not include the first recycling method; When the CPU utilization rate is less than the second preset threshold, the recycling method for the kswapd thread to recycle memory includes the first recycling method; wherein, the second preset threshold is less than the first preset threshold.
5. The method according to claim 1, characterized in that, the kswapd thread supports recycling memory by multiple recycling methods, the multiple recycling methods include a first recycling method, and the computing resources occupied by the first recycling method are greater than the computing resources occupied by other recycling methods in the multiple recycling methods; the method further includes: In response to the CPU utilization rate of the electronic device being greater than the first preset threshold, calling the kswapd thread to recycle memory; When the CPU utilization rate of the electronic device is greater than the first preset threshold, the recycling method for the kswapd thread to recycle memory does not include the first recycling method.
6. The method according to any one of claims 1-5, characterized in that, the method further includes: Determining that the current scene where the electronic device is located is included in the whitelist; wherein, the whitelist is used to configure the application scenarios applicable to the memory recycling method.
7. The method according to claim 2, characterized in that, the killing the application programs in the electronic device includes: The electronic device calls the application framework layer of the electronic device to find the background application programs that meet the conditions, and the background application programs that meet the conditions include at least one of the following: application programs with a usage frequency lower than the threshold, application programs with a priority lower than the threshold, and application programs with a startup time lower than the threshold; Releasing the memory occupied by the background application programs that meet the conditions.
8. An electronic device, characterized in that, The electronic device includes a memory and one or more processors; the memory is coupled to the processors; the memory is used to store computer program code, and the computer program code includes computer instructions; when the processors execute the computer instructions, the electronic device is caused to execute the method according to any one of claims 1-7.
9. A computer-readable storage medium, characterized in that it includes computer instructions, and when the computer instructions run on an electronic device, the electronic device is caused to execute the method according to any one of claims 1-7.
Citation Information
Patent Citations
Application processing method and apparatus, readable storage medium and mobile terminal
CN107577508A
Memory recovery method, electronic equipment and computer readable storage medium
CN115292052A
Memory management method and device, electronic equipment and storage medium
CN115421909A