Memory management method and device, electronic equipment and storage medium
By comparing memory reclamation parameters with preset thresholds in the memory management method, memory defragmentation is performed only when memory reclamation pressure and CPU load meet the conditions, thus solving the problem of unoptimized timing in the memory management method and improving system performance and stability.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- GUANGDONG OPPO MOBILE TELECOMMUNICATIONS CORP LTD
- Filing Date
- 2022-08-19
- Publication Date
- 2026-06-26
Smart Images

Figure CN115421909B_ABST
Abstract
Description
Technical Field
[0001] This application belongs to the field of computer technology, and specifically relates to a memory management method, apparatus, device, and readable storage medium. Background Technology
[0002] In various operating systems, such as Linux, prolonged continuous operation and high memory consumption by tasks can easily lead to memory shortages and degrade system performance. Memory compaction, based on the kernel's page migration mechanism, moves movable pages, thereby consolidating discrete pages into contiguous ones, ensuring normal system operation and improving performance. However, the timing of initiating memory compaction in related memory management methods still requires further optimization. Summary of the Invention
[0003] In view of the above problems, this application proposes a memory management method, apparatus, electronic device and storage medium to improve the above problems.
[0004] In a first aspect, embodiments of this application provide a memory management method, the method comprising: obtaining current memory reclamation parameters, the memory reclamation parameters including memory reclamation pressure and CPU load; and if the memory reclamation parameters meet a preset parameter threshold, performing memory consolidation on the system memory.
[0005] Secondly, embodiments of this application provide a memory management device, which includes a memory reclamation parameter acquisition unit and a memory reclamation parameter comparison unit. The memory reclamation parameter acquisition unit is used to acquire current memory reclamation parameters, including memory reclamation pressure and CPU load; the memory reclamation parameter comparison unit is used to perform memory regularization on the system memory if the memory reclamation parameters meet a preset parameter threshold.
[0006] Thirdly, embodiments of this application provide an electronic device, including one or more processors and a memory; one or more programs, wherein the one or more programs are stored in the memory and configured to be executed by the one or more processors, and the one or more programs are configured to perform the methods described above.
[0007] Fourthly, embodiments of this application provide a computer-readable storage medium storing program code, wherein the above-described method is executed when the program code is run.
[0008] This application provides a memory management method, apparatus, electronic device, and storage medium. The memory management method includes: firstly, acquiring current memory reclamation parameters, including memory reclamation pressure and CPU load; then, if the memory reclamation parameters meet a preset parameter threshold, performing memory defragmentation on the system memory. By comparing the memory reclamation pressure and CPU load with the preset parameter threshold, and only performing memory defragmentation after both the memory reclamation pressure and CPU load meet the preset parameter threshold, the number of times memory defragmentation is initiated prematurely can be reduced. This optimizes the timing of memory defragmentation, reduces the probability of processes entering slow paths, improves system performance, and avoids the deteriorating effects of excessive memory defragmentation. 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 flowchart of a memory management method according to an embodiment of this application is shown;
[0011] Figure 2 A flowchart of a memory management method according to another embodiment of this application is shown;
[0012] Figure 3 A flowchart illustrating a method for obtaining memory reclamation pressure according to another embodiment of this application is shown;
[0013] Figure 4 A flowchart illustrating a method for obtaining CPU load according to another embodiment of this application is shown;
[0014] Figure 5 A flowchart of a memory management method according to another embodiment of this application is shown;
[0015] Figure 6 A flowchart of a memory management method according to another embodiment of this application is shown;
[0016] Figure 7 This paper shows a structural block diagram of a memory management device according to an embodiment of this application;
[0017] Figure 8 This diagram illustrates a structural block diagram of an electronic device used to execute the memory management method of the embodiments of this application in real time.
[0018] Figure 9This application shows a storage unit for storing or carrying program code that implements the memory management method according to the embodiments of this application. Detailed Implementation
[0019] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those of ordinary skill in the art without creative effort are within the scope of protection of this application.
[0020] In various operating systems, such as Linux, prolonged continuous operation and high memory consumption by tasks can easily lead to memory shortages and degrade system performance. Memory compaction, based on the kernel's page migration mechanism, moves movable pages, thereby consolidating discrete pages into contiguous ones, ensuring normal system operation and improving performance. However, the timing of initiating memory compaction in related memory management methods still requires further optimization.
[0021] The inventors, in their research on related memory management methods, discovered that these methods typically require memory reclamation when the system's free pages fall below the low watermark, causing memory allocation to enter a slow path. During reclamation, the system determines the size of the memory to be reclaimed. If the amount is small, the kernel's page migration mechanism can be used to migrate discrete pages and consolidate them into contiguous pages. In this system, a page is equivalent to memory, and contiguous pages constitute contiguous memory. If the freed contiguous memory is sufficient to allocate the reclaimed memory, it is not reclaimed, thus reducing memory fragmentation and the frequency of reclamation. However, in these methods, when system memory is extremely low, insufficient memory can cause processes to enter a slow path. However, allocating memory to processes through memory compaction can increase the probability of processes entering slow paths in the long run, significantly impacting process performance. At the same time, since memory compaction uses the kernel's page migration mechanism to move movable pages and free up contiguous pages, this process consumes CPU resources. If memory compaction is performed when the CPU load is high, it can cause a sharp increase in CPU load, leading to a decrease in system performance.
[0022] Therefore, the inventors have proposed the memory management method, apparatus, electronic device, and readable storage medium of this application. First, current memory reclamation parameters are obtained, including memory reclamation pressure and CPU load. Then, if the memory reclamation parameters meet a preset threshold, memory defragmentation is performed on the system memory. By comparing the memory reclamation pressure and CPU load with the preset threshold, and only performing memory defragmentation after both the memory reclamation pressure and CPU load meet the preset threshold, the number of times memory defragmentation is prematurely entered can be reduced. This optimizes the timing of memory defragmentation, reduces the probability of processes entering slow paths, improves system performance, and avoids the deteriorating effects of excessive memory defragmentation.
[0023] The embodiments of this application will now be described in detail with reference to the accompanying drawings.
[0024] Please see Figure 1 This application provides a memory management method, the method comprising:
[0025] Step S110: Obtain the current memory reclamation parameters, which include memory reclamation pressure and CPU load.
[0026] As one approach, memory reclamation parameters include memory reclamation pressure and CPU load. Memory reclamation pressure characterizes the level of system memory strain, while CPU load characterizes the level of CPU resource strain. Optionally, the current memory reclamation pressure can be obtained during the memory reclamation process; and the current CPU load can be obtained through the task scheduling subsystem when the system performs task scheduling.
[0027] As a method, the system performs memory reclamation for two main reasons. First, the system needs to provide enough memory for any sudden memory requests at any time, so that cache and other related memory can be used, and the system's remaining memory will not be in a state of being very low for a long time. Second, when there is a memory request that is greater than the free memory, the system will directly trigger a forced memory reclamation mechanism to try to reclaim memory to solve the problem due to memory shortage. The kernel employs two different mechanisms to address these two reasons. First, the Linux system uses the kswapd background reclamation thread. When the kernel allocates memory to a process, if the system memory level is below the low watermark, the system cannot allocate memory while the memory level is below the low watermark. Therefore, the kswapd background reclamation thread is awakened to asynchronously reclaim memory. Although the kswapd background reclamation thread is created at system startup, it is mostly in a sleep state, only being awakened when a process fails to allocate memory due to insufficient memory. This allows the kswapd background reclamation thread to reclaim memory for the process to use. Second, the Linux system triggers direct memory reclamation. When the kernel calls the page allocation function to allocate memory, if the system memory is insufficient to meet the allocation request, the kernel triggers the page reclamation mechanism to attempt to reclaim memory to resolve the problem. During memory allocation, the kswapd background reclamation thread only works when the watermark is between the low watermark and the minimum watermark; when the watermark is below the minimum watermark, direct memory reclamation is triggered. The kernel is the core of the system. Its main responsibilities include process management, disk management, task scheduling, and memory management. It can translate user-input commands into machine language that the computer hardware can understand, communicate directly with the hardware, and send requests initiated by application software to the hardware.
[0028] In this embodiment, during memory allocation, if the system memory is lower than the preset memory, memory allocation may fail, causing the process to enter a slow path. Once the process enters the slow path, a memory reclamation operation is triggered. The duration of a single memory reclamation operation can be obtained during this process. As one approach, this duration can be used as the memory reclamation pressure to obtain the current memory reclamation pressure.
[0029] When the system memory size is above the low watermark, the system can quickly allocate and acquire memory; this process is the fast path. However, when the system memory size is below the low watermark, the system cannot allocate memory normally and must enter the slow path.
[0030] The categories of memory to be reclaimed include file-mapped pages and anonymous pages. File-mapped pages can include page cache, dcache and icache in slabs, code segments of executable programs from user processes, and file-mapped pages. Anonymous pages can include physical memory allocated by processes using various APIs, including heap, stack, shared memory for inter-process communication, pipes, BSS segments, data segments, and TMPFS pages. This part cannot be directly written back; swap areas need to be created for it. These pages are also converted into file-mapped pages, which can be written back to disk. Furthermore, the system handles memory reclamation differently depending on whether the page is dirty. Dirty pages need to be written back to disk before reclamation, while clean pages can be released directly.
[0031] In this embodiment, CPU load can be obtained when the system performs task scheduling. CPU load can be used to display the size of the CPU's operating load over a period of time. The higher the CPU load, the more tasks are running. If the CPU is in a high-load state for a long time, it can easily have an adverse effect on the lifespan of the CPU and other computer hardware. Therefore, by monitoring the size of the CPU load, the CPU can be prevented from operating in a high-load state for a long time.
[0032] As a way to make reasonable use of CPU resources, after obtaining the CPU load status, if the CPU is detected to be in a high load state, some running tasks that consume a lot of CPU resources can be terminated, thereby reducing the CPU load; if the CPU is detected to be in a low load state, some tasks can be scheduled to run.
[0033] One approach is to begin acquiring the current memory reclamation pressure as soon as it is detected that memory reclamation has just begun. Specifically, the system triggers memory reclamation when memory levels fall below the low watermark. Memory reclamation can include kswapd memory reclamation and direct memory reclamation. The triggering conditions for these two types of reclamation differ; therefore, the memory reclamation method can be determined by detecting the triggering conditions, thereby determining the start time of memory reclamation. After determining the start time of memory reclamation, the current memory reclamation pressure is then acquired.
[0034] As another approach, when the system detects task scheduling through the task scheduling subsystem, the current CPU load can be obtained through the task scheduling subsystem. Specifically, the system can be equipped with a task scheduling subsystem, which can include functions for task scheduling and data reading. In this embodiment, the data reading function of the task scheduling subsystem is mainly used to read the current CPU load. Optionally, a task identifier can be pre-set for each type of task, and then the task identifier can be detected to determine whether the task is being scheduled by the task scheduling subsystem. When the corresponding task identifier is detected, it is determined that the task scheduling subsystem is performing task scheduling, and the current CPU load is then obtained.
[0035] Step S120: If the memory reclamation parameters meet the preset parameter threshold, perform memory consolidation on the system memory.
[0036] In this embodiment, the preset parameter threshold is used to characterize the threshold at which the corresponding memory reclamation parameters meet the conditions. The preset parameter threshold may include a preset pressure threshold and a preset load threshold, which correspond to memory reclamation pressure and CPU load, respectively.
[0037] Among them, memory consolidation applies the kernel's page reclamation mechanism to migrate movable pages and then integrate discrete pages into contiguous pages for use in the next memory allocation.
[0038] Understandably, memory fragmentation occurs when a process cannot fully utilize the fixed memory region allocated to it. Memory fragments are essentially unusable free memory, resulting in a large number of discrete, non-contiguous pages. In some scenarios, a large number of contiguous pages are needed. If the kernel cannot satisfy this need, a kernel error will occur. To avoid kernel errors, memory compaction can be used to obtain the required contiguous pages. For example, when using the kmalloc memory allocation function, the pages must be contiguous, and the amount of free memory in the system must be greater than the requested memory size. In this case, if the system does not have enough free memory, memory compaction can be used to obtain the required contiguous pages.
[0039] There are three ways to trigger memory compaction in the system: First, manual triggering, which is achieved by writing 1 to the ` / proc / sys / vm / compact_memory` node. This manual memory compaction scans all zones on all memory nodes in the system and performs memory compaction on each zone. Second, triggering via the kcompactd kernel thread. The kcompactd kernel thread is similar to the kswapd background recycling thread for page reclamation. A kcompactd kernel thread is created for each memory node and named "kcompactd0", "kcompactd1", etc. Third, direct memory compaction, which is similar to page reclamation. When the system detects that memory is below the low watermark and cannot satisfy page allocation, it enters the slow path. In the slow path, in addition to waking up the kswapd background recycling thread, the `_alloc_pages_direct_compact()` function is called to attempt to consolidate discrete pages into contiguous pages.
[0040] In this embodiment, the memory reclamation parameters obtained by the aforementioned method can be compared with a preset parameter threshold. If it is determined that the current memory reclamation parameters meet the preset parameter threshold, memory regularization is performed on the system memory. The current memory reclamation parameters meeting the preset parameter threshold may include memory reclamation pressure meeting a preset pressure threshold and CPU load meeting a preset load threshold.
[0041] This application provides a memory management method that first obtains current memory reclamation parameters, including memory reclamation pressure and CPU load. Then, if the memory reclamation parameters meet a preset threshold, memory defragmentation is performed on the system memory. By comparing memory reclamation pressure and CPU load with preset thresholds, and only performing memory defragmentation after both parameters meet the thresholds, the number of times memory defragmentation is initiated prematurely can be reduced. This optimizes the timing of memory defragmentation, lowers the probability of processes entering slow paths, improves system performance, and avoids the deteriorating effects of excessive memory defragmentation.
[0042] Please see Figure 2 This application provides a memory management method, the method comprising:
[0043] Step S210: If the current free memory is lower than the preset memory, then start the memory reclamation operation.
[0044] In this embodiment, before performing memory reclamation, the system needs to monitor the relationship between the current system's free memory and the preset memory. If the current system's free memory is lower than the preset memory, the memory reclamation operation is triggered. The preset memory indicates that the current system's free memory is very low. The preset memory can be a specific value set by the user or a memory threshold inherent in the system. The memory threshold inherent in the system can be a low watermark, and no specific limitation is made here.
[0045] Step S220: Obtain the start time and end time corresponding to the memory reclamation operation.
[0046] In this embodiment of the application, during the memory reclamation operation, the system records the start time when the memory reclamation operation begins and the end time when the memory reclamation operation ends.
[0047] Step S230: Determine the current memory reclamation pressure based on the start time and the end time.
[0048] In this embodiment, the duration of a single memory reclamation operation can be obtained based on the start and end times of the memory reclamation operation. The duration of a single memory reclamation operation can be calculated by subtracting the start time from the end time, and the magnitude of the duration represents the magnitude of the memory reclamation pressure.
[0049] Once the memory reclamation pressure is obtained using the above method, the system can determine whether the current free memory is greater than the preset memory. If the system detects that the current free memory is still lower than the preset memory after one memory reclamation, it will perform another memory reclamation operation, recording the duration of each reclamation. If the system detects that the current free memory is greater than the preset memory after one memory reclamation, it will set the duration of each reclamation to 0 and end the reclamation operation to prevent misjudgments in memory assessment and avoid errors in monitoring memory reclamation pressure.
[0050] For example, the processes described in steps S210, S220, and S230 can be as follows: Figure 3As shown, when the system monitors memory load, it first allocates memory. When the system detects that the current free memory is lower than the preset memory, it performs memory reclamation. During memory reclamation, the start time is recorded as timestamp 1, and the end time as timestamp 2. The duration of a single memory reclamation can be calculated using timestamps 1 and 2, and is denoted as STALL_TIME, which is timestamp 2 minus timestamp 1. After each memory reclamation, it first checks if the current free memory is greater than the preset memory. If the current free memory is greater than or equal to the preset memory, the system has sufficient free memory, and STALL_TIME can be set to 0, ending memory reclamation. If the current free memory is less than the preset memory, the system lacks sufficient free memory, and memory reclamation needs to continue.
[0051] Step S240: Obtain the CPU's running time and idle time within a preset time period.
[0052] In this embodiment, when scheduling tasks, the system can obtain the running time and idle time of each CPU node within a certain time period. The time period can be set to any value; for example, it can be set to 1 second. CPU idle time is mainly caused by a mismatch between the operating speeds of the CPU and I / O devices. To fully utilize CPU resources, the system cannot allow excessive CPU idle time. The system generally proposes solutions from two aspects: utilizing CPU idle time and reducing CPU idle time. Commonly used methods include multiprogramming, DMA, and I / O channels.
[0053] Meanwhile, each CPU load calculation may increase the system load, thus affecting system performance. Therefore, the CPU's runtime and idle time can be obtained at preset time intervals to reduce the impact of CPU load calculation on system performance. This preset time interval can be pre-set or configured according to different tasks. For example, the preset time interval can be simply set to 100ms, or it can be set according to the number of scheduled tasks. For instance, if the number of scheduled tasks exceeds a preset threshold, the preset time interval is extended; if the number of scheduled tasks is less than or equal to the preset threshold, the preset time interval is shortened. This reduces the number of CPU load calculations, thereby reducing the impact of CPU load calculation on the system.
[0054] Step S250: Determine the current CPU load based on the running time and the idle time.
[0055] In this embodiment of the application, the system obtains the running time and idle time of each CPU node through task scheduling, and then calculates the current CPU load by processing the running time and idle time of each CPU node. The calculation formula is as follows: CPU load = running time / (idle time + running time), that is, the value of the CPU load is obtained by dividing the running time by the sum of the running time and the idle time.
[0056] After obtaining the current CPU load using the above method, the system can monitor whether the task has been scheduled successfully. If the task scheduling is successful, the system will proceed with the next task scheduling and end the current CPU load monitoring.
[0057] For example, the processes described in steps S240 and S250 can be as follows: Figure 4 As shown, when the system monitors CPU load, it obtains the system's CPU running time and idle time over a period of time through the scheduling subsystem, calculates the CPU load based on the running time and idle time, schedules the next task after the load calculation is completed, and finally ends the monitoring.
[0058] Step S260: Monitor whether the memory reclamation parameters meet the preset parameter threshold. If they do, proceed to step S270; otherwise, proceed to step S280.
[0059] In this embodiment, the memory reclamation parameters include two parts: memory reclamation pressure and CPU load. The preset parameter thresholds also include two parts: a preset pressure threshold and a preset load threshold. The preset pressure threshold corresponds to memory reclamation pressure, and the preset load threshold corresponds to CPU load. The preset parameter thresholds can be fixed values or adjusted according to the memory size required by the process; no specific limitation is made here.
[0060] Once the system obtains the memory reclamation pressure and CPU load, it can compare the memory reclamation pressure and CPU load with preset parameter thresholds to determine whether the memory reclamation parameters meet the preset parameter thresholds, and then determine whether to perform memory consolidation or memory reclamation.
[0061] Step S270: Perform memory defragmentation on the system memory.
[0062] As one approach, the current memory reclamation parameters are compared with preset parameter thresholds. If the conditions are met, memory warping is performed, including: if the memory reclamation pressure is less than or equal to the pressure threshold and the CPU load is less than or equal to the preset load threshold, the system is triggered to perform memory warping.
[0063] In this embodiment, before performing memory consolidation, the system needs to compare the memory reclamation parameters with a preset threshold. Only after the condition is met can the system trigger memory consolidation. Since the system memory is lower than the preset threshold, the system enters a slow path, triggering a memory reclamation pressure acquisition operation. The system acquires the memory reclamation pressure by obtaining the duration of a single memory reclamation operation and compares it with a preset pressure threshold. If the memory reclamation pressure is less than or equal to the preset pressure threshold, the system triggers a CPU load acquisition operation. The system obtains the CPU load by acquiring the CPU's running time and idle time and compares the CPU load with a preset load threshold. If the CPU load is less than or equal to the preset load threshold, the system performs memory consolidation. The system uses a page migration mechanism to migrate movable pages, thereby integrating discrete pages into continuous pages and small memory blocks into large memory blocks.
[0064] Step S280: Enter memory reclamation.
[0065] In this embodiment of the application, the memory reclamation parameters are compared with preset parameter thresholds. If the conditions are not met, memory reclamation is performed. This includes: if the memory reclamation pressure is greater than the preset pressure threshold, or the CPU load is greater than the preset load threshold, then the memory reclamation parameters do not meet the preset parameter thresholds, and the system is triggered to perform memory reclamation.
[0066] This application provides a memory management method. First, if the current free memory is lower than a preset memory limit, a memory reclamation operation is initiated. The start and end times of the memory reclamation operation are obtained. Based on these times, the current memory reclamation pressure is determined. Then, the CPU runtime and idle time within a preset time period are obtained. Based on these times, the current CPU load is determined. Next, it is monitored whether the memory reclamation parameters meet preset thresholds. If the memory reclamation pressure is less than or equal to a preset pressure threshold, and the CPU load is less than or equal to a preset load threshold, the preset thresholds are met, and memory defragmentation is performed. If the memory reclamation parameters do not meet the preset thresholds, memory reclamation is initiated. By comparing the memory reclamation pressure and CPU load with preset thresholds, and only performing memory defragmentation after both thresholds are met, the number of premature memory defragmentation attempts is reduced. This optimizes the timing of memory defragmentation, reduces the probability of processes entering slow paths, improves system performance, and avoids the deterioration caused by excessive memory defragmentation.
[0067] Please see Figure 5 This application provides a memory management method, the method comprising:
[0068] Step S301: If the current free memory is detected to be lower than the preset memory, trigger the acquisition of the current memory reclamation parameters.
[0069] In this embodiment, the system detects that the current free memory is lower than the preset memory, resulting in memory allocation failure. Therefore, the system enters a slow path and triggers the acquisition of current memory reclamation parameters, including: acquiring memory reclamation pressure and CPU load. The memory reclamation pressure is determined by the duration of a single memory reclamation cycle, and the CPU load is determined by the running time and idle time of each CPU during task scheduling.
[0070] Step S302: If the current free memory is lower than the preset memory, then start the memory reclamation operation.
[0071] Step S302 can be specifically explained in the detailed explanation of the above embodiments, and therefore will not be repeated in this embodiment.
[0072] Step S303: Obtain the start time and end time corresponding to the memory reclamation operation.
[0073] Step S303 can be specifically explained in the detailed explanation of the above embodiments, and therefore will not be repeated in this embodiment.
[0074] Step S304: Determine the current memory reclamation pressure based on the start time and the end time.
[0075] Step S304 can be specifically explained in the detailed explanation of the above embodiments, and therefore will not be repeated in this embodiment.
[0076] Step S305: Obtain the CPU's running time and idle time within a preset time period.
[0077] Step S305 can be specifically explained in the detailed explanation of the above embodiments, and therefore will not be repeated in this embodiment.
[0078] Step S306: Determine the current CPU load based on the running time and the idle time.
[0079] Step S306 can be specifically explained in the detailed explanation of the above embodiments, and therefore will not be repeated in this embodiment.
[0080] Step S307: If the memory reclamation pressure is less than or equal to a preset pressure threshold, and the CPU load is less than or equal to a preset load threshold, determine that the preset parameter threshold is met, and perform memory regularization on the system memory.
[0081] Step S307 can be specifically explained in the detailed explanation of the above embodiments, and therefore will not be repeated in this embodiment.
[0082] Step S308: If the memory reclamation pressure is greater than the preset pressure threshold, or the CPU load is greater than the preset load threshold, it is determined that the preset parameter threshold is not met, and memory reclamation is initiated.
[0083] In this embodiment, when the memory reclamation pressure is greater than a preset pressure threshold, or the CPU load is greater than a preset load threshold, the memory reclamation pressure parameter does not meet the preset parameter threshold, and the system enters memory reclamation.
[0084] Alternatively, if the memory reclamation pressure is less than or equal to the preset pressure threshold, and the CPU load is greater than the preset load threshold, it is determined that the preset parameter threshold is not met, and memory reclamation is initiated.
[0085] In this embodiment, when the memory reclamation pressure is less than or equal to a preset pressure threshold and the CPU load is greater than a preset load threshold, the memory reclamation pressure parameter does not meet the preset parameter threshold, and the system enters memory reclamation.
[0086] Step S309: Allocate memory for the process.
[0087] In this embodiment, the system performs memory consolidation on the system memory, integrating small memory blocks into contiguous memory, and then allocates this contiguous memory to the requested task.
[0088] Step S310: If the allocation is successful, return the memory information.
[0089] In this embodiment, the system performs memory allocation on the memory that has been squared, including: if the contiguous memory obtained after memory squared meets the memory requirements of the requested task, the system can allocate the memory, and the memory allocation is successful. After the memory allocation is completed, the system returns the task's memory information to the user for viewing. The task's memory information includes the amount of memory occupied by the task, the task's location on the computer, etc., which are not specifically limited here.
[0090] Step S311: If allocation fails, proceed to memory reclamation.
[0091] In this embodiment, the system performs memory allocation processing on the memory after memory consolidation, including: if the contiguous memory obtained after memory consolidation does not meet the memory requirements of the requested task, the system memory cannot be allocated, and the memory allocation fails. After the memory allocation fails, a memory reclamation operation is triggered to reclaim reclaimable memory in the system, thereby increasing the system's memory size and thus meeting the memory requirements of the requested task. The reclaimable memory includes file-mapped pages and anonymous pages.
[0092] As a method, since page reclamation always comes at the cost of cache discarding and memory swapping, it has a certain negative impact on system performance. Therefore, memory reclamation is not about reclamating as much as possible, but rather about achieving a balance where the system reclaims memory down to a threshold. This threshold must ensure system performance while also handling new page allocation requests. This threshold is uniquely determined by the system.
[0093] This application provides a memory management method. First, if the current free memory is detected to be lower than a preset memory, the method triggers the acquisition of current memory reclamation parameters. Then, if the current free memory is lower than the preset memory, a memory reclamation operation is initiated. The start and end times of the memory reclamation operation are acquired. Based on the start and end times, the current memory reclamation pressure is determined. Next, the CPU running time and idle time within a preset time period are acquired. Based on the running time and idle time, the current CPU load is determined. If the memory reclamation pressure is less than or equal to a preset pressure threshold, and the CPU load is less than or equal to a preset load threshold, the preset parameter threshold is satisfied, and memory reorganization is performed on the system memory. If the memory reclamation pressure is greater than the preset pressure threshold, or the CPU load is greater than the preset load threshold, the preset parameter threshold is not satisfied, and memory reclamation is initiated. Alternatively, if the memory reclamation pressure is less than or equal to the preset pressure threshold, and the CPU load is greater than the preset load threshold, the preset parameter threshold is not satisfied, and memory reclamation is initiated. Finally, memory allocation is performed on the process. If the allocation is successful, memory information is returned; if the allocation fails, memory reclamation is initiated. By comparing memory reclamation pressure and CPU load with preset parameter thresholds, the above method reduces the number of times memory defragmentation is initiated prematurely. This optimizes the timing of memory defragmentation, reduces the probability of processes entering slow paths, improves system performance, and avoids the deterioration caused by excessive memory defragmentation.
[0094] Please see Figure 6 This application provides a memory management method, the method comprising:
[0095] Step S401: The process enters the slow path.
[0096] In this embodiment of the application, the system memory is lower than the preset memory, which causes the system memory allocation to fail, thus causing the system process to enter a slow path.
[0097] Step S402: Obtain memory reclamation pressure.
[0098] In this embodiment, the system obtains the memory reclamation pressure by measuring the duration of a single memory reclamation during the memory reclamation process. When the system's memory is higher than the preset memory, the duration of a single memory reclamation is set to 0.
[0099] Step S403: Determine whether the current memory reclamation pressure meets the pressure threshold. If it does, proceed to step S404; otherwise, proceed to step S410.
[0100] Step S404: Obtain CPU load.
[0101] In this embodiment of the application, the system obtains the CPU load by acquiring the running time and idle time of each CPU during task scheduling.
[0102] Step S405: Determine whether the current CPU load meets the load threshold. If it does, proceed to step S406; otherwise, proceed to step S410.
[0103] Step S406: Perform memory defragmentation.
[0104] In this embodiment, the kernel uses a page reclamation mechanism to migrate movable pages, thereby integrating discrete pages into contiguous pages and small memory into large memory.
[0105] Step S407: Reallocate memory.
[0106] Step S408: If memory allocation is successful, proceed to step S409; if memory allocation fails, proceed to step S410.
[0107] In this embodiment of the application, if the memory obtained after memory regularization is greater than the memory required by the requested task, the memory allocation is successful; if the memory obtained after memory regularization is less than the memory required by the requested task, the allocation fails.
[0108] Step S409: Return the memory to the user.
[0109] After successful memory allocation, the task information will be returned to the user for viewing. This information includes the amount of memory used and the task's location in the system, which are not specifically limited here.
[0110] Step S410: Enter memory reclamation.
[0111] In this embodiment, when the memory reclamation pressure does not meet the pressure threshold, or the CPU load does not meet the load threshold, or memory allocation fails, the system will enter memory reclamation. The reclamation objects include file-mapped pages and anonymous pages. When the system's memory meets the memory requirements of the requested task, memory reclamation ends.
[0112] This application provides a memory management method that first enters the slow path, then obtains the current memory reclamation pressure, and determines whether the current memory reclamation pressure meets a pressure threshold. If it does, the current CPU load is obtained; otherwise, memory reclamation is initiated, and the current CPU load is checked against a load threshold. If it does, memory defragmentation is performed; otherwise, memory reclamation is initiated again, and memory is reallocated. If memory allocation is successful, the memory is returned to the user; otherwise, memory reclamation is initiated again. By comparing the memory reclamation pressure and CPU load with preset parameter thresholds, memory defragmentation is only performed after both the pressure and CPU load meet the preset parameter thresholds. This reduces the number of times memory defragmentation is initiated prematurely, thus optimizing the timing of memory defragmentation, reducing the probability of processes entering the slow path, improving system performance, and avoiding the deterioration caused by excessive memory defragmentation.
[0113] Please see Figure 7 This application provides a memory management device 500, the device comprising:
[0114] The memory reclamation parameter acquisition unit 510 is used to acquire the current memory reclamation parameters, including memory reclamation pressure and CPU load.
[0115] In one approach, the memory reclamation parameter acquisition unit 510 is used to start a memory reclamation operation if the current free memory is lower than the preset memory; and to acquire the start time and end time corresponding to the memory reclamation operation.
[0116] Optionally, the memory reclamation parameter acquisition unit 510 is further configured to determine the current memory reclamation pressure based on the start time and the end time; acquire the CPU's running time and idle time within a preset time period; and determine the current CPU load based on the running time and the idle time.
[0117] Optionally, the memory reclamation parameter acquisition unit 510 is also used to trigger the acquisition of the current memory reclamation parameters if the current free memory is detected to be lower than the preset memory.
[0118] The memory reclamation parameter comparison unit 520 is used to perform memory consolidation on the system memory if the memory reclamation parameters meet the preset parameter threshold.
[0119] In one approach, the memory reclamation parameter comparison unit 520 is used to determine that the preset parameter threshold is met if the memory reclamation pressure is less than or equal to a preset pressure threshold and the CPU load is less than or equal to a preset load threshold, and to perform memory regularization on the system memory; if the memory reclamation parameters do not meet the preset parameter threshold, then memory reclamation is initiated.
[0120] Optionally, the memory reclamation parameter comparison unit 520 is specifically used to determine that the preset parameter threshold is not met and to enter memory reclamation if the memory reclamation pressure is greater than a preset pressure threshold or the CPU load is greater than a preset load threshold; or, if the memory reclamation pressure is less than or equal to the preset pressure threshold and the CPU load is greater than the preset load threshold, determine that the preset parameter threshold is not met and to enter memory reclamation.
[0121] Optionally, the memory reclamation parameter comparison unit 520 is also used to allocate memory for the process; if the allocation is successful, it returns memory information; if the allocation fails, it enters the memory reclamation process.
[0122] It should be noted that the device embodiments in this application correspond to the aforementioned method embodiments. The specific principles in the device embodiments can be found in the content of the aforementioned method embodiments, and will not be repeated here.
[0123] The following will combine Figure 8 This application describes an electronic device.
[0124] Please see Figure 8 Based on the aforementioned memory management methods and apparatus, this application also provides another electronic device 600 capable of executing the aforementioned memory management method. The electronic device 600 includes one or more (only one shown in the figure) processors 602, a memory 604, and a network module 606 coupled together. The memory 604 stores programs capable of executing the contents of the aforementioned embodiments, and the processor 602 can execute the programs stored in the memory 604.
[0125] The processor 602 may include one or more processing cores. The processor 602 connects to various parts within the electronic device 600 using various interfaces and lines, and executes various functions of the server 600 and processes data by running or executing instructions, programs, code sets, or instruction sets stored in the memory 604, and by calling data stored in the memory 604. Optionally, the processor 602 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 602 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 displayed content; and the modem handles wireless communication. It is understood that the modem may also not be integrated into the processor 602 and may be implemented separately using a communication chip.
[0126] The memory 604 may include random access memory (RAM) or read-only memory (ROM). The memory 604 can be used to store instructions, programs, code, code sets, or instruction sets. The memory 604 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 600 during use (such as phonebook data, audio and video data, chat log data, etc.).
[0127] The network module 606 is used to receive and transmit electromagnetic waves, realizing the mutual conversion between electromagnetic waves and electrical signals, thereby communicating with communication networks or other devices, such as audio playback devices. The network module 606 may include various existing circuit elements for performing these functions, such as antennas, radio frequency transceivers, digital signal processors, encryption / decryption chips, user identity modules (SIM cards), memory, etc. The network module 606 can communicate with various networks such as the Internet, corporate intranets, and wireless networks, or communicate with other devices through wireless networks. The aforementioned wireless networks may include cellular telephone networks, wireless local area networks (WLANs), or metropolitan area networks (MANs). For example, the network module 606 can interact with base stations.
[0128] Please refer to Figure 9 This diagram illustrates a structural block diagram of a computer-readable storage medium provided in an embodiment of this application. The computer-readable medium 700 stores program code that can be called by a processor to execute the methods described in the above method embodiments.
[0129] The computer-readable storage medium 700 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 700 includes a non-transitory computer-readable storage medium. The computer-readable storage medium 700 has storage space for program code 710 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 710 may, for example, be compressed in a suitable form.
[0130] This application provides a memory management method, apparatus, electronic device, and storage medium. The memory management method includes: firstly, acquiring current memory reclamation parameters, including memory reclamation pressure and CPU load; then, if the memory reclamation parameters meet a preset parameter threshold, performing memory defragmentation on the system memory. By comparing the memory reclamation pressure and CPU load with the preset parameter threshold, and only performing memory defragmentation after both the memory reclamation pressure and CPU load meet the preset parameter threshold, the number of times memory defragmentation is initiated prematurely can be reduced. This optimizes the timing of memory defragmentation, reduces the probability of processes entering slow paths, improves system performance, and avoids the deteriorating effects of excessive memory defragmentation.
[0131] The embodiments of the present invention have been described above with reference to the accompanying drawings. However, the present invention is not limited to the specific embodiments described above. The specific embodiments described above are merely illustrative and not restrictive. Those skilled in the art can make many other forms under the guidance of the present invention without departing from the spirit and scope of the claims, and all of these forms are within the protection scope of the present invention.
Claims
1. A memory management method, characterized in that, The method includes: Obtain the current memory reclamation parameters, which include memory reclamation pressure and CPU load. The value of memory reclamation pressure is the value of the duration of a single memory reclamation operation obtained by subtracting the start time from the end time corresponding to the memory reclamation operation. If the memory reclamation parameters meet the preset parameter threshold, memory consolidation is performed on the system memory. The memory consolidation is the page reclamation mechanism of the application kernel, which migrates movable pages and integrates discrete pages into continuous pages for the next memory request. If the memory reclamation parameters meet the preset parameter threshold, memory defragmentation is performed on the system memory, including: If the memory reclamation pressure is less than or equal to a preset pressure threshold, and the CPU load is less than or equal to a preset load threshold, the preset parameter threshold is satisfied, and memory regularization is performed on the system memory.
2. The method according to claim 1, characterized in that, The method further includes: If the memory reclamation parameters do not meet the preset parameter threshold, then memory reclamation is initiated.
3. The method according to claim 2, characterized in that, If the memory reclamation parameters do not meet the preset parameter threshold, then memory reclamation is initiated, including: If the memory reclamation pressure exceeds a preset pressure threshold, or the CPU load exceeds a preset load threshold, it is determined that the preset parameter threshold is not met, and memory reclamation is initiated; or... If the memory reclamation pressure is less than or equal to the preset pressure threshold, and the CPU load is greater than the preset load threshold, it is determined that the preset parameter threshold is not met, and memory reclamation is initiated.
4. The method according to claim 1, characterized in that, The process of obtaining the current memory reclamation parameters includes: If the current free memory is lower than the preset memory, then a memory reclamation operation will begin; Obtain the start and end times corresponding to the memory reclamation operation; Based on the start time and the end time, the current memory reclamation pressure is determined.
5. The method according to claim 1, characterized in that, The process of obtaining the current memory reclamation parameters includes: Obtain the CPU's running time and idle time within a preset time period; The current CPU load is determined based on the running time and the idle time.
6. The method according to claim 1, characterized in that, If the memory reclamation parameters meet the preset parameter threshold, the process after memory defragmentation of the system memory further includes: Allocate memory for the process; If the allocation is successful, memory information will be returned. If allocation fails, memory reclamation will occur.
7. The method according to claim 1, characterized in that, Before obtaining the current memory reclamation parameters, the following steps are also included: If the current free memory is detected to be lower than the preset memory, the current memory reclamation parameters will be retrieved.
8. A memory management device, characterized in that, The device includes: The memory reclamation parameter acquisition unit is used to acquire the current memory reclamation parameters, which include memory reclamation pressure and CPU load. The value of memory reclamation pressure is the value of the duration of a single memory reclamation operation obtained by subtracting the start time from the end time corresponding to the memory reclamation operation. The memory reclamation parameter comparison unit is used to perform memory regularization on the system memory if the memory reclamation parameters meet a preset parameter threshold. The memory regularization is the application kernel's page reclamation mechanism, which migrates movable pages and integrates discrete pages into contiguous pages for the next memory allocation. The step of performing memory regularization on the system memory if the memory reclamation parameters meet the preset parameter threshold includes: if the memory reclamation pressure is less than or equal to a preset pressure threshold and the CPU load is less than or equal to a preset load threshold, it is determined that the preset parameter threshold is met, and memory regularization is performed on the system memory.
9. An electronic device, characterized in that, It includes one or more processors and memory, wherein one or more programs are stored in the memory and configured to be executed by one or more processors according to any one of claims 1-7.
10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores program code, which includes instructions for performing the method as claimed in any one of claims 1-7.
Citation Information
Patent Citations
CN114116191A