Memory management method and system and electronic equipment

By monitoring memory stress events and improving memory reclamation performance, and combining changes in memory blocking duration, the memory stress level is evaluated in a more refined manner. This solves the problem of inaccurate memory stress assessment in existing technologies and improves the adaptability and stability of the system's memory management strategy.

CN121935017APending Publication Date: 2026-04-28LENOVO (BEIJING) LTD
View PDF 0 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
LENOVO (BEIJING) LTD
Filing Date
2025-12-31
Publication Date
2026-04-28

AI Technical Summary

Technical Problem

Existing technologies assess memory pressure by measuring only the single dimension of memory blocking state, resulting in inaccurate assessments that fail to reflect the accumulation of potential memory pressure in a timely or accurate manner. This, in turn, leads to deviations in the formulation and timing of system management strategies.

Method used

By monitoring kernel pressure stall events where memory pressure exceeds a threshold, system memory information is obtained. Combined with changes in memory reclamation performance and memory blocking duration, the memory pressure level is determined to achieve fine-grained memory management.

Benefits of technology

It enables accurate assessment of memory pressure, avoids ineffective or excessive garbage collection, ensures that memory management strategies are adapted to memory pressure, and improves system stability and response efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121935017A_ABST
    Figure CN121935017A_ABST
Patent Text Reader

Abstract

The embodiment of the invention discloses a memory management method and system and electronic equipment. Comprising the steps of obtaining memory information of a system in response to a detected kernel pressure stall event; wherein the kernel pressure stall event is a trigger event generated when it is monitored that the memory pressure exceeds a threshold value; according to the memory information and the memory blocking duration of the kernel pressure stall event, the memory recovery improvement efficiency of the system is determined, and the memory recovery improvement efficiency is related to the variable quantity of the memory blocking duration in the kernel pressure stall event in the adjacent statistical period; and based on the memory recovery improvement efficiency, determining a memory pressure level, so that the target component performs memory management based on the memory pressure level.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to, but is not limited to, the field of memory management technology, and in particular to a memory management method, system, and electronic device. Background Technology

[0002] On embedded devices such as mobile phones and tablets, computing and storage resources are limited. When users run multiple applications simultaneously, the system is prone to memory shortages, leading to systemic lag and impacting user experience. When memory is low, the system level and individual business modules need to respond and adjust their operating behavior to allow the system to recover from a low memory state as quickly as possible.

[0003] In related technologies, memory pressure is typically determined by calculating the percentage of time tasks in the system were blocked due to insufficient memory resources within past sampling periods, in order to trigger mitigation measures such as memory reclamation or load adjustment in a timely manner. However, this approach measures memory pressure solely from the single dimension of whether task execution flow is interrupted. This results in an inaccurate and comprehensive assessment of memory pressure, failing to reflect potential memory pressure accumulation in a timely or accurate manner, and consequently leading to deviations in the formulation and timing of system management strategies. Summary of the Invention

[0004] This application provides a memory management method, system, and electronic device. The technical solution of this application is implemented as follows: Firstly, a memory management method is provided, including: In response to a detected kernel pressure stall event, the system's memory information is obtained; the kernel pressure stall event is a trigger event generated when the memory pressure exceeds a threshold. The memory reclamation improvement performance of the system is determined based on memory information and memory blocking duration of kernel stress stall events. The memory reclamation improvement performance is related to the amount of change in memory blocking duration of kernel stress stall events in adjacent statistical periods. Based on memory reclamation to improve performance, memory stress levels are determined so that target components can manage memory based on these levels.

[0005] In some embodiments, determining the memory reclamation improvement performance of the system based on memory information and the memory blocking duration of kernel stress stall events includes: determining the decrease in memory blocking duration of kernel stress stall events within adjacent statistical periods based on the memory blocking duration of kernel stress stall events in the current statistical period and the previous statistical period; determining the reclamation activity of the system based on memory information; wherein, the reclamation activity is used to characterize the reclamation operation intensity of the system in reclaiming memory; and determining the memory reclamation improvement performance of the system within adjacent statistical periods based on the decrease in memory blocking duration and the reclamation activity.

[0006] In some embodiments, memory information includes the number of memory pages scanned by the system at each sampling time; determining the recycling activity of the system based on the memory information includes: determining the scanning speed based on the difference between the number of memory pages scanned at at least two sampling times and the time interval between the two sampling times; and determining the recycling activity of the system based on the scanning speed and a preset scanning threshold.

[0007] In some embodiments, the scanning speed includes a first scanning speed obtained using a first recycling method and a second scanning speed obtained using a second recycling method, wherein the first recycling method adopts a background asynchronous recycling mechanism and the second recycling method adopts a direct synchronous recycling mechanism; the memory recycling activity is determined based on the first scanning speed with a first weight and the second scanning speed with a second weight.

[0008] In some embodiments, determining the memory stress level based on memory reclamation performance improvement includes: determining the system's memory stress level based on memory information; wherein the memory stress level is used to characterize the proportion of unused memory in the system's memory; and determining the memory stress level based on the memory stress level and memory reclamation performance improvement.

[0009] In some embodiments, the memory information includes the system's current remaining memory information and the system's current remaining space information; determining the system's memory stress based on the memory information includes: determining the free memory stress based on the remaining memory information and the memory water level threshold; determining the space stress based on the remaining space information and preset space information; and determining the memory stress based on the free memory stress and the space stress.

[0010] In some embodiments, determining the free memory stress based on remaining memory information and a preset memory threshold includes: determining the free memory stress as a preset value when the remaining memory information is not less than the memory waterline threshold; and taking the proportion of the difference between the remaining memory information and the memory waterline threshold in the memory waterline threshold as the free memory stress when the remaining memory information is less than the memory waterline threshold.

[0011] In some embodiments, the memory stress level is determined based on memory stress and memory reclamation improvement performance, including: determining the memory stress level based on memory stress, reclamation activity, and memory reclamation improvement performance with preset weights.

[0012] Secondly, a memory management system is provided, including a kernel component, a monitoring component, and a system service component. The kernel component triggers a kernel pressure stall event when memory pressure exceeds a threshold. The monitoring component obtains system memory information in response to the detected kernel pressure stall event. The system service component determines the system's memory reclamation improvement performance based on the memory information and the memory blocking duration of the kernel pressure stall event, wherein the memory reclamation improvement performance is related to the change in memory blocking duration in adjacent kernel pressure stall events. Based on the memory reclamation improvement performance, a memory pressure level is determined so that the target component performs memory management based on the memory pressure level.

[0013] Thirdly, an electronic device is provided, comprising: a memory and a processor, wherein the memory stores a computer program executable on the processor, and the processor executes the program to implement the method of any one of the first aspects of the embodiments of this application.

[0014] It should be understood that the above general description and the following detailed description are merely exemplary and explanatory, and are not intended to limit the technical solutions of this application. Attached Figure Description

[0015] Figure 1A This is a schematic diagram illustrating the implementation process of a memory management method provided in an embodiment of this application; Figure 1B This is a schematic diagram of the implementation flow of a memory management method provided in an embodiment of this application. Figure 2 ; Figure 2 This is a schematic diagram of the composition structure of a memory management system provided in an embodiment of this application; Figure 3 This is a schematic diagram of a pressure index provided in an embodiment of this application; Figure 4 This is a schematic diagram illustrating how memory pages are compressed and placed into zRam space, as provided in an embodiment of this application. Figure 5 This is a schematic diagram illustrating the behavior of memory at different water levels, provided in an embodiment of this application. Figure 6 This is a schematic diagram illustrating the implementation of a memory management system provided in an embodiment of this application; Figure 7 This is a flowchart illustrating the implementation of memory pressure calculation provided in an embodiment of this application; Figure 8 This is a schematic diagram of the hardware entity of an electronic device provided in the embodiments of this application.

[0016] It should be noted that the terms "first" and "second" mentioned above are only used to distinguish between different options and do not represent the degree of superiority or inferiority of the options or their priority in the implementation process. Detailed Implementation

[0017] To make the objectives, technical solutions, and advantages of this application clearer, the technical solutions of this application are further described in detail below with reference to the accompanying drawings and embodiments. The described embodiments should not be regarded as limitations on this application. All other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.

[0018] In the following description, references to "some embodiments" refer to a subset of all possible embodiments. It is understood that "some embodiments" may be the same or different subsets of all possible embodiments and may be combined with each other without conflict. The terms "first / second / third" are used merely to distinguish similar objects and do not represent a specific ordering of objects. It is understood that "first / second / third" may be interchanged in a specific order or sequence where permitted, so that the embodiments of this application described herein can be implemented in an order other than that illustrated or described herein.

[0019] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this application pertains. The terminology used herein is for descriptive purposes only and is not intended to be limiting of this application.

[0020] Currently, embedded devices such as mobile phones and tablets have relatively limited computing and storage resources. When users run multiple applications simultaneously, system memory resources easily become strained. If this memory strain is not effectively alleviated, it can cause system-level stuttering, slow response times, and other problems, severely impacting the user experience. To maintain system smoothness, when memory strain is detected, the operating system and various upper-layer business modules need to coordinate their responses and adjust their memory usage behavior in a timely manner so that the system can recover from a low memory state as quickly as possible.

[0021] In this process, accurately and timely identifying and measuring system memory pressure is the primary and fundamental problem to be solved. Failure to accurately assess memory pressure typically leads to two adverse consequences: first, modules in the system may overuse memory due to a lack of awareness of the pressure, potentially triggering Out of Memory (OOM) and causing application crashes or system freezes; second, modules may be overly conservative and fail to fully utilize existing memory resources, resulting in the device's performance potential being underutilized. Both situations impair the user experience.

[0022] In related technologies, memory pressure is typically assessed using the following method: Calculating Pressure Stall Information (PSI). This involves calculating the proportion of time a task in the system is blocked waiting for memory resources within a past sampling period. PSI can be statistically analyzed from two dimensions: a "Some" dimension (the proportion of time at least one task in the system is blocked waiting for memory resources within the sampling period) and a "Full" dimension (the proportion of time all non-idle tasks in the system are blocked waiting for memory resources within the sampling period). Figure 3 As shown, PSI can be measured from two dimensions: one is the Some dimension, where at least one task in the system is blocked while waiting for memory resources; the other is the Full dimension, where all non-idle tasks in the system are blocked while waiting for memory resources.

[0023] Although this method statistically analyzes memory blocking status from two dimensions, it still only considers memory blocking status. When determining the current memory pressure, the assessment of memory pressure is still not accurate or comprehensive enough, and it cannot reflect the potential accumulation of memory pressure in a timely or accurate manner. This leads to deviations in the formulation and triggering of system management strategies.

[0024] In view of the above problems, embodiments of this application provide a memory management method, system, and electronic device. The technical solution of this application will be described in detail below with reference to the accompanying drawings.

[0025] The memory management methods provided in the embodiments of this application can be executed by system service components. These system service components can be monitoring modules within the operating system kernel or memory management agents running in user space. In other words, the memory management methods in the embodiments of this application can be executed by kernel modules, by user-space components, or through collaborative interaction between the kernel and user space.

[0026] Figure 1 is a schematic diagram of the implementation flow of a memory management method provided in an embodiment of this application. The following will be described in conjunction with the steps shown in Figure 1. It should be noted that the memory management method in Figure 1 is illustrated using a system service component as the execution subject. As shown in Figure 1, the method includes the following steps S101 to S103.

[0027] Step S101: In response to the detected kernel pressure stall event, obtain the system's memory information.

[0028] Among them, the kernel pressure stall event is a trigger event generated when the memory pressure exceeds the threshold. It can be an asynchronous trigger event generated by the Linux (an operating system) kernel layer pressure monitoring component (such as the PSI component). It can be used to characterize that the current memory pressure state of the system has reached or exceeded the preset critical threshold. Specifically, the kernel pressure stall event can be triggered by statistically analyzing the proportion of memory blocking time based on the PSI mechanism in related technologies. The implementation steps are consistent with those in related technologies, and the specific details will not be repeated here.

[0029] In this embodiment, the system service component that performs the operation of obtaining system memory information in response to a detected kernel pressure stall event can be deployed in the kernel space or as a monitoring service in user space.

[0030] In one possible implementation, the component can be used to monitor memory status in real time and immediately initiate a memory information acquisition process when a kernel pressure stall event is detected; in another possible implementation, the component can also initiate a memory information acquisition process periodically and immediately read the acquired memory information when a kernel pressure stall event is detected.

[0031] The memory information may include a set of data reflecting the current memory resource allocation, usage, and idle status of the embedded device. This set of data may include, but is not limited to, multiple database types, such as: usage-based memory information, activity-based information, allocation efficiency-based information, and cache and swap information.

[0032] In this embodiment, the PSI function can be enabled in the kernel, and a monitor for memory pressure can be configured. This monitor can preset one or more pressure thresholds for the "some" and / or "full" indicators of PSI. When the PSI value calculated by the kernel exceeds the configured threshold, the kernel can generate an event and notify the memory management device through an event file polling mechanism or a system call mechanism. After the memory management device receives the triggered kernel pressure stall event, it immediately obtains comprehensive memory information by reading the interface exposed by the kernel.

[0033] Step S102: Determine the system's memory reclamation improvement performance based on memory information and the memory blocking duration of kernel pressure stall events.

[0034] Among them, the improvement in memory reclamation performance is related to the change in memory blocking duration during kernel pressure stall events in adjacent statistical periods.

[0035] In this embodiment, memory blocking duration refers to the total time a task is in a waiting state due to insufficient memory resources, calculated by a kernel stress monitoring mechanism (such as PSI) within a specific sampling period that triggers the kernel stress misalignment event. It can be used to quantify the total performance loss caused by memory shortages to task execution within that statistical window. In other words, memory blocking duration can be an instantaneous or short-term performance indicator, reflecting the severity of the memory performance bottleneck experienced by the system within the recently concluded time window when the stress exceeded the threshold.

[0036] The adjacent statistical period refers to one or more statistical windows of equal length that are closely adjacent to the statistical period that triggered the current stress event on the time axis. For example, if the average value of the PSI over the past 10 seconds triggers an event, the adjacent statistical period could refer to the time period of 10-20 seconds before the triggering event, or a series of consecutive 10-second windows of equal length.

[0037] Considering that the inherent recycling mechanism in the system is a continuously running, dynamic background process that changes dynamically with system load and memory status, in this embodiment, the actual working efficiency of the inherent recycling mechanism in the system within this time window can be determined by analyzing the correlation between the change in memory blocking duration and the memory information reflecting recycling activities in adjacent statistical periods.

[0038] If the memory blocking duration decreases significantly (negative change), and the memory information shows that the system's reclaimable memory has been effectively released, it indicates that the memory reclamation mechanism is functioning well, successfully converting idle memory resources into usable resources, thereby alleviating task blocking. Conversely, if the memory blocking duration remains high or continues to rise (zero or positive change), but the memory information shows that there is no considerable reclaimable memory left in the system, or reclamation actions are frequent but have little effect, it indicates that the memory reclamation mechanism is unable to effectively release resources from the existing memory layout to meet the demand, and its improvement capability is poor. Therefore, the system's memory reclamation improvement efficiency can be determined based on memory information and the memory blocking duration of kernel pressure stall events. This indicator of the system's memory reclamation improvement efficiency can serve as an indicator of the system's self-regulation capability and determine whether subsequent external intervention is needed and the extent of such intervention.

[0039] Step S103: Based on memory reclamation to improve performance, determine the memory pressure level so that the target component can manage memory based on the memory pressure level.

[0040] The memory stress level can include multiple stress levels, each with a different level of memory strain.

[0041] In the embodiments of this application, when the memory pressure level is low, the target component can maintain a normal memory usage strategy without imposing additional memory restrictions; when the level is medium, the target component can appropriately reduce the memory allocation of non-critical tasks; when the level is high or very high, the target component needs to strictly control memory usage, and may even actively release some cache or close background processes to reduce the overall memory consumption of the system.

[0042] In practice, service components can use a weighted algorithm to calculate the final memory pressure level based on memory reclamation performance improvements and other memory status indicators (such as available memory stress and memory reclamation activity), and then pass this memory pressure level to each target component. After receiving the memory pressure level, the target components will adjust their memory usage behavior according to their respective business logic, thereby achieving more granular memory management.

[0043] By adopting the above technical solution, when a stress event is triggered, multi-dimensional memory information such as memory blocking duration and system memory information is captured simultaneously. Furthermore, by analyzing the changes in memory blocking duration between adjacent periods and combining them with data reflecting reclamation activities in the memory information, the memory reclamation improvement performance of the system can be determined. This avoids the problem of using a single dimension to measure memory pressure, which makes the assessment of memory pressure inaccurate and incomplete. Moreover, the memory pressure can be dynamically graded through the system's memory reclamation improvement performance, ensuring that the formulation and triggering timing of memory management strategies are adapted to memory pressure.

[0044] Since the decrease in memory blocking duration within adjacent statistical periods can be achieved through different system memory reclamation mechanisms, and the execution costs of different memory reclamation mechanisms are different. For example, memory reclamation mechanisms can include low-intensity reclamation and high-intensity reclamation. Low-intensity reclamation may include, but is not limited to, clearing only inactive page caches and compressing cache blocks, which has low system overhead and little impact on user experience. High-intensity reclamation may include, but is not limited to, killing a large number of background processes and reclamating active anonymous pages, which has high system overhead and can lead to negative experiences such as application restarts and foreground lag.

[0045] In some scenarios, a decrease in blocking time may be a short-term effect caused by excessive resource reclamation, rather than the true effectiveness of the reclamation strategy. For example, the system achieves a significant reduction in blocking time by killing all background processes, but at this point, there are no more low-priority resources available for reclamation. If memory pressure recurs later, it can only be alleviated by killing high-priority processes or foreground processes, leading to system stability risks.

[0046] Therefore, in some embodiments, the recycling activity can be obtained by quantifying the data reflecting recycling activities in the memory information. The cost incurred by the system to reduce blocking time can be evaluated by using the recycling activity parameter, thereby correcting the memory recycling performance of the system. Figure 1B This is a schematic diagram of the implementation flow of a memory management method provided in an embodiment of this application. Figure 2 ,like Figure 1B As shown, step S102 can be achieved through the following steps.

[0047] Step S1021: Based on the memory blocking duration in the kernel pressure stall event in the current statistical period and the previous statistical period, determine the decrease in memory blocking duration in the kernel pressure stall event in the adjacent statistical period.

[0048] In this step, the memory monitoring service in the system maintains a time-series buffer. The time-series buffer is used to record the memory blocking duration and timestamp corresponding to each kernel stress stall event. When the latest kernel stress stall event is detected, the memory blocking duration and timestamp corresponding to the current kernel stress stall event can be read, as well as the memory blocking duration recorded at the end of the previous adjacent statistical period can be retrieved from the buffer. Then, the difference between the blocking duration of the current period and the previous period can be calculated, which is the amount of decrease in memory blocking duration in kernel stress stall events within adjacent statistical periods.

[0049] When the decrease in memory blocking duration is greater than 0, it indicates a decrease in pressure, and the larger the positive value, the more significant the relief effect. When the decrease in memory blocking duration is equal to 0, it indicates that the pressure has not changed. When the decrease in memory blocking duration is less than 0, it indicates an increase in pressure, and the larger the negative value, the more severe the deterioration.

[0050] Step 1022: Determine the system's recycling activity based on memory information.

[0051] Among them, the reclamation activity is used to characterize the intensity of the system's reclamation operation, which is the execution cost that the system pays to achieve the above-mentioned pressure change (the amount of change in the decrease of memory blocking time). It can reflect information such as the CPU and I / O resources consumed by the system to alleviate the pressure.

[0052] In this embodiment, the memory information may include memory information data reflecting memory reclamation activities. Specifically, the memory information data reflecting memory reclamation activities may include, but is not limited to, kernel-mode reclamation data and user-mode reclamation data. Kernel-mode reclamation data may be kernel-level data such as the number of pages scanned during memory reclamation, the number of direct reclamations, the number of pages written back, and the total duration of reclamation operations; user-mode reclamation data may be user-level data such as the number of background processes killed and the low memory callback execution level.

[0053] For example, memory information data reflecting memory reclamation activities can be read directly from interface files such as / proc / vmstat.

[0054] Once memory information data reflecting memory reclamation activities is obtained, corresponding weight parameters can be set for different memory parameters under each reclamation method. This is because different reclamation methods have different reclamation intensity, execution cost, and impact on system performance and user experience. Therefore, reclamation activity can be determined based on different memory information and corresponding weight parameters, which can reflect the differences between different reclamation methods.

[0055] For example, when performing low-intensity garbage collection mechanisms such as reclaiming inactive file pages and compressing cache blocks using kswapd, since its main cost is CPU cycles and limited I / O resources, it has a short blocking time for user tasks and will not damage the application's working set. Therefore, its corresponding counter (such as pgsteal_kswapd) can be assigned a lower first weight. On the other hand, when performing high-intensity garbage collection mechanisms such as swapping anonymous pages pswpout and terminating processes using OOM Killer, since swapping introduces huge disk I / O latency and killing processes directly leads to the loss of application state, its corresponding counter (such as pgsteal_direct) can be assigned a higher second weight.

[0056] Given memory information data reflecting memory reclamation activities, and the weight corresponding to each piece of memory information data, the system's reclamation activity level can be obtained by calculating the weighted sum of multiple memory information data. In this way, the system's reclamation activity level can accurately reflect the execution cost incurred by the system to alleviate memory pressure.

[0057] Step S1023: Determine the memory reclamation improvement performance of the system within adjacent statistical periods based on the decrease in change and the reclamation activity.

[0058] In this embodiment, the improved memory reclamation effect of the system is used to illustrate the stress relief effect brought about by the unit reclamation cost of the system.

[0059] Specifically, when the decrease in change is significantly greater than 0 and the recovery activity is low, it indicates that the system has achieved good results at a low cost. When the decrease in change is slightly greater than or equal to 0 and the recovery activity is high, it indicates that the system has paid a high cost but the effect is limited, and the recovery mechanism is already struggling. When the decrease in change is less than 0 and the recovery activity is high, it indicates that the system's recovery action is completely ineffective and the system is in a disabled state. When the decrease in change is greater than 0 but the recovery activity is abnormally high, it indicates that although the pressure has temporarily decreased, the system has paid a huge cost, which will lead to subsequent system lag issues.

[0060] For example, the ratio of the decrease in change to the recycling activity can be used as a measure of the improvement in memory recycling performance of the system over adjacent statistical periods.

[0061] By adopting the above technical solution, the intensity of memory reclamation operations can be determined based on memory information. This allows us to determine the execution cost required for the system to achieve a decrease in memory blocking time. This fills the gap in existing technologies that only focus on stress relief effects while ignoring reclamation costs. It enables us to determine the memory reclamation improvement efficiency based on stress relief effects and reclamation costs, accurately reflecting the potential accumulation of memory stress and avoiding system resource waste and system risks caused by ineffective or excessive reclamation.

[0062] Considering that when the kernel-mode memory reclamation mechanism is executed, the system kernel needs to scan memory page frames first to identify inactive pages and reclaimable pages before performing subsequent reclamation operations, in other words, memory scanning is a prerequisite for the kernel-mode memory reclamation mechanism. Therefore, the scanning speed of memory scanning can be used to characterize the intensity and urgency of the reclamation mechanism.

[0063] In some embodiments, the memory information includes the number of memory pages scanned by the system at each sampling time, so the above step S1022 can be implemented by the following steps T1 and T2.

[0064] Step T1: Determine the scanning speed based on the difference in the number of memory pages obtained from scanning at least two sampling times, and the time interval between the two sampling times.

[0065] The scanning speed can be expressed as the number of memory pages scanned by the system per unit time, and it can be used to measure the frequency of operation of the memory reclamation mechanism.

[0066] For low-intensity garbage collection mechanisms, the system only needs to clean up a small number of inactive pages to alleviate pressure, resulting in slow kernel scanning speeds (i.e., fewer pages scanned per unit time). In this case, garbage collection activity is low and system overhead is low. However, for high-intensity garbage collection mechanisms, a large number of reclaimable pages need to be found quickly, and even active pages may need to be scanned. This results in fast kernel scanning speeds (i.e., more pages scanned per unit time), leading to high garbage collection activity and high system overhead. Therefore, garbage collection activity can be quantified by scanning speed, directly mapping garbage collection intensity to execution cost. This method reflects the dynamic state of garbage collection more effectively than simply counting the number of pages scanned.

[0067] The scanning speed is calculated by dividing the difference in the number of memory pages scanned between two adjacent sampling times by the time interval. For example, in an embedded device, if 1000 memory pages are scanned at the first sampling time and 1500 memory pages are scanned at the second sampling time, and the time interval between the two sampling times is 5 seconds, then the scanning speed is (1500-1000) / 5 = 100 pages / second.

[0068] In this embodiment, the scanning speed can be determined by calculating the ratio of the difference in the number of memory pages scanned between two adjacent sampling times to the time interval, which can reflect the workload of the current memory reclamation mechanism in real time.

[0069] Step T2: Determine the system's recycling activity based on the scanning speed and preset scanning threshold.

[0070] The preset scanning threshold can be one or more pre-defined critical values, which can be used to map continuous scanning speed values ​​to different levels of recovery intensity ranges.

[0071] The preset scan threshold may include, but is not limited to, a baseline threshold and a critical threshold. The baseline threshold may be the scan speed of the system when it is idle or under low load. The critical threshold may be the scan speed of the system when it is under critical memory pressure and begins to frequently trigger direct reclamation.

[0072] Since the system's memory scan speed is an absolute physical quantity, for example, a scan speed of 100 pages per second is usually considered a fast scan on a small embedded device, but for a large memory server, this speed is only a slow scan. Therefore, by setting a scan threshold, the recycling activity determined based on the scan speed can be adapted to different devices.

[0073] In one possible implementation, the calculated scan rate can be compared with preset thresholds at different levels to directly map the scan rate to a predefined recycling activity level.

[0074] Specifically, if the scanning speed is not greater than the first-level threshold, the recycling activity can be determined as level one; if the scanning speed is greater than the first-level threshold but not greater than the second-level threshold, the recycling activity can be determined as level two; if the scanning speed is greater than the second-level threshold but not greater than the third-level threshold, the recycling activity can be determined as level three.

[0075] In another possible implementation, the ratio of the scan speed to a preset scan threshold can be used as the system's recycling activity. In this case, the preset scan threshold can be used as a baseline value.

[0076] This enables data normalization, allowing for comparison of recycling activity calculated on devices with different memory capacities and CPU performances; moreover, the ratio of scan speed to preset scan threshold can directly characterize the aggressiveness of pre-recycling behavior relative to the system baseline state.

[0077] In some embodiments, the scanning speed includes a first scanning speed obtained using a first recycling method and a second scanning speed obtained using a second recycling method, wherein the first recycling method employs a background asynchronous recycling mechanism and the second recycling method employs a direct synchronous recycling mechanism.

[0078] In this embodiment, the first reclamation method can refer to the system asynchronously performing memory reclamation operations during idle periods or low loads via a background thread (such as KSwapd) during operation. This background asynchronous reclamation mechanism does not block the execution of current tasks and is suitable for handling non-urgent memory release needs. For example, in a Linux system, when memory usage approaches a set threshold, KSwapd is awakened to gradually swap out infrequently used memory pages to disk or compressed storage space (such as zRAM), thereby reducing physical memory usage.

[0079] The second type of memory reclamation can refer to a synchronous memory reclamation process triggered immediately when an application requests memory allocation if the system detects insufficient available memory. This direct synchronous reclamation mechanism temporarily blocks the execution of the current process to ensure that enough memory is released for application use in a timely manner. For example, under the Direct Reclaim mechanism, when a process fails to allocate memory, it will actively perform page scanning and reclamation until its memory needs are met.

[0080] The above step S1022 may also include the following step T3.

[0081] Step T3: Determine the memory reclamation activity based on the first scan speed with a first weight and the second scan speed with a second weight.

[0082] Among them, the first weight and the second weight can be coefficients used to adjust the degree of influence of different reclamation methods on memory reclamation activity.

[0083] Since the background asynchronous garbage collection mechanism has a relatively small impact on the overall system performance, but runs frequently, while the direct synchronous garbage collection mechanism runs less often, each run of the direct synchronous garbage collection mechanism significantly affects the user experience. Therefore, the first weight can be set to be less than the second weight.

[0084] In this step, the weighted sum of the first and second scan speeds can be used as the memory reclamation activity level. The memory reclamation activity level determined in this way can be used to measure the intensity of the system's current memory reclamation mechanism. Specifically, the reclamation activity level can reflect the operation of both the background asynchronous reclamation mechanism and the direct synchronous reclamation mechanism.

[0085] By adopting the above technical solution and introducing differentiated weights for the scanning speeds of different reclamation mechanisms, a refined calculation of the overall cost of system memory reclamation behavior is achieved. By introducing two different reclamation methods and their corresponding weighting mechanisms, it is possible to more accurately determine whether the current system is in a memory-scarce state, thus providing a reliable basis for subsequent resource scheduling and optimization decisions, and effectively improving system stability and response efficiency.

[0086] Considering that when using memory pressure levels to guide target components in memory management, relying solely on memory reclamation performance improvement to determine the memory pressure level can lead to misjudgments. Specifically, when a system experiences frequent triggering of reclamation paths due to localized high-intensity memory allocation within a short period, it will detect a decrease in memory reclamation performance improvement and an increase in blocking time. However, if the overall available memory in the system is still sufficient (i.e., low memory stress), this pressure event will be a momentary fluctuation, and the system has sufficient resource buffers to absorb the impact without external intervention. Conversely, if available memory is low (high memory stress), even if the current reclamation performance is acceptable, the system is already on the verge of resource exhaustion and requires early warning.

[0087] Therefore, in some embodiments, the memory stress level can also be determined by identifying the memory stress level used to characterize the system resource inventory, based on the memory stress level and memory reclamation improvement performance. Step S103 described above can be implemented through steps S1031 and S1032.

[0088] Step S1031: Determine the system's memory pressure based on the memory information.

[0089] Among them, memory stress is used to characterize the proportion of unused memory in the system's memory. It can be used as a quantitative indicator to measure whether the system's current available memory is under stress.

[0090] In this embodiment, the memory information may include memory information data reflecting memory usage. Specifically, the memory information data reflecting memory usage may include, but is not limited to, the amount of physical memory available and the amount of swap space available. The amount of physical memory available may be free memory, reclaimable page cache, and reclaimable cache blocks; the amount of swap space available may be the remaining, available swap space, where the swap space may be the storage area (which may be a disk partition or zRAM compressed memory block) to which the system swaps out inactive memory pages when physical memory is insufficient.

[0091] For example, the amount of physical memory available can be read directly from interface files such as / proc / meminfo, and the amount of swap space available can also be read directly from interface files such as / proc / meminfo.

[0092] In one possible implementation, given memory information data reflecting memory usage, the percentage of available memory relative to total memory can be calculated, and this percentage can be used as the system's memory stress level.

[0093] Available memory may include, but is not limited to, the system's current remaining memory information (Free Memory) and the system's current remaining space information (Swap Free).

[0094] In this implementation, physical memory and swap space can be treated as homogeneous resources and summed together. For example, firstly, the total available resources (Available_Global) can be calculated: Available_Global = Free Memory + SwapFree; then, the total resources (Total_Global) can be calculated: Total_Global = MemTotal + SwapTotal, where MemTotal is the total physical memory and SwapTotal is the total swap space.

[0095] However, considering that using swap space (especially disk swapping) can lead to high I / O latency and performance sacrifice, another possible implementation is to introduce weighting coefficients to quantify the performance cost difference between physical memory and swap space.

[0096] In this implementation, different weight parameters can be pre-set for physical memory and swap space. Specifically, the weight parameter for physical memory can be set to a baseline, such as 1; and the weight parameter for swap space can be set to a value less than the weight parameter for physical memory. The smaller the weight parameter for swap space, the lower the unit availability value of swap space, and the greater the risk of its consumption.

[0097] Given the weight parameters for physical memory and swap space, we can calculate the weighted total available resources and total resources. For example, the weight parameter for physical memory can be represented as α, and the weight parameter for swap space can be represented as β. The weighted total available resources (Available_Weighted) = α × MemAvailable + β × SwapFree; the weighted total resources (Total_Weighted) = α × MemTotal + β × SwapTotal.

[0098] The system's memory pressure can then be calculated using the following formula: Memory stress = (Available_Weighted / Total_Weighted) × 100%.

[0099] In another possible implementation, the system's memory stress can be determined based on available memory and a preset memory threshold.

[0100] The memory information includes the system's current remaining memory and the system's current remaining space.

[0101] In this implementation, the free memory pressure can be determined first based on the remaining memory information and the memory water level threshold. Then, the space pressure can be determined based on the remaining space information and the preset space information. Finally, the memory pressure can be determined based on the free memory pressure and the space pressure.

[0102] The memory water level threshold can be a threshold used to distinguish memory status, which can be High, Low, and Min. When the remaining memory is greater than High, it means that the memory is sufficient; when the remaining memory is less than Low, it means that the memory is starting to be tight; when the remaining memory is less than Min, it means that the memory is very tight.

[0103] Specifically, when the remaining memory information is not less than the memory water level threshold, the free memory tension is determined to be a preset value; and when the remaining memory information is less than the memory water level threshold, the proportion of the difference between the remaining memory information and the memory water level threshold in the memory water level threshold is used as the free memory tension.

[0104] The default value can be 0.

[0105] Step S1032: Determine the memory stress level based on memory pressure and memory reclamation performance improvement.

[0106] In this step, the memory stress level, which characterizes the static resource reserve, and the memory reclamation improvement performance, which characterizes the dynamic adjustment efficiency of the system, can be used as orthogonal input dimensions. Through a preset decision logic, the memory pressure level is output.

[0107] Specifically, memory stress and memory reclamation performance can each be assigned a different weight parameter to quantify and adjust the importance of these two dimensions in the final stress level determination.

[0108] In one possible implementation, weights can be configured based on the device's memory capacity.

[0109] For example, for devices with limited memory capacity and fixed hardware specifications (such as low-end embedded devices), the weight for memory stress can be set higher than the weight for improving performance through memory reclamation. This is because their resource buffer is small, and the absolute amount of available memory is a more pressing constraint, requiring greater sensitivity to this. Conversely, for devices with ample memory but bottlenecks in CPU or storage I / O, the weight for memory stress can be set slightly lower than the weight for improving performance through memory reclamation, or the two weights can be balanced. This allows the focus to be placed on the efficiency cost of the reclamation process itself.

[0110] In another possible implementation, the weights can be dynamically fine-tuned based on the device's operating status.

[0111] For example, in interactive front-end scenarios, the performance weight of memory reclamation can be increased, because lag caused by inefficient reclamation will directly damage the user experience; while in background batch processing scenarios, the weight of memory stress can be increased, which can ensure that long-running tasks have enough memory resources to complete and avoid interruption due to resource exhaustion.

[0112] In this step, a weighted sum of memory stress and memory reclamation performance can be calculated, and the corresponding memory stress level can be determined based on the range of the weighted sum. Different ranges of the weighted sum can correspond to different memory stress levels.

[0113] By adopting the above technical solution and introducing two key indicators—memory stress and memory reclamation performance improvement—the memory pressure status of the target operating system can be more accurately assessed. This allows for timely identification of memory resource bottlenecks, enabling reasonable adjustments to the target operating system's behavior and ultimately improving overall performance and user experience.

[0114] In some embodiments, the memory stress level can also be determined based on preset weights of memory stress, reclamation activity, and memory reclamation improvement performance.

[0115] In this step, the calculation of memory stress level not only considers two indicators—memory stress and memory reclamation improvement performance—but also introduces reclamation activity as a third key factor. Furthermore, a weighted comprehensive evaluation is performed on these three factors using preset weights. This provides a more complete reflection of the system's current memory status and avoids misjudgments caused by a single indicator.

[0116] The preset weights refer to the coefficients assigned to the three indicators—memory stress, reclamation activity, and memory reclamation improvement performance—to reflect the importance of different indicators when comprehensively calculating memory stress levels. For example, if the system is more concerned with the current availability of memory, the weight of memory stress can be appropriately increased; conversely, if it wants to prioritize responding to changes in memory reclamation efficiency, the weight of memory reclamation improvement performance can be increased. These weights can be dynamically adjusted based on device type, user scenario, or performance requirements, thereby improving the system's flexibility and adaptability.

[0117] By combining and assigning appropriate weights to three metrics—memory stress, reclamation activity, and memory reclamation performance improvement—the system can more accurately identify different levels of memory pressure, such as low, medium, high, and severe, and take corresponding countermeasures, such as limiting background process startup, releasing cache resources, and adjusting scheduling priorities. This method effectively prevents system lag or crashes due to insufficient memory, while avoiding resource waste caused by excessive memory reclamation, thereby improving overall system stability and user experience.

[0118] Based on the above embodiments, this application also provides a memory management system. Figure 2 This is a schematic diagram of the composition structure of a memory management system provided in an embodiment of this application, as shown below. Figure 2 As shown, the memory management system 200 includes a kernel component 201, a monitoring component 202, and a system service component 203, wherein: Kernel component 201 is used to trigger a kernel pressure stall event when memory pressure exceeds a threshold. Monitoring component 202 is used to obtain system memory information in response to a detected kernel pressure stall event; System service component 203 is used to determine the memory reclamation improvement performance of the system based on memory information and the memory blocking duration of kernel pressure stall events. The memory reclamation improvement performance is related to the amount of change in memory blocking duration in adjacent kernel pressure stall events. Based on the memory reclamation improvement performance, the memory pressure level is determined so that the target component can perform memory management based on the memory pressure level.

[0119] The description of the system-side embodiments above is similar to that of the method embodiments above, and has similar beneficial effects. For technical details not disclosed in the device embodiments of this application, please refer to the description of the method embodiments of this application for understanding.

[0120] The following describes the application of the memory management method provided in the embodiments of this application in a real-world scenario, which can be used for memory pressure calculation on embedded devices such as mobile phones and tablets.

[0121] PSI was originally designed for computing clusters, which are systems composed of multiple servers, primarily used for high-performance computing or load balancing. However, embedded devices such as mobile phones and tablets have a critical characteristic: real-time processing capability. Foreground tasks need to respond to user input as quickly as possible, while background tasks have less stringent processing time requirements. PSI calculates the blocking time of all tasks together, which is unsuitable for embedded devices and negatively impacts the user experience.

[0122] In this embodiment, the current system memory pressure can be measured by defining the following three indicators, which are closer to the behavior characteristics of embedded devices when memory is low, thereby more completely and accurately describing the current system memory pressure and providing a better reference for each module in the system to formulate memory pressure response strategies.

[0123] The first metric can be the Available Memory Scarce Ratio (in %), which is the memory scarcity in the above embodiments; the second metric can be the Memory Reclaim Activity Ratio (in %), which is the reclamation activity in the above embodiments; and the third metric can be the Memory Reclaim Efficiency Ratio (in %), which is the memory reclamation improvement efficiency in the above embodiments.

[0124] In this embodiment, available memory stress directly reflects the current memory status of the system. Low free memory increases the probability of memory allocation failures when applications request memory, requiring memory allocation to proceed only after the system has completed memory reclamation, thus increasing application execution time. Therefore, calculating the available memory stress index provides a direct indication of memory pressure.

[0125] Available memory stress can be measured by free memory stress (hereinafter referred to as...). (represented by) and the available swap space ratio (hereinafter referred to as) It is measured from two aspects: (representation) and (other aspects).

[0126] Regarding free memory pressure, Linux systems can directly obtain the current system's free memory value. However, this is an absolute value, and considering the differences in memory size and virtual memory parameter configurations across different devices, using an absolute value to measure memory pressure is not applicable. In related technologies, different devices are configured with FreeMemory levels, ranging from High, Low, to Min. When Free Memory > High level, Free Memory is sufficient; when Free Memory falls below Low or even below Min level, memory pressure gradually increases.

[0127] In this embodiment, the current free memory can be compared with a benchmark value with clear system significance, thereby achieving a unified and standardized measurement across different devices. The degree of memory shortage can be quantified by the extent to which Free Memory is below the High water level.

[0128] It can be calculated using the following formula (1): Formula (1); in, This refers to the memory high level; free memory is the memory that can be directly used in the system. When free memory is not less than [a certain value], [the remaining amount is considered high]. hour, The value can be 0; otherwise, it depends on the free memory deviation. The degree, calculation .

[0129] It should be noted that different devices' kernels automatically calculate their respective values ​​based on their total memory. Therefore, the result calculated using this method It is naturally compatible with embedded devices of any configuration, eliminating interference caused by device differences.

[0130] Regarding the scarcity of available swap space, swap space is used to reclaim memory when the system's free memory is insufficient. For example... Figure 4As shown, Android devices use zRam to implement swap space. When system memory is low (MemFree is below the Low level), KSwapd is woken up, compresses some memory pages in the system, and puts them into zRam space. Through compression, memory usage is reduced. The more swap space is used, the less swap space remains. The system's ability to release physical memory through page compression / swapping is weakened, reflecting the potential for reclamation. When swap space is nearly exhausted, the system loses an important memory pressure buffer, which may directly lead to physical memory allocation failure or frequent OOM (Out of Memory) triggers, meaning that fewer memory pages can be reclaimed later.

[0131] It can be calculated using the following formula (2): Formula (2); in, This refers to the size of the Swap space configured on the device. This is the system's currently remaining available Swap space.

[0132] In obtaining and Then, the Available MemoryScarce Ratio (AMSR) can be calculated using the following formula (3): Formula (3); in, and These are pre-set weighting parameters for free memory stress and swap available space stress, respectively.

[0133] In this embodiment, the Linux system provides two memory reclamation mechanisms: KSwapd (asynchronous memory reclamation), which performs memory reclamation in the background when memory is insufficient, occurring asynchronously and not blocking tasks; and DirectReclaim, which triggers direct memory reclamation when a task requests memory and if memory is insufficient, blocking the currently requesting task. Therefore, this memory reclamation method is synchronous. The Linux system also sets three memory water levels: Min, Low, and High. Figure 5 As shown, the behavior of Memory Free memory at different watermarks can be referenced. Figure 5 .

[0134] like Figure 5As shown, when the system experiences memory pressure, both memory reclamation mechanisms will be triggered. With increasing memory pressure, the triggering frequency and runtime of both mechanisms will increase. There is a strong positive correlation between memory pressure and memory reclamation activity. Calculating the activity of memory reclamation mechanisms is a good representation of memory pressure. Both memory reclamation mechanisms have high operating costs. Although KSwapd does not block the current process, its CPU usage is high. In actual projects, when system memory pressure is high, KSwapd CPU usage is generally in the top 3, or even first, further increasing the system load. When the Direct Reclaim mechanism is triggered, it directly blocks the current task. Both reclamation mechanisms directly or indirectly increase application runtime.

[0135] In this embodiment, the activity levels of both memory reclamation mechanisms are considered when calculating memory pressure. The system does not provide direct information about the activity levels of these mechanisms. However, the Linux proc filesystem provides two fields: `pgscan_kswapd`, which indicates the number of memory pages scanned by KSwapd since boot, and `pgscan_direct`, which indicates the number of memory pages scanned by Direct Reclaim since boot.

[0136] While the number of memory pages scanned can indicate the workload of the memory reclamation mechanism, these two fields provide static, cumulative figures and cannot be used to represent dynamic, real-time metrics. To define the current activity level of memory reclamation, the KSwapd scan speed and Direct Reclaim scan speed can be calculated.

[0137] The KSwapd scan speed (f3) represents the number of memory pages scanned by KSwapd per unit time. The KSwapd scan speed can be calculated using the following formula (4): Formula (4); in, For two adjacent samples The difference in values ​​represents the number of memory pages scanned by KSwapd within the current sampling period; It can be the interval (in seconds) between two consecutive samples.

[0138] The Direct Reclaim scan speed (f4) represents the number of memory pages scanned per unit time. The Direct Reclaim scan speed is calculated using the following formula (5): Formula (5); in, Two adjacent samples The difference in values ​​represents the number of memory pages scanned by Direct Reclaim during this sampling period. It can be the interval (in seconds) between two consecutive samples.

[0139] After obtaining f3 and f4, a peak speed can be introduced to calculate the memory reclamation activity. The current scan speed is compared with the peak speed to calculate the memory reclamation activity. The peak speed is a predefined value that defines the maximum scan speed during the memory reclamation mechanism's operation. It should be noted that this value can be adjusted in actual implementation depending on the device.

[0140] The Memory Reclaim Activeness Ratio (MRAR) can be calculated using the following formula (6): Formula (6); in, , These are the weight parameters corresponding to the KSwapd scanning mechanism and the Direct Reclaim scanning mechanism, respectively. This is the maximum value of the preset KSwapd scan speed. This is the maximum value of the preset Direct Reclaim scan speed.

[0141] In this embodiment, the memory reclamation performance coefficient can be used to visually demonstrate the effectiveness of the system's memory reclamation work, which is helpful in assessing whether the current system's memory reclamation effect is ideal and also helps in predicting whether memory pressure will continue.

[0142] When system memory pressure is high, memory thrashing can easily occur, meaning that memory pages reclaimed by the system are quickly accessed again and need to be reloaded (refault). When thrashing occurs, the CPU spends most of its time handling page faults, I / O is busy, and memory pressure is not alleviated. A large number of reclaimed memory pages does not necessarily mean that memory pressure has been relieved. Therefore, simply using the above field (the number of reclaimed memory pages) to calculate memory reclamation efficiency is inaccurate.

[0143] Therefore, in this embodiment, when calculating the memory reclamation performance coefficient, it is necessary to consider both input and output. Input refers to Memory Reclamation Activity Ratio (MRAR), which is the cost the system pays for memory reclamation. Output also needs to be considered, which is the percentage decrease in the system's blocking time due to memory pressure between two adjacent statistical periods, representing the actual performance improvement (reduction in blocking time) brought about by the reclamation work. Using the value of the system's blocking time due to memory pressure, relative to the number of memory pages reclaimed, can more intuitively and accurately reflect the current memory pressure status.

[0144] The output can be calculated using the following formula (7): Formula (7); in, This represents the percentage of time spent in memory-intensive blocking during the current statistical period. This represents the percentage of time spent in memory-intensive blocking during the previous statistical period.

[0145] It should be noted that, in Not less than In this case, the output value can be 0.

[0146] The Memory Reclamation Performance Ratio (MRER) is calculated using the following formula (8): Formula (8).

[0147] Given AMSR, MRAR, and MRER, the memory pressure value (MPI) can be calculated using the following formula (9): Formula (9); in, , These are the pre-set weight parameters for AMSR and MRAR, respectively.

[0148] After obtaining the Memory Stress Index (MPI), the memory stress level can be determined based on the MPI.

[0149] Specifically, when 0% ≤ MPI ≤ 20%, the memory pressure level (MPL) can be determined as Low, indicating slight memory pressure; the system resources are sufficient, the recycling mechanism is efficient, and there is no performance risk. When 20% < MPI ≤ 60%, the memory pressure level (MPL) can be determined as Medium, indicating medium memory pressure; the system begins to sense the pressure, and the recycling mechanism may be working hard to maintain balance. When 60% < MPI ≤ 90%, the memory pressure level (MPL) can be determined as Critical, indicating high memory pressure; the system resources are significantly strained, the recycling mechanism may be struggling, and task execution begins to be affected. When 90% < MPI ≤ 100%, the memory pressure level (MPL) can be determined as SuperCritical, indicating an emergency memory pressure state; the system resources are on the verge of exhaustion, and there is a high risk of freezing or OOM (out of memory).

[0150] Adopting the above technical solution, on the one hand, it can be closer to the business requirements on the embedded device. By customizing the above three indicator parameters to calculate the current system memory pressure, the characterization of memory pressure is more comprehensive and accurate.

[0151] On the other hand, the calculation cost is lower, and system resources can be saved. This is because in the related technology, Kswapd is a separate process, and its CPU Usage needs to be collected for calculation. Direct Reclaim runs in the Kernel code and is triggered when memory allocation fails, and it can run in the task that currently requests memory. If its activity needs to be counted, it is necessary to place a stake at the relevant Kernel code. This method is not conducive to following GKI (Generic Kernel Image) and also has an impact on performance. In this solution, when measuring the activity of memory recycling, its activity is measured by calculating the number of memory pages scanned per unit time. This method occupies less CPU resources and is faster in calculation compared to the above traditional method. In actual implementation, the value calculated by this indicator has a strong positive correlation with the running duration and frequency of the memory recycling mechanism. When the system resources are strained, this solution can quickly and accurately reflect the activity level of the memory recycling mechanism and provide a better characterization of memory pressure.

[0152] On another front, the calculation of memory reclamation efficiency is more accurate. In related technologies, when system memory pressure is high, memory thrashing is prone to occur, meaning that memory pages reclaimed by the system are quickly accessed again and need to be reloaded (refault). When thrashing occurs, a large number of memory pages are reclaimed, but this does not necessarily mean that memory pressure has been alleviated. Simply using the above field (number of reclaimed memory pages) to calculate memory reclamation efficiency is inaccurate. This solution, however, combines PSI Memory statistics (i.e., the percentage of time the system is blocked due to memory shortage) with a custom metric, memory reclamation activity, when calculating memory reclamation efficiency. Compared to traditional calculation methods, this method more intuitively and accurately reflects the current memory reclamation efficiency.

[0153] Figure 6 This is a schematic diagram of an implementation of a memory management system provided in an embodiment of this application. The following will be combined with... Figure 6 The steps shown are explained as follows: Figure 6 As shown, the memory management system 300 may include a kernel 301, a low memory daemon (LKMD) 302, a system server 303, a memory stress level module (MLP) 304, and multiple subscription components 305.

[0154] In this embodiment, the Pressure Stall Information (PSI) mechanism built into kernel 301 can be used to continuously monitor the system memory resource status. When the memory pressure value calculated by PSI exceeds a preset threshold, the kernel will generate an epoll event through its provided / proc / pressure / memory interface. This epoll event can be used to indicate that the system has entered a memory stress state.

[0155] The PSI Trigger module inside the Low Memory Daemon (LKMD) 302 can be used to listen for PSI events issued by the kernel. After the PSI trigger captures the kernel's epoll event, LKMD immediately encapsulates the current critical memory information, forms a trigger message, and sends the message to the upper-layer system service process 303 via a Socket connection.

[0156] The system service process 303 may include an Lmkd connection thread and a Zui memory cleaner thread. The Lmkd connection thread, as an extension of ZuiMemoryCleaner, is responsible for communicating with LMKD and receiving stress event messages reported by it. The Zui memory cleaner thread is responsible for performing in-depth memory status analysis and stress assessment, and is used to execute the memory management method of any one of the embodiments of this application.

[0157] Specifically, after the Lmkd connection thread receives the LMKD message, it can notify the Zui memory cleaner thread that a stress event has occurred, thereby activating the Zui memory cleaner thread. The Zui memory cleaner thread not only retrieves statistical information from the kernel's / proc / vmstat and other interfaces based on the received information, but also performs composite calculations using the method proposed in the embodiments of this application, ultimately generating a precise quantified memory pressure value.

[0158] The Memory Pressure Level Module (MLP) 304 can map the received specific pressure values ​​into standardized, discrete memory pressure levels (e.g., L1-L4) and make global notifications. Specifically, the MLP broadcasts the determined memory pressure levels in real time and synchronously to all components that have subscribed to the signal through an efficient shared memory mechanism.

[0159] Multiple subscription components 305 may include, but are not limited to, various applications (App-1, App-2, ..., App-n) and system services (system service-1, ..., system service-n), which can autonomously adjust their memory usage behavior based on the received unified memory pressure level instruction. For example, when receiving a "moderate pressure" level, an application may proactively release non-critical caches; a system service may postpone non-urgent background tasks.

[0160] By adopting the above technical solution, the process starts from the kernel's initial perception, proceeds through LMKD event reporting, System Server's intelligent diagnosis, MPL level decision-making and broadcasting, and finally reaches the coordinated response of all subscribed components. This architecture can effectively engineer the memory pressure assessment method proposed in this application embodiment, realizing refined, intelligent, and systematic management of memory pressure.

[0161] Figure 7 This is a flowchart illustrating the implementation of memory pressure calculation provided in an embodiment of this application, such as... Figure 7 As shown, the method may include the following steps.

[0162] In step S401, the kernel detects a kernel pressure stall event and sends it to the system service process component via the low memory daemon.

[0163] Step S402: Determine whether a memory status check has been planned.

[0164] In this case, step S403 can be executed; If not, step S404 can be executed; Step S403: Cancel the planned memory status check and proceed to step S404.

[0165] Step S404: Obtain memory information and calculate the memory pressure level based on the memory information.

[0166] Step S405: Has the calculated memory pressure level changed?

[0167] In this case, step S406 can be executed; If not, step S407 can be performed; Step S406: Write the new memory pressure level to shared memory and publish an event to notify all subscribed components, and then execute step S407 after notification.

[0168] Step S407: Schedule the next round of memory status checks.

[0169] By adopting the above technical solution, on the one hand, PSI emergency events and periodic polling can be triggered together. This ensures that the system can respond instantly to sudden high pressure and continuously capture slowly accumulating hidden pressure, achieving a balance between timely response and comprehensive monitoring. On the other hand, publishing only when the MPL level changes avoids broadcasting duplicate and invalid status signals to all subscribers (applications and services), greatly reducing inter-process communication and processing overhead, and improving the overall system efficiency and energy efficiency.

[0170] The embodiments of this application are primarily used to improve the accuracy of memory pressure management. It should be noted that, in the embodiments of this application, if the above methods are implemented as software functional modules and sold or used as independent products, they can also be stored in a computer-readable storage medium. Based on this understanding, the technical solutions of the embodiments of this application, or the parts that contribute to related technologies, can be embodied in the form of a software product. This software product is stored in a storage medium and includes several instructions to cause an electronic device (which may be a personal computer, server, or network device, etc.) to execute all or part of the methods of the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, external hard drives, read-only memory (ROM), magnetic disks, or optical disks. Thus, the embodiments of this application are not limited to any specific hardware and software combination.

[0171] This application also provides an electronic device, including a memory and a processor, wherein the memory stores a computer program that can run on the processor, and the processor executes the computer program to implement any of the methods described above.

[0172] This application also provides a computer-readable storage medium having executable instructions stored thereon, which, when executed by a processor, implement the above-described method. The computer-readable storage medium can be transient or non-transient.

[0173] This application also provides a computer program product, which includes a computer program or instructions that, when executed by a processor, implement some or all of the steps in any of the above-described methods. The computer program product can be implemented specifically through hardware, software, or a combination thereof. In one optional embodiment, the computer program product is specifically embodied in a computer storage medium; in another optional embodiment, the computer program product is specifically embodied in a software product, such as a software development kit (SDK), etc.

[0174] It should be noted that, Figure 8 This is a schematic diagram of the hardware entity of an electronic device provided in the embodiments of this application, such as... Figure 8 As shown, the hardware entity of the electronic device 800 includes: a processor 801, a communication interface 802, and a memory 803, wherein: The processor 801 typically controls the overall operation of the electronic device 800.

[0175] Communication interface 802 enables electronic devices to communicate with other terminals or servers via a network.

[0176] The memory 803 is configured to store instructions and applications executable by the processor 801, and can also cache data to be processed or already processed (e.g., image data, audio data, voice communication data, and video communication data) in the processor 801 and various modules in the electronic device 800. It can be implemented using flash memory or random access memory (RAM). Data transfer between the processor 801, the communication interface 802, and the memory 803 can be performed via bus 804.

[0177] It should be noted that the descriptions of the storage medium and device embodiments above are similar to those of the method embodiments above, and have similar beneficial effects. For technical details not disclosed in the storage medium and device embodiments of this application, please refer to the descriptions of the method embodiments of this application for understanding.

[0178] The above embodiments are merely preferred embodiments provided to fully illustrate the present invention, and the scope of protection of the present invention is not limited thereto. Equivalent substitutions or modifications made by those skilled in the art based on the present invention are all within the scope of protection of the present invention.

[0179] It should be understood that the phrase "one embodiment" or "an embodiment" throughout the specification means that a specific feature, structure, or characteristic related to the embodiment is included in at least one embodiment of this application. Therefore, "in one embodiment" or "in an embodiment" appearing throughout the specification does not necessarily refer to the same embodiment. Furthermore, these specific features, structures, or characteristics can be combined in any suitable manner in one or more embodiments. It should be understood that in the various embodiments of this application, the sequence numbers of the above steps / processes do not imply a sequential order of execution; the execution order of each step / process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of this application. The sequence numbers of the above embodiments of this application are merely descriptive and do not represent the superiority or inferiority of the embodiments.

[0180] It should be noted that, in this document, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Unless otherwise specified, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes that element.

[0181] Furthermore, the terms "first" and "second" are used for descriptive purposes only and should not be construed as indicating or implying relative importance or implicitly specifying the number of technical features indicated. Thus, a feature defined as "first" or "second" may explicitly or implicitly include at least one of that feature. In the description of this application, "multiple" means at least two, such as two, three, etc., unless otherwise explicitly specified.

[0182] In this application, unless otherwise expressly specified and limited, the terms "installation," "connection," "joining," and "fixing," etc., should be interpreted broadly. For example, they can refer to a fixed connection, a detachable connection, or an integral part; they can refer to a mechanical connection or an electrical connection; they can refer to a direct connection or an indirect connection through an intermediate medium; they can refer to the internal communication of two components or the interaction between two components, unless otherwise expressly limited. Those skilled in the art can understand the specific meaning of the above terms in this application according to the specific circumstances.

[0183] In this application, unless otherwise expressly specified and limited, "above" or "below" the second feature can mean that the first feature is in direct contact with the second feature, or that the first feature is in indirect contact with the second feature through an intermediate medium. Furthermore, "above," "on top of," and "over" the second feature can mean that the first feature is directly above or diagonally above the second feature, or simply that the first feature is at a higher horizontal level than the second feature. "Below," "below," and "under" the second feature can mean that the first feature is directly below or diagonally below the second feature, or simply that the first feature is at a lower horizontal level than the second feature.

[0184] In the description of this specification, the references to terms such as "one embodiment," "some embodiments," "example," "specific example," or "some examples," etc., refer to specific features, structures, materials, or characteristics described in connection with that embodiment or example, which are included in at least one embodiment or example of this application. In this specification, the illustrative expressions of the above terms do not necessarily refer to the same embodiment or example. Furthermore, the specific features, structures, materials, or characteristics described may be combined in any suitable manner in one or more embodiments or examples. Moreover, without contradiction, those skilled in the art can combine and integrate the different embodiments or examples described in this specification, as well as the features of different embodiments or examples.

[0185] The above are merely preferred embodiments of this application and are not intended to limit the scope of protection of this application. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of this application should be included within the scope of protection of this application.

Claims

1. A memory management method, comprising: In response to a detected kernel pressure stall event, the system's memory information is acquired; wherein, the kernel pressure stall event is a trigger event generated when the memory pressure exceeds a threshold. Based on the memory information and the memory blocking duration of the kernel pressure stall event, the memory reclamation improvement performance of the system is determined. The memory reclamation improvement performance is related to the change in the memory blocking duration of the kernel pressure stall event in adjacent statistical periods. Based on the improved memory reclamation performance, a memory stress level is determined so that the target component can manage memory based on the memory stress level.

2. The method according to claim 1, wherein determining the memory reclamation improvement performance of the system based on the memory information and the memory blocking duration of the kernel pressure stall event includes: Based on the memory blocking duration of the kernel pressure stall event in the current statistical period and the previous statistical period, determine the amount of decrease in the memory blocking duration of the kernel pressure stall event in adjacent statistical periods. Based on the memory information, the reclamation activity level of the system is determined; wherein, the reclamation activity level is used to characterize the intensity of the system's memory reclamation operation. Based on the decrease in change and the recycling activity, the memory recycling improvement performance of the system within adjacent statistical periods is determined.

3. The method according to claim 2, wherein the memory information includes the number of memory pages scanned by the system at each sampling time; determining the recycling activity of the system based on the memory information includes: The scanning speed is determined based on the difference in the number of memory pages obtained from scanning at least two sampling times, and the time interval between the two sampling times; The recycling activity of the system is determined based on the scanning speed and the preset scanning threshold.

4. The method according to claim 3, wherein the scanning speed includes a first scanning speed obtained using a first recovery method and a second scanning speed obtained using a second recovery method, wherein, The first recycling method employs a background asynchronous recycling mechanism, and the second recycling method employs a direct synchronous recycling mechanism; the method further includes: The memory reclamation activity is determined based on the first scan speed with a first weight and the second scan speed with a second weight.

5. The method according to any one of claims 2-4, wherein determining the memory stress level based on the memory reclamation improvement performance includes: Based on the memory information, the memory stress of the system is determined; wherein, the memory stress is used to characterize the proportion of unused memory in the system's memory; The memory stress level is determined based on the memory stress level and the memory reclamation improvement performance.

6. The method according to claim 5, wherein the memory information includes the current remaining memory information of the system and the current remaining space information of the system; Determining the system's memory pressure based on the memory information includes: Based on the remaining memory information and the memory water level threshold, the free memory pressure is determined; The spatial tension is determined based on the remaining space information and the preset space information; The memory stress is determined based on the free memory stress and the space stress.

7. The method according to claim 6, wherein determining the free memory pressure based on the remaining memory information and a preset memory threshold includes: If the remaining memory information is not less than the memory water level threshold, the free memory tension is determined to be a preset value; If the remaining memory information is less than the memory water level threshold, the difference between the remaining memory information and the memory water level threshold, as a percentage of the memory water level threshold, is used as the free memory tension.

8. The method according to claim 5, wherein determining the memory stress level based on the memory tension and the memory reclamation improvement performance includes: The memory pressure level is determined based on the memory stress, the recycling activity, and the memory recycling improvement performance, all with preset weights.

9. A memory management system, comprising a kernel component, a monitoring component, and a system service component; wherein, The kernel component is used to trigger a kernel pressure stall event when memory pressure is detected to exceed a threshold. The monitoring component is used to obtain system memory information in response to a detected kernel pressure stall event; The system service component is used to determine the memory reclamation improvement performance of the system based on the memory information and the memory blocking duration of the kernel pressure stall event. The memory reclamation improvement performance is related to the amount of change in the memory blocking duration in adjacent kernel pressure stall events. Based on the improved memory reclamation performance, a memory stress level is determined so that the target component can manage memory based on the memory stress level.

10. An electronic device, comprising: The method includes a memory and a processor, the memory storing a computer program that can run on the processor, the processor executing the program to implement the method of any one of claims 1 to 8.