Memory recovery method and device, electronic equipment and storage medium
By obtaining the memory allocation parameters of the sliding window and dynamically generating memory reclamation parameters, the problem of insufficient memory capacity in electronic devices is solved, improving memory read/write efficiency and user experience.
Patent Information
- Application Number
- CN202110363358.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-04-02
- Publication Date
- 2026-02-10
- Estimated Expiration
- 2041-04-02
AI Technical Summary
Electronic devices have limited memory capacity, which cannot accommodate a large number of applications. Existing memory reclamation mechanisms frequently trigger Direct reclaim mode unnecessarily, affecting memory allocation efficiency and user experience.
By obtaining the memory allocation parameters of the sliding window, memory reclamation parameters are dynamically generated. A lightweight prediction mechanism is used to predict future memory needs and perform memory reclamation, avoiding frequent triggering of the Direct reclaim mode.
It improves memory read and write efficiency, prevents blocking and delays in the memory allocation process, optimizes memory resource management, and enhances the user experience.
Smart Images

Figure CN113138940B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of electronic equipment technology, and more specifically, to a memory reclamation method, apparatus, electronic device, and storage medium. Background Technology
[0002] With the development of science and technology, electronic devices are becoming increasingly widespread and feature-rich, becoming an essential part of people's daily lives. In recent years, applications (APPs) have been demanding more and more memory from electronic devices, while the memory capacity of electronic devices is limited and cannot accommodate a large number of applications residing in memory. Summary of the Invention
[0003] In view of the above problems, this application proposes a memory reclamation method, apparatus, electronic device, and storage medium to solve the above problems.
[0004] In a first aspect, embodiments of this application provide a memory reclamation method applied to an electronic device. The method includes: acquiring window data of a sliding window, wherein the window data includes memory allocation parameters of the electronic device in a first time period; determining memory reclamation parameters of the electronic device in a second time period based on the window data, wherein the first time period is earlier than the second time period; and performing memory reclamation on the electronic device based on the memory reclamation parameters.
[0005] Secondly, embodiments of this application provide a memory reclamation device applied to an electronic device. The device includes: a window data acquisition module for acquiring window data of a sliding window, wherein the window data includes memory allocation parameters of the electronic device in a first time period; a memory reclamation parameter determination module for determining memory reclamation parameters of the electronic device in a second time period based on the window data, wherein the first time period is earlier than the second time period; and a memory reclamation module for performing memory reclamation on the electronic device based on the memory reclamation parameters.
[0006] Thirdly, embodiments of this application provide an electronic device, including a memory and a processor, wherein the memory is coupled to the processor, the memory stores instructions, and when the instructions are executed by the processor, the processor performs the above-described method.
[0007] Fourthly, embodiments of this application provide a computer-readable storage medium storing program code, which can be invoked by a processor to execute the above-described method.
[0008] The memory reclamation method, apparatus, electronic device, and storage medium provided in this application embodiment acquire window data of a sliding window, wherein the window data includes memory allocation parameters of the electronic device in a first time period, and memory reclamation parameters of the electronic device in a second time period are determined based on the window data, wherein the first time period is earlier than the second time period, and memory reclamation is performed on the electronic device based on the memory reclamation parameters. Thus, by using a mechanism that dynamically generates a sliding window through historical data including memory allocation parameters, memory reclamation parameters are predicted and memory reclamation is performed, thereby improving memory read and write efficiency. Attached Figure Description
[0009] To more clearly illustrate the technical solutions in the embodiments of this application, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0010] Figure 1 A block diagram of an electronic device for performing a memory reclamation method according to an embodiment of this application is shown;
[0011] Figure 2 A block diagram of the operating system of the electronic device provided in an embodiment of this application is shown;
[0012] Figure 3 A flowchart illustrating a memory reclamation method provided in one embodiment of this application is shown;
[0013] Figure 4 A flowchart illustrating a memory reclamation method according to yet another embodiment of this application is shown;
[0014] Figure 5 A flowchart illustrating a memory reclamation method provided in another embodiment of this application is shown;
[0015] Figure 6 A flowchart illustrating a memory reclamation method provided in another embodiment of this application is shown;
[0016] Figure 7 A flowchart illustrating a memory reclamation method according to yet another embodiment of this application is shown;
[0017] Figure 8 A framework diagram is shown that can be used in the memory reclamation method provided in the embodiments of this application;
[0018] Figure 9 A block diagram of a memory reclamation device provided in an embodiment of this application is shown;
[0019] Figure 10An embodiment of the present application shows a storage unit for storing or carrying program code that implements the memory reclamation method according to an embodiment of the present application. Detailed Implementation
[0020] To enable those skilled in the art to better understand the present application, the technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the accompanying drawings.
[0021] In recent years, applications (APPs) have increasingly demanded more and more memory from electronic devices, while the memory capacity of electronic devices is limited and cannot accommodate a large number of applications residing in memory. Therefore, when the memory of an electronic device is insufficient, memory reclamation will be performed.
[0022] The electronic device runs the Android system, a mobile platform operating system based on the Linux kernel. When the device's memory becomes overloaded, memory reclamation is performed on each overloaded partition of the Android system. This reclamation primarily targets anonymous pages and file pages. For anonymous pages, infrequently used pages are selected during reclamation and written to the swap partition, then released as free page frames to the buddy system. For file pages, infrequently used pages are also selected. If the content of this file page matches the corresponding content of the file on the disk, it is considered a clean file page and does not need to be written back; it is directly released as a free page frame to the buddy system. Conversely, if the data in the file page does not match the corresponding data in the file on the disk, it is considered a dirty page and needs to be written back to the corresponding data location on the disk before being released as a free page frame to the buddy system. This way, once memory reclamation is complete, the number of free page frames in the system will increase, which can alleviate memory pressure. The downside is that the reclamation process will put a lot of pressure on the system's input / output. Therefore, in the Android system, each partition usually has a memory reclamation threshold. When the number of free page frames does not meet this threshold, memory reclamation will be performed. However, when the number of free page frames meets this threshold, the Android system will not perform memory reclamation.
[0023] Linux's principle for using physical memory is to utilize it as much as possible. For example, the file cache (Page Cache) in the Android system is cached in memory to speed up read and write operations. The Android system needs to reserve a certain amount of free memory at all times to meet new memory request operations. Linux has designed a daemon called kswapd, which swaps inactive data in memory to the Swap partition when certain conditions are met. If the requested space exceeds the free memory, the Linux system will trigger direct memory reclamation. The reclaimed memory objects include file cache (Page Cache) and process heap and stack data in anonymous memory. For file cache, if there is dirty data, the dirty data is written to disk and then the related memory is released. For anonymous memory, its data is directly swapped to the Swap partition and the memory is released.
[0024] The inventors discovered through research that current memory reclamation methods can unnecessarily trigger the Direct reclaim mode. In Direct reclaim mode, memory allocation and reclamation are synchronous; the memory allocation process is blocked by the reclamation operation, increasing waiting time. Direct reclaim severely impacts performance because all memory allocations are blocked waiting for Direct reclaim to complete, and many dirty pages need to be written back to external storage, significantly delaying memory allocation. Although the Linux kernel implements a lightweight background reclamation mechanism, Kswapd, to avoid Direct reclaim, and Kswapd is intermittently woken up to reclaim memory, Kswapd spends too much time reclaiming memory, inevitably causing Direct reclaim to be triggered again. While the system sets memory watermarks to ensure a certain amount of unused memory space to avoid triggering Direct reclaim due to sudden large memory demands, Direct reclaim is still a necessary measure. However, the inventors discovered through research that a commonly used Android system triggers Direct reclaim nearly 10 times within one minute. Currently, the coarse-grained reclamation mechanism for large memory blocks is suitable for desktop or server systems, but not for the fine-grained memory requirements of mobile platforms. Furthermore, the reclamation of large memory blocks usually causes a long latency, which affects the user experience.
[0025] To address the aforementioned problems, the inventors, through extensive research, discovered and proposed the memory reclamation method, apparatus, electronic device, and storage medium provided in the embodiments of this application. This method utilizes a mechanism that dynamically generates a sliding window based on historical data including memory allocation parameters to predict memory reclamation parameters and perform memory reclamation, thereby improving memory read / write efficiency. The specific memory reclamation method will be described in detail in subsequent embodiments.
[0026] Please see Figure 1 This diagram illustrates a structural block diagram of an electronic device 100 provided in an embodiment of this application. The electronic device 100 can be a smartphone, tablet computer, e-reader, or other electronic device capable of running applications. The electronic device 100 in this application may include one or more of the following components: a processor 110, a memory 120, a touch display screen 130, and one or more applications. The one or more applications may be stored in the memory 120 and configured to be executed by one or more processors 110. The one or more applications are configured to perform the methods described in the foregoing method embodiments.
[0027] The processor 110 may include one or more processing cores. The processor 110 connects to various parts within the electronic device 100 using various interfaces and lines, and performs various functions and processes data by running or executing instructions, programs, code sets, or instruction sets stored in the memory 120, and by calling data stored in the memory 120. Optionally, the processor 110 may be implemented using at least one hardware form of Digital Signal Processing (DSP), Field-Programmable Gate Array (FPGA), or Programmable Logic Array (PLA). The processor 110 may integrate one or a combination of several of the following: Central Processing Unit (CPU), Graphics Processing Unit (GPU), and modem. The CPU primarily handles the operating system, user interface, and applications; the GPU is responsible for rendering and drawing the content to be displayed; and the modem handles wireless communication. It is understood that the modem may also not be integrated into the processor 110 and may be implemented separately using a communication chip.
[0028] The memory 120 may include random access memory (RAM) or read-only memory (ROM). The memory 120 can be used to store instructions, programs, code, code sets, or instruction sets. The memory 120 may include a program storage area and a data storage area. The program storage area may store instructions for implementing an operating system, instructions for implementing at least one function (such as touch functionality, sound playback functionality, image playback functionality, etc.), and instructions for implementing the various method embodiments described below. The data storage area may also store data created by the electronic device 100 during use (such as phonebook data, audio and video data, chat log data, etc.).
[0029] The touch screen 130 is used to display information input by the user, information provided to the user, and various graphical user interfaces of the electronic device 100. These graphical user interfaces can be composed of graphics, text, icons, numbers, video, and any combination thereof. In one example, the touch screen 130 can be a liquid crystal display (LCD) or an organic light-emitting diode (OLED), without limitation.
[0030] Taking the operating system of electronic device 100 as Android as an example, the programs and data stored in memory 120 are as follows: Figure 2As shown, memory 120 stores the Linux kernel layer 220, system runtime library layer 240, application framework layer 260, and application layer 280. The Linux kernel layer 220 provides low-level drivers for various hardware components of the electronic device 100, such as display drivers, audio drivers, camera drivers, Bluetooth drivers, Wi-Fi drivers, and power management. The system runtime library layer 240 provides support for key features of the Android system through several C / C++ libraries. For example, the SQLite library provides database support, the OpenGL / ES library provides 3D graphics support, and the Webkit library provides browser kernel support. The system runtime library layer 240 also provides the Android Runtime, which mainly provides core libraries that allow developers to write Android applications using the Java language. The application framework layer 260 provides various APIs that may be used when building applications. Developers can also use these APIs to build their own applications, such as activity management, window management, view management, notification management, content providers, package management, call management, resource management, and location management. At least one application runs in the application layer 280. These applications can be built-in contact programs, SMS programs, clock programs, camera applications, etc., or they can be applications developed by third-party developers, such as instant messaging programs, photo enhancement programs, etc.
[0031] Please see Figure 3 , Figure 3 A flowchart illustrating a memory reclamation method according to an embodiment of this application is shown. The memory reclamation method uses a mechanism that dynamically generates a sliding window based on historical data including memory allocation parameters to predict memory reclamation parameters and perform memory reclamation, thereby improving memory read / write efficiency. In a specific embodiment, the memory reclamation method is applied to, for example... Figure 9 The memory reclamation device 300 and the electronic device 100 equipped with the memory reclamation device 300 are shown. Figure 1 The following will use an electronic device as an example to illustrate the specific process of this embodiment. Of course, it is understood that the electronic device used in this embodiment may include smartphones, tablets, wearable electronic devices, etc., and is not limited thereto. The following will focus on... Figure 3 The process shown will be described in detail. The memory reclamation method may specifically include the following steps:
[0032] Step S110: Obtain window data of the sliding window, wherein the window data includes memory allocation parameters of the electronic device in the first time period.
[0033] In this embodiment, the electronic device may be equipped with a sliding window, which serves as the basis for memory allocation and reclamation. In some implementations, the sliding window set by the electronic device may be generated based on the memory allocation parameters of the electronic device over a past period. The electronic device can then predict its future memory needs based on the window data included in the sliding window, and reclaim memory accordingly, thereby releasing memory space.
[0034] In this embodiment, window data of the sliding window can be obtained. This window data includes memory allocation parameters of the electronic device in a first time period. These parameters can be memory allocation size, memory allocation quantity, or both. For example, the window size of the sliding window can be obtained, and the memory allocation parameters of the electronic device in the first time period can be determined based on this window size. Alternatively, the size of the pages allocated by the electronic device in the first time period, as recorded by the sliding window's window data, can be obtained, and the memory allocation parameters of the electronic device in the first time period can be obtained based on this page size.
[0035] Step S120: Based on the window data, determine the memory reclamation parameters of the electronic device in the second time period, wherein the first time period is earlier than the second time period.
[0036] In this embodiment, after acquiring the window data of the sliding window, the memory reclamation parameters of the electronic device in the second time period can be determined based on the window data. This allows for the prediction and determination of the memory reclamation parameters of the electronic device in the future time period based on the memory allocation parameters of the electronic device in the past time period. The memory reclamation parameters can be the memory reclamation size, the memory reclamation quantity, or both.
[0037] In some implementations, the electronic device may have preset parameters that are used as the basis for judging the difference between memory reclamation parameters and memory allocation parameters. Therefore, when determining the memory reclamation parameters of the electronic device in the second time period based on the memory allocation parameters of the electronic device in the first time period, the memory reclamation parameters of the electronic device in the second time period may be determined based on the memory allocation parameters of the electronic device in the first time period and the preset parameters, so that the difference between the determined memory reclamation parameters and memory allocation parameters is less than the preset parameters.
[0038] As one approach, the memory reclamation parameter can be equal to the memory allocation parameter, or it can be slightly larger than the memory allocation parameter. For example, when the memory reclamation parameter is the memory reclamation size and the memory allocation parameter is the memory allocation size, the memory reclamation size can be equal to the memory allocation size, or it can be slightly larger than the memory allocation size; when the memory reclamation parameter is the memory reclamation quantity and the memory allocation parameter is the memory allocation quantity, the memory reclamation quantity can be equal to the memory allocation size, or it can be slightly larger than the memory allocation size; when the memory reclamation parameter is both the memory reclamation size and the memory reclamation quantity, and the memory allocation parameter is both the memory allocation size and the memory reclamation quantity, the memory reclamation size and the memory reclamation quantity can be equal to the memory allocation size and the memory allocation quantity, or they can be slightly larger than the memory allocation size and the memory allocation quantity.
[0039] In some implementations, the first time period can be the nth time period preceding the current time period, and the second time period can be the next time period following the current time period. That is, the memory reclamation parameters of the electronic device in the next time period can be predicted and determined based on the memory allocation parameters of the electronic device in the nth time period preceding the current time period, where n is a natural number. As another approach, when n is 0, the first time period can be the current time period, and the second time period can be the next time period following the current time period. In other words, the memory reclamation parameters of the electronic device in the next time period can be predicted and determined based on the memory allocation parameters of the electronic device in the current time period. As yet another approach, when n is 1, the first time period can be the previous time period, and the second time period can be the next time period following the current time period. In other words, the memory reclamation parameters of the electronic device in the next time period can be predicted and determined based on the memory allocation parameters of the electronic device in the previous time period.
[0040] In some implementations, the currently running application of the electronic device can be obtained, and the value of n can be determined based on the currently running application. Specifically, the larger the memory requirement of the currently running application, the smaller n is, and the larger the memory requirement of the currently running application, the larger n is. For example, when the currently running application is an instant messaging application (with file transfer caching), the value of n is determined to be smaller, for example, n is determined to be 0; when the currently running application is a novel application, the value of n is determined to be larger, for example, n is determined to be 1 or 2.
[0041] In some implementations, the current time can be obtained, and the value of n can be determined based on the current time. The value of n is smaller when the current time indicates a higher probability that the electronic device is being used, and larger when the current time indicates a lower probability that the electronic device is being used. For example, when the current time corresponds to a daytime period, the value of n is determined to be smaller, such as n=0; when the current time corresponds to a nighttime period, the value of n is determined to be larger, such as n=1 or 2.
[0042] In some implementations, the length of the time period can be automatically set by the electronic device or manually set by the user. For example, the length of the time period can be automatically set to 20ms by the electronic device or manually set to 25ms by the user, etc., and there is no limitation here.
[0043] In some implementations, the duration of the time segment can be divided equally or unequally. For example, the duration of the time segment can be divided into 20ms. In this case, the first time segment can be 20ms and the second time segment can be 20ms. The duration of the time segment can also be divided into multiple different durations such as 20ms, 25ms, and 28ms. In this case, the first time segment can be 20ms and the second time segment can be 25ms. No limitation is made here.
[0044] In some implementations, the length of the time period can be set to a fixed value. For example, the length of the time period can remain unchanged after the initial setting. Alternatively, the length of the time period can be set to a variable value. For example, the length of the time period can be set to different values depending on the time or the running application. No limitation is made here.
[0045] Step S130: Based on the memory reclamation parameters, perform memory reclamation on the electronic device.
[0046] In this embodiment, after determining the memory reclamation parameters of the electronic device in the second time period, memory reclamation can be performed on the electronic device based on these parameters. This ensures that memory reclamation meets the memory requirements of the electronic device and prevents frequent triggering of the Direct reclaim mode. For example, when the memory reclamation parameter is the memory reclamation size, memory reclamation can be performed based on the memory reclamation size; when the memory reclamation parameter is the memory reclamation quantity, memory reclamation can be performed based on the memory reclamation quantity; and when the memory reclamation parameters are both the memory reclamation size and the memory reclamation quantity, memory reclamation can be performed based on both.
[0047] As one approach, electronic devices can employ a Lightweight Prediction Scheme (LWP). This scheme generates a sliding window based on a timeline, storing memory allocation parameters over a past period. The window data is then used to predict memory reclamation parameters. To avoid burdening the electronic device's system and consuming CPU and storage resources, the LWP can be designed asynchronously and stores only a small amount of information. First, it stores values chronologically, avoiding the need to store timeline information to save storage space. Second, the sliding window comprises multiple information storage units, each storing sampled information (memory allocation parameters) from a specific time period, such as 20ms. Based on this information, the LWP can predict the next memory demand parameters and perform corresponding memory reclamation with relatively low storage and latency costs.
[0048] One embodiment of this application provides a memory reclamation method that obtains window data of a sliding window, wherein the window data includes memory allocation parameters of an electronic device in a first time period, and determines memory reclamation parameters of the electronic device in a second time period based on the window data, wherein the first time period is earlier than the second time period, and performs memory reclamation on the electronic device based on the memory reclamation parameters. This improves memory read and write efficiency by using a mechanism that dynamically generates a sliding window using historical data including memory allocation parameters to predict memory reclamation parameters and perform memory reclamation.
[0049] Please see Figure 4 , Figure 4 A flowchart illustrating a memory reclamation method according to another embodiment of this application is shown. This method is applied to the aforementioned electronic device, and will be discussed below. Figure 4 The process shown will be described in detail. The memory reclamation method may specifically include the following steps:
[0050] Step S210: Obtain window data of the sliding window, wherein the window data includes memory allocation parameters of the electronic device in the first time period.
[0051] For a detailed description of step S210, please refer to step S110, which will not be repeated here.
[0052] Step S220: Based on the window data, determine the memory requirement parameters of the electronic device in the second time period.
[0053] In this embodiment, after acquiring the window data of the sliding window, the memory requirement parameters of the electronic device in the second time period can be determined based on the window data. This allows for the prediction and determination of the memory requirement parameters of the electronic device in the future time period based on the memory allocation parameters of the electronic device in the past time period. The memory requirement parameters can be the memory size requirement, the memory quantity requirement, or both.
[0054] In some implementations, the electronic device may pre-set and store a first preset parameter, which is used as the basis for judging the difference between the memory requirement parameter and the memory allocation parameter. Therefore, when determining the memory requirement parameter of the electronic device in the second time period based on the memory allocation parameter of the electronic device in the first time period, the memory requirement parameter of the electronic device in the second time period can be determined based on the memory allocation parameter of the electronic device in the first time period and the first preset parameter, so that the difference between the determined memory requirement parameter and the memory allocation parameter is less than the preset parameter.
[0055] In one approach, the memory requirement parameter can be equal to the memory allocation parameter, or it can be slightly larger than the memory allocation parameter. For example, when the memory requirement parameter is the memory requirement size and the memory allocation parameter is the memory allocation size, the memory requirement size can be equal to the memory allocation size, or it can be slightly larger than the memory allocation size. Similarly, when the memory requirement parameter is the memory requirement quantity and the memory allocation parameter is the memory allocation quantity, the memory requirement quantity can be equal to the memory allocation quantity, or it can be slightly larger than the memory allocation quantity. Furthermore, when the memory requirement parameters are both the memory requirement size and the memory requirement quantity, and the memory allocation parameters are both the memory allocation size and the memory requirement quantity, the memory requirement size and the memory requirement quantity can be equal to the memory allocation size and the memory allocation quantity, or they can be slightly larger than the memory allocation size and the memory allocation quantity.
[0056] Step S230: Based on the memory requirement parameters, determine the memory reclamation parameters of the electronic device in the second time period.
[0057] In this embodiment, after determining the memory demand parameters, the memory reclamation parameters for the electronic device in the second time period can be determined based on the demand reclamation parameters. This allows for the determination of the memory reclamation parameters for the electronic device in the future time period based on its memory demand parameters. The memory reclamation parameters can be the memory reclamation size, the memory reclamation quantity, or both.
[0058] In some implementations, the electronic device may pre-set and store a second preset parameter, which is used as the basis for judging the difference between the memory reclamation parameter and the memory demand parameter. Therefore, when determining the memory reclamation parameter of the electronic device in the second time period based on the memory demand parameter of the electronic device in the second time period, the memory reclamation parameter of the electronic device in the second time period can be determined based on the memory demand parameter of the electronic device in the second time period and the second preset parameter, so that the difference between the determined memory reclamation parameter and the memory demand parameter is less than the preset parameter.
[0059] As one approach, the memory reclamation parameter can be equal to the memory requirement parameter, or it can be slightly larger than the memory requirement parameter. For example, when the memory reclamation parameter is the memory reclamation size and the memory requirement parameter is the memory requirement size, then the memory reclamation size can be equal to the memory requirement size, or it can be slightly larger than the memory requirement size; when the memory reclamation parameter is the memory reclamation quantity and the memory requirement parameter is the memory requirement quantity, then the memory reclamation quantity can be equal to the memory requirement size and the memory requirement quantity, or it can be slightly larger than the memory requirement size and the memory requirement quantity; when the memory reclamation parameter is both the memory reclamation size and the memory reclamation quantity, and the memory requirement parameter is both the memory requirement size and the memory reclamation quantity, then the memory reclamation size and the memory reclamation quantity can be equal to the memory requirement size and the memory requirement quantity, or they can be slightly larger than the memory requirement size and the memory requirement quantity.
[0060] Step S240: Based on the memory reclamation parameters, perform memory reclamation on the electronic device.
[0061] For a detailed description of step S240, please refer to step S130, which will not be repeated here.
[0062] Another embodiment of this application provides a memory reclamation method that obtains window data of a sliding window, wherein the window data includes memory allocation parameters of an electronic device in a first time period, determines memory requirement parameters of the electronic device in a second time period based on the window data, determines memory reclamation parameters of the electronic device in the second time period based on the memory requirement parameters, and performs memory reclamation on the electronic device based on the memory reclamation parameters. Compared to Figure 3 The memory reclamation method shown in this embodiment further determines the memory requirement parameters of the electronic device based on window data, and determines the memory reclamation parameters of the electronic device based on the memory requirement parameters, thereby improving the rationality of memory reclamation of the electronic device.
[0063] Please see Figure 5 , Figure 5 A flowchart illustrating a memory reclamation method provided in another embodiment of this application is shown. This method is applied to the aforementioned electronic device, and will be discussed below regarding... Figure 5The process shown will be described in detail. The memory reclamation method may specifically include the following steps:
[0064] Step S310: Obtain window data of the sliding window, wherein the window data includes memory allocation parameters of the electronic device in the first time period.
[0065] Step S320: Based on the window data, determine the memory reclamation parameters of the electronic device in the second time period, wherein the first time period is earlier than the second time period.
[0066] Step S330: Based on the memory reclamation parameters, perform memory reclamation on the electronic device.
[0067] For a detailed description of steps S310-S330, please refer to steps S110-S130, which will not be repeated here.
[0068] Step S340: Adjust the memory reclamation level of the electronic device based on the memory reclamation parameters.
[0069] In this embodiment, after determining the memory reclamation parameters of the electronic device in the second time period, the memory reclamation level of the electronic device can be adjusted based on the memory reclamation parameters. As one approach, after determining the memory reclamation parameters of the electronic device in the second time period, the memory reclamation level of the electronic device can be adjusted to the current memory reclamation level based on the memory reclamation parameters. The parameter corresponding to the current memory reclamation level is equal to or greater than the memory reclamation parameters, thereby achieving a balance in the memory performance of the electronic device and preventing the loss and latency caused by frequent triggering of the Direct reclaim mode.
[0070] As one approach, electronic devices can employ a lightweight prediction mechanism. Based on the memory reclamation parameters predicted by this mechanism, the lightweight prediction mechanism dynamically adjusts the background memory reclamation parameters of the electronic device. Specifically, Linux systems set a memory reclamation watermark to control the amount of memory reclaimed. By adjusting the watermark, the background memory reclamation of the electronic device can be adjusted. To achieve an optimal balance between performance and memory reclamation costs, the lightweight prediction mechanism dynamically adjusts the default background reclamation mechanism to reclaim just enough memory within a certain timeframe. Based on this, the lightweight prediction mechanism outputs the predicted memory to be reclaimed (memory reclamation parameters), allowing the background reclamation mechanism to reclaim the corresponding memory and adjusting the appropriate watermark to avoid the overhead and latency caused by frequent triggering of Direct reclaim mode.
[0071] In another embodiment of this application, a memory reclamation method is provided, which acquires window data of a sliding window, wherein the window data includes memory allocation parameters of an electronic device in a first time period. Based on the window data, memory reclamation parameters of the electronic device in a second time period are determined, wherein the first time period is earlier than the second time period. Based on the memory reclamation parameters, memory reclamation is performed on the electronic device, and the memory reclamation threshold of the electronic device is adjusted based on the memory reclamation parameters. Compared to Figure 3 The memory reclamation method shown in this embodiment also adjusts the memory reclamation level of the electronic device based on memory reclamation parameters to achieve memory performance balance and prevent performance loss and delay in memory reclamation.
[0072] Please see Figure 6 , Figure 6 A flowchart illustrating a memory reclamation method according to another embodiment of this application is shown. This method is applied to the aforementioned electronic device. In this embodiment, the memory reclamation parameters include memory reclamation size and memory reclamation quantity. The following will focus on... Figure 6 The process shown will be described in detail. The memory reclamation method may specifically include the following steps:
[0073] Step S410: Obtain window data of the sliding window, wherein the window data includes memory allocation parameters of the electronic device in the first time period.
[0074] For a detailed description of step S410, please refer to step S110, which will not be repeated here.
[0075] Step S420: Based on the window data, determine the memory reclamation size and memory reclamation quantity of the electronic device in the second time period, wherein the first time period is earlier than the second time period.
[0076] In this embodiment, the memory reclamation parameters include memory reclamation size and memory reclamation quantity. Based on this, after obtaining the window data of the sliding window, the memory reclamation size and memory reclamation quantity of the electronic device in the second time period can be determined based on the window data, thereby enabling the prediction and determination of the memory reclamation size and memory reclamation quantity of the electronic device in the future time period based on the memory allocation parameters of the electronic device in the past time period.
[0077] In some implementations, the electronic device may pre-set and store preset parameters, which are used as the basis for judging the difference between the memory reclamation size and memory reclamation quantity and the memory allocation parameters. Therefore, when determining the memory reclamation size and memory reclamation quantity of the electronic device in the second time period based on the memory allocation parameters of the electronic device in the first time period, the memory reclamation size and memory reclamation quantity of the electronic device in the second time period can be determined based on the memory allocation parameters of the electronic device in the first time period and the preset parameters, so that the difference between the determined memory reclamation size and memory reclamation quantity and the memory allocation parameters is less than the preset parameters.
[0078] In one approach, the size and amount of memory reclamation can be equal to the memory allocation parameters, or they can be slightly larger than the memory allocation parameters. For example, the size and amount of memory reclamation can be equal to the memory allocation size and the number of memory allocated, or they can be slightly larger than the memory allocation size and the number of memory allocated.
[0079] Step S430: Based on the memory reclamation size and the memory reclamation quantity, perform memory reclamation on the electronic device.
[0080] In this embodiment, after determining the size and quantity of memory reclamation for the electronic device in the second time period, memory reclamation can be performed on the electronic device based on the size and quantity of memory reclamation, so that memory reclamation meets the memory requirements of the electronic device and prevents frequent triggering of the Direct reclaim mode.
[0081] Another embodiment of this application provides a memory reclamation method that obtains window data of a sliding window, wherein the window data includes memory allocation parameters of an electronic device in a first time period. Based on the window data, the method determines the memory reclamation size and memory reclamation quantity of the electronic device in a second time period, wherein the first time period is earlier than the second time period. Based on the memory reclamation size and memory reclamation quantity, memory reclamation is performed on the electronic device. Compared to Figure 3 The memory reclamation method shown in this embodiment further determines the memory reclamation size and quantity of the electronic device based on window data, and performs memory reclamation on the electronic device based on the memory reclamation size and quantity, thereby improving the rationality of memory reclamation.
[0082] Please see Figure 7 , Figure 7 A flowchart illustrating a memory reclamation method according to yet another embodiment of this application is shown. This method is applied to the aforementioned electronic device, and will be discussed below. Figure 7 The process shown will be described in detail. The memory reclamation method may specifically include the following steps:
[0083] Step S510: Obtain historical data, wherein the historical data characterizes the memory allocation parameters of the electronic device in at least one historical time period, and the at least one historical time period includes the first time period.
[0084] In this embodiment, historical data that can be used to characterize memory allocation parameters of an electronic device over at least one historical time period can be obtained, wherein the at least one historical time period includes a first time period. Alternatively, when the number corresponding to the historical time period is one, the historical time period only includes the first time period; when the number corresponding to the historical time period is multiple, the historical time period includes the first time period and other time periods.
[0085] In some implementations, the historical data may include only the memory allocation parameters of the electronic device over at least one historical period, or it may include the memory allocation parameters and memory allocation trends over at least one historical period; this is not limited here. The memory allocation trend may include: memory allocation gradually decreasing, memory allocation gradually increasing, or memory allocation remaining unchanged.
[0086] Step S520: Based on the historical data, generate a sliding window and determine the window data of the sliding window.
[0087] In this embodiment, after acquiring historical data, a sliding window can be generated based on the historical data, and the window data of the sliding window can be determined.
[0088] Please see Figure 8 , Figure 8 A framework diagram is shown that can be used in the memory reclamation method provided in the embodiments of this application. For example... Figure 8 As shown, electronic devices can apply a lightweight prediction mechanism, which generates a sliding window based on the acquired historical data and the timeline. The window data of the sliding window is used to predict and determine the memory reclamation parameters (memory reclamation size and memory reclamation quantity), thereby determining the memory reclamation scheme and performing memory reclamation based on the memory reclamation scheme.
[0089] Step S530: Obtain window data of the sliding window, wherein the window data includes memory allocation parameters of the electronic device in the first time period.
[0090] Step S540: Based on the window data, determine the memory reclamation parameters of the electronic device in the second time period, wherein the first time period is earlier than the second time period.
[0091] Step S550: Based on the memory reclamation parameters, perform memory reclamation on the electronic device.
[0092] For a detailed description of steps S530-S550, please refer to steps S110-S130, which will not be repeated here.
[0093] This application provides another embodiment of a memory reclamation method, which acquires historical data, wherein the historical data characterizes the memory allocation parameters of an electronic device in at least one historical time period, the at least one historical time period including a first time period. Based on the historical data, a sliding window is generated and window data of the sliding window is determined, the window data of the sliding window is acquired, wherein the window data includes the memory allocation parameters of the electronic device in the first time period, and based on the window data, memory reclamation parameters of the electronic device in a second time period are determined, wherein the first time period is earlier than the second time period, and memory reclamation is performed on the electronic device based on the memory reclamation parameters. Compared to Figure 3 The memory reclamation method shown in this embodiment also generates a sliding window based on historical data containing memory allocation parameters for historical time periods, so as to realize the dynamic generation of the sliding window and improve the rationality of memory reclamation.
[0094] Please see Figure 9 , Figure 9 A module block diagram of a memory reclamation device provided in an embodiment of this application is shown. This memory reclamation device 300 is applied to the aforementioned electronic device, and will be discussed below regarding... Figure 9 The block diagram shown illustrates that the memory reclamation device 300 includes: a window data acquisition module 310, a memory reclamation parameter determination module 320, and a memory reclamation module 330, wherein:
[0095] The window data acquisition module 310 is used to acquire window data of the sliding window, wherein the window data includes memory allocation parameters of the electronic device in a first time period.
[0096] The memory reclamation parameter determination module 320 is used to determine the memory reclamation parameters of the electronic device in a second time period based on the window data, wherein the first time period is earlier than the second time period.
[0097] Furthermore, the memory reclamation parameter determination module 320 includes: a memory requirement parameter determination submodule and a memory reclamation parameter determination submodule, wherein:
[0098] The memory requirement parameter determination submodule is used to determine the memory requirement parameters of the electronic device in the second time period based on the window data.
[0099] The memory reclamation parameter determination submodule is used to determine the memory reclamation parameters of the electronic device in the second time period based on the memory demand parameters.
[0100] The memory reclamation module 330 is used to reclaim memory from the electronic device based on the memory reclamation parameters.
[0101] Furthermore, the memory reclamation module 330 includes: a memory reclamation submodule, wherein:
[0102] The memory reclamation submodule is used to reclaim memory from the electronic device based on the memory reclamation size and the memory reclamation quantity.
[0103] Furthermore, the memory reclamation device 300 also includes: a memory reclamation waterline adjustment module, wherein:
[0104] The memory reclamation waterline adjustment module is used to adjust the memory reclamation waterline of the electronic device based on the memory reclamation parameters.
[0105] Furthermore, the memory waterline adjustment module includes: a memory reclamation waterline adjustment submodule, wherein:
[0106] The memory reclamation waterline adjustment submodule is used to adjust the memory reclamation waterline of the electronic device to the current memory reclamation waterline based on the memory reclamation parameters, wherein the parameter corresponding to the current memory reclamation waterline is equal to or greater than the memory reclamation parameters.
[0107] Furthermore, the memory reclamation device 300 also includes: a historical data acquisition module and a window data generation module, wherein:
[0108] A historical data acquisition module is used to acquire historical data, wherein the historical data represents the memory allocation parameters of the electronic device in at least one historical time period, wherein the at least one historical time period includes the first time period.
[0109] The window data generation module is used to generate a sliding window and determine the window data of the sliding window based on the historical data.
[0110] Those skilled in the art will clearly understand that, for the sake of convenience and brevity, the specific working process of the above-described device and module can be referred to the corresponding process in the foregoing method embodiments, and will not be repeated here.
[0111] In the several embodiments provided in this application, the coupling between modules can be electrical, mechanical, or other forms of coupling.
[0112] Furthermore, the functional modules in the various embodiments of this application can be integrated into one processing module, or each module can exist physically separately, or two or more modules can be integrated into one module. The integrated modules described above can be implemented in hardware or as software functional modules.
[0113] Please see Figure 10 This diagram illustrates a structural block diagram of a computer-readable storage medium provided in an embodiment of this application. The computer-readable medium 400 stores program code that can be called by a processor to execute the methods described in the above method embodiments.
[0114] The computer-readable storage medium 400 may be an electronic memory such as flash memory, EEPROM (Electrically Erasable Programmable Read-Only Memory), EPROM, hard disk, or ROM. Optionally, the computer-readable storage medium 400 includes a non-transitory computer-readable storage medium. The computer-readable storage medium 400 has storage space for program code 410 that performs any of the method steps described above. This program code can be read from or written to one or more computer program products. The program code 410 may be compressed, for example, in a suitable form.
[0115] In summary, the memory reclamation method, apparatus, electronic device, and storage medium provided in this application obtain window data of a sliding window, wherein the window data includes memory allocation parameters of the electronic device in a first time period. Based on the window data, memory reclamation parameters of the electronic device in a second time period are determined, wherein the first time period is earlier than the second time period. Based on the memory reclamation parameters, memory reclamation is performed on the electronic device. Thus, by using a mechanism that dynamically generates a sliding window using historical data including memory allocation parameters, memory reclamation parameters are predicted and memory reclamation is performed, thereby improving memory read and write efficiency.
[0116] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of this application, and are not intended to limit them. Although this application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features. Such modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of this application.
Claims
1. A memory reclamation method, characterized in that, Applied to electronic devices, the method includes: Obtain window data of the sliding window, wherein the window data includes memory allocation parameters of the electronic device in a first time period, the first time period is determined based on the memory requirements of the application currently running on the electronic device, the first time period is the current time period or the nth time period before the current time period, n is negatively correlated with the requirements, and n is a natural number; Based on the window data, the memory reclamation parameters of the electronic device in the second time period are determined, wherein the first time period is earlier than the second time period; Based on the memory reclamation parameters, memory reclamation is performed on the electronic device.
2. The method according to claim 1, characterized in that, The step of determining the memory reclamation parameters of the electronic device in the second time period based on the window data includes: Based on the window data, the memory requirement parameters of the electronic device in the second time period are determined; Based on the memory requirement parameters, the memory reclamation parameters of the electronic device in the second time period are determined.
3. The method according to claim 1, characterized in that, The difference between the memory reclamation parameter and the memory allocation parameter is less than a preset parameter.
4. The method according to claim 1, characterized in that, After determining the memory reclamation parameters of the electronic device in the second time period based on the window data, the method further includes: Based on the memory reclamation parameters, the memory reclamation level of the electronic device is adjusted.
5. The method according to claim 4, characterized in that, The adjustment of the memory reclamation level of the electronic device based on the memory reclamation parameters includes: Based on the memory reclamation parameters, the memory reclamation level of the electronic device is adjusted to the current memory reclamation level, wherein the parameter corresponding to the current memory reclamation level is equal to or greater than the memory reclamation parameters.
6. The method according to any one of claims 1-5, characterized in that, The memory reclamation parameters include memory reclamation size and memory reclamation quantity. The process of reclamating memory in the electronic device based on these parameters includes: The electronic device performs memory reclamation based on the stated memory reclamation size and the stated memory reclamation quantity.
7. The method according to any one of claims 1-5, characterized in that, Before obtaining the window data of the sliding window, the process also includes: Acquire historical data, wherein the historical data characterizes the memory allocation parameters of the electronic device in at least one historical time period, and the at least one historical time period includes the first time period; Based on the historical data, a sliding window is generated and the window data of the sliding window is determined.
8. The method according to any one of claims 1-5, characterized in that, The sliding window includes multiple information storage units, each of which is used to store the memory allocation parameters of the electronic device over a period of time.
9. The method according to any one of claims 1-5, characterized in that, The first time period includes the current time period or the previous time period of the current time period, and the second time period includes the next time period of the current time period.
10. A memory reclamation device, characterized in that, Applied to electronic devices, the device includes: A window data acquisition module is used to acquire window data of a sliding window. The window data includes memory allocation parameters of the electronic device in a first time period. The first time period is determined based on the memory requirements of the application currently running on the electronic device. The first time period is the current time period or the nth time period before the current time period, where n is negatively correlated with the memory requirements and n is a natural number. The memory reclamation parameter determination module is used to determine the memory reclamation parameters of the electronic device in a second time period based on the window data, wherein the first time period is earlier than the second time period; The memory reclamation module is used to reclaim memory from the electronic device based on the memory reclamation parameters.
11. An electronic device, characterized in that, The method includes a memory and a processor, the memory being coupled to the processor, the memory storing instructions, and the processor performing the method as described in any one of claims 1-9 when the instructions are executed by the processor.
12. A computer-readable storage medium, characterized in that, The computer-readable storage medium contains program code that can be invoked by a processor to execute the method as described in any one of claims 1-9.
Citation Information
Patent Citations
Work set division method and system
CN110928635A
Memory allocation method and device, storage medium and electronic equipment
CN111078405A