A method for locating memory leaks
By monitoring CPU utilization and memory usage in real time, adjusting the memory usage threshold dynamically, and analyzing memory leaks using hash tables and causal relationship models, the problem of difficulty in accurately positioning memory leak detection in the existing technology is solved, and more efficient memory management and system stability are achieved.
Patent Information
- Application Number
- CN202411324888.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-23
- Publication Date
- 2025-07-01
- Estimated Expiration
- 2044-09-23
AI Technical Summary
The existing memory leak detection methods lack a dynamic threshold adjustment mechanism, making it difficult to accurately locate the location and cause of memory leaks.
By monitoring CPU utilization and memory usage in real time, establishing a memory usage trend model, dynamically adjusting memory usage thresholds, starting memory leak detection, using hash tables to record memory allocation and release information, building a causal relationship model, and analyzing the causal relationship between memory leaks and other system behaviors.
It improves the foresight and accuracy of memory management, reduces system instability and resource waste caused by memory leaks, enhances the system's adaptability, and reduces the false alarm rate and missed alarm rate.
Smart Images

Figure CN119149285B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer science and technology, and particularly to a method for locating memory leaks. Background Art
[0002] In the fields of modern software development and system operation and maintenance, memory management has always been one of the key factors to ensure system stability and performance. With the development of computing technology, software systems have become increasingly complex, and the problem of memory leaks has become increasingly prominent. Memory leaks not only cause waste of system resources, but may also lead to performance bottlenecks and even system crashes. Therefore, the development of efficient and reliable memory leak detection and management technologies has become the focus of attention in the industry.
[0003] By introducing a dynamic threshold adjustment mechanism, the present invention can dynamically adjust the memory usage threshold according to the changes in CPU utilization and memory usage trends during the operation of the system. Compared with the existing technology that usually uses a fixed threshold, it can better adapt to the changes in the system operation environment. Summary of the Invention
[0004] In view of the above existing problems, the present invention is proposed.
[0005] Therefore, the present invention provides a method for locating memory leaks to solve the problems that the memory leak detection method lacks a dynamic threshold adjustment mechanism and it is difficult to accurately locate the position and cause of memory leaks.
[0006] To solve the above technical problems, the present invention provides the following technical solutions:
[0007] In a first aspect, an embodiment of the present invention provides a method for locating memory leaks, which includes:
[0008] Initialize a memory usage monitoring module, set an initial memory usage threshold, and define threshold adjustment parameters;
[0009] Real-time monitor CPU utilization and memory usage, record historical data, and establish a memory usage trend model;
[0010] When the CPU utilization exceeds the set CPU utilization threshold, lower the first preset threshold of the memory usage rate, adjust the second preset threshold according to the memory usage trend, and comprehensively adjust the memory usage threshold based on the adjusted first preset threshold and second preset threshold;
[0011] When the memory usage rate exceeds the first preset threshold, start memory leak detection, use a hash table to record memory allocation and release information, and search for unreleased memory;
[0012] Analyze the system logs and behavior records before and after memory leakage, construct a causal relationship model, and analyze the causal relationship between memory leakage and other system behaviors;
[0013] Adjust the memory usage threshold according to the results of the causal relationship, optimize the threshold setting, and generate a report including the location of memory leakage and its cause analysis.
[0014] As a preferred solution of the method for locating memory leakage described in the present invention, wherein: set an initial memory usage threshold;
[0015] The parameters in the threshold adjustment parameters are CPU utilization threshold and memory usage trend threshold;
[0016] Set a first preset threshold according to the CPU utilization threshold, and set a second preset threshold according to the memory usage trend threshold.
[0017] As a preferred solution of the method for locating memory leakage described in the present invention, wherein: continuously monitor the CPU utilization and memory usage using system calls;
[0018] Record the CPU utilization and memory usage per second into historical data;
[0019] Use historical data to establish a memory usage trend model to predict the future memory usage trend, and its expression is:
[0020] ;
[0021] Wherein, represents the memory usage trend predicted by the memory usage trend model, a represents the base value of the initial memory usage, e represents the base of the natural logarithm, b represents the growth rate of the memory usage over time, t represents the time interval starting from the initial moment, and c represents the base value of the memory usage.
[0022] As a preferred solution of the method for locating memory leakage described in the present invention, wherein: when the CPU utilization exceeds the initially set memory usage threshold, the system automatically reduces the first preset threshold of the memory usage rate, and its expression is:
[0023] ;
[0024] Wherein, represents the adjusted first preset threshold, represents the original first preset threshold, represents the threshold adjustment coefficient when the CPU utilization exceeds, represents the exceeded CPU utilization, represents the threshold adjustment parameter;
[0025] Adjust the second preset threshold according to the memory usage trend. When the system detects a change in the memory usage trend, the system will adjust the second preset threshold accordingly. Its expression is:
[0026] ;
[0027] Wherein, represents the adjusted second preset threshold, represents the original second preset threshold, represents the trend adjustment coefficient.
[0028] As a preferred solution of the method for locating memory leaks according to the present invention, wherein: Dynamically adjust the threshold according to the real-time monitored CPU utilization rate and memory usage trend model. Its expression is:
[0029] ;
[0030] Wherein, represents the adjusted memory usage rate threshold, represents the influence coefficient of CPU utilization rate on the adjustment of memory usage rate threshold, U represents the current CPU utilization rate, represents the influence coefficient of memory usage trend on the adjustment of memory usage rate threshold, represents the threshold adjustment parameter, represents the base value of threshold adjustment.
[0031] As a preferred solution of the method for locating memory leaks according to the present invention, wherein: When the real-time monitored memory usage rate exceeds the first preset threshold, the system will use a hash table to record the information of memory allocation and release. Each memory block will be assigned a unique identifier and record its allocation and release status. Its expression is:
[0032] ;
[0033] Wherein, H represents the hash table, ID represents the unique identifier of the memory block, A represents allocated, B represents released, C represents leaked, represents whether the memory block is allocated at time or not, represents whether the memory block is released at time or not, represents whether the memory block is marked as leaked at time ;
[0034] Search for the leaked memory block in the hash table. Its expression is:
[0035] ;
[0036] Among them, Q represents the number of memory blocks that have not been released.
[0037] As a preferred solution of the method for locating memory leaks described in the present invention, the following steps are included: Analyze the system logs and behavior records before and after memory leaks, construct a causal relationship model, and analyze the causal relationship between memory leaks and other system behaviors. The expression is:
[0038] ;
[0039] Among them, represents the correlation score between the event set E and the memory leak, represents the frequency of event e, represents the importance of event e, represents the correlation between event e and the memory leak, represents the time interval between the occurrence time of event e and the occurrence time of the memory leak, represents the time decay coefficient, represents all possible event spaces;
[0040] Analyze the causal relationship. The expression is:
[0041] ;
[0042] Among them, represents the probability of memory leak C when the event set E occurs, represents the probability of memory leak C when event e occurs, represents the probability of event e occurring.
[0043] As a preferred solution of the method for locating memory leaks described in the present invention, the following steps are included: Adjust the memory usage threshold according to the results of the causal relationship, optimize the threshold setting. The expression is:
[0044] ;
[0045] Among them, represents the adjusted memory usage threshold, represents the trend adjustment coefficient function;
[0046] Generate a report containing the location of the memory leak and its cause analysis. The expression is:
[0047] ;
[0048] Among them, represents the comprehensive score for location L, represents at location the event set related to The conditional probability of a memory leak C occurring in the event of occurrence, Indicates the location The set of related events The correlation score with the memory leak, Indicates the location The predicted memory usage at the corresponding current time point;
[0049] Based on the obtained comprehensive location score, write an analysis report on the memory leak location and its causes.
[0050] In a second aspect, an embodiment of the present invention provides a computer device, including a memory and a processor, where the memory stores a computer program, and: when the computer program is executed by the processor, any step of the method for locating memory leaks as described in the first aspect of the present invention is implemented.
[0051] In a third aspect, an embodiment of the present invention provides a computer-readable storage medium, on which a computer program is stored, and: when the computer program is executed by the processor, any step of the method for locating memory leaks as described in the first aspect of the present invention is implemented.
[0052] The beneficial effects of the present invention are as follows: By continuously monitoring the CPU utilization rate and memory usage, and recording these data to establish a memory usage trend model, the prediction of future memory usage is realized. When the CPU utilization rate exceeds the set threshold, the first preset threshold is automatically reduced, and the second preset threshold is adjusted according to the memory usage trend, realizing the function of dynamically adjusting the memory usage rate threshold, improving the predictability and accuracy of memory management, reducing system instability and resource waste caused by memory leaks, enhancing the adaptive ability of the system, being able to better meet the memory management requirements in different scenarios, and reducing the false alarm rate and missed alarm rate. BRIEF DESCRIPTION OF THE DRAWINGS
[0053] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the following will briefly introduce the drawings required for the description of the embodiments. Obviously, the following drawings are only some embodiments of the present invention. For those of ordinary skill in the art, other drawings can be obtained based on these drawings without creative efforts.
[0054] Figure 1 It is a flowchart of the method for locating memory leaks in Embodiment 1.
[0055] Figure 2 It is a determination diagram of whether the memory exceeds the threshold in Embodiment 1. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0056] To make the above objects, features, and advantages of the present invention more apparent and understandable, the following provides a detailed description of the specific embodiments of the present invention in conjunction with the accompanying drawings of the specification.
[0057] In the following description, numerous specific details are set forth in order to provide a thorough understanding of the present invention. However, the present invention may be practiced in other ways different from those described herein. Those skilled in the art can make similar generalizations without departing from the spirit of the present invention. Therefore, the present invention is not limited by the specific embodiments disclosed below.
[0058] Secondly, the so-called "one embodiment" or "embodiment" herein refers to a specific feature, structure, or characteristic that may be included in at least one implementation manner of the present invention. The appearances of "in one embodiment" in different places in this specification do not all refer to the same embodiment, nor are they separate or alternative embodiments that exclude each other with other embodiments.
[0059] Example 1, referring to Figure 1 and Figure 2 , is the first embodiment of the present invention. This embodiment provides a method for locating memory leaks, including the following steps:
[0060] S1. Set an initial memory usage rate threshold. The memory usage rate refers to the ratio of available memory to total memory in the system, which measures the ratio of the physical memory already used in the current system to the total physical memory. If the memory usage rate is very high, it may indicate that the memory resources are approaching exhaustion.
[0061] S1.1. The parameters in the threshold adjustment parameter are the CPU utilization threshold and the memory usage trend threshold.
[0062] Further explanation: The CPU utilization threshold is used to monitor the load situation of the CPU. When the CPU utilization exceeds this threshold, the system will take corresponding measures to determine when to lower the first preset threshold to reduce the burden on the CPU; the memory usage trend threshold is used to monitor the change in the memory usage trend and determine when to adjust the second preset threshold to better adapt to the change in memory usage. A suitable memory usage trend threshold is set according to historical data and system characteristics.
[0063] S1.2. Set the first preset threshold according to the CPU utilization threshold and set the second preset threshold according to the memory usage trend threshold.
[0064] Further explanation: Set the first preset threshold according to the set CPU utilization threshold, and set the second preset threshold according to the memory usage trend threshold; the first preset threshold is used to monitor the memory utilization rate. When the memory utilization rate exceeds this threshold, the system will start the memory leak detection program; the second preset threshold is used to monitor the change in the memory usage trend. When the memory usage trend changes, the system will adjust this threshold; the first preset threshold can timely detect the abnormal situation of the memory utilization rate, start the memory leak detection, and reduce the impact of memory leak on the system performance. The second preset threshold can more accurately adjust the threshold by monitoring the change in the memory usage trend, improve the rationality of the threshold setting, and thus better adapt to the current operating state of the system.
[0065] S2. Continuously monitor the CPU utilization rate and memory usage using system calls to ensure that data on these key metrics can be obtained in real time;
[0066] Further explanation: Monitoring the CPU utilization rate and memory usage is crucial for judging the current load status of the system, which helps to timely detect potential performance bottlenecks. Through real-time monitoring, abnormal changes in the CPU utilization rate and memory usage can be timely detected, providing a basis for subsequent threshold adjustment.
[0067] Record the monitored CPU utilization rate per second and memory usage in historical data for subsequent analysis; these historical data are used to establish a memory usage trend model and predict the future memory usage trend. The accumulation of historical data helps to more accurately predict the future memory usage trend, thereby improving the accuracy and timeliness of memory leak detection.
[0068] S2.1. Use historical data to establish a memory usage trend model to predict the future memory usage trend, and its expression is:
[0069] ;
[0070] Among them, represents the memory usage trend predicted by the memory usage trend model. a represents the base value of the initial memory usage, usually representing the memory usage at the initial moment predicted by the model. e represents the base of the natural logarithm, which is a constant used to describe the growth trend of memory usage over time. b represents the speed at which the memory usage increases over time, which is a positive number. The larger the value of b, the faster the memory usage increases over time. t represents the time interval starting from the initial moment. c represents the base value of the memory usage. c can be understood as the minimum base level of memory usage over time. Even if there is no obvious growth, the system will maintain a certain amount of memory usage.
[0071] Adjusting the threshold setting according to the prediction results helps to respond promptly to changes in memory usage and prevent the system performance degradation caused by memory leaks.
[0072] S3. When the CPU utilization rate exceeds the initially set memory usage threshold, the system will automatically reduce the first preset threshold of the memory usage rate, and its expression is:
[0073] ;
[0074] where, represents the adjusted first preset threshold, represents the original first preset threshold, represents the threshold adjustment coefficient when the CPU utilization rate exceeds. This is an adjustment coefficient used to quantify the impact of the exceeded part on the threshold adjustment. When the CPU utilization rate exceeds the set threshold, the larger the δ value, the more obvious the degree of threshold reduction. represents the exceeded CPU utilization rate, which is the difference between the actual value of the CPU utilization rate and the set threshold, reflecting the degree to which the CPU load exceeds the normal range. represents the threshold adjustment parameter, used to control the amplitude of the threshold adjustment. The larger the
[0075] value, the greater the degree of threshold adjustment;
[0076] Furthermore, by reducing the first preset threshold, the memory usage can be reduced, thereby reducing the burden on the CPU. Reducing the memory usage when the CPU load is high helps to maintain the stable operation of the system.
[0076] S3.1. Adjust the second preset threshold according to the memory usage trend. When the system detects a change in the memory usage trend, the system will accordingly adjust the second preset threshold, and its expression is:
[0077] ;
[0078] where, represents the adjusted second preset threshold, represents the original second preset threshold, represents the trend adjustment coefficient, used to quantify the impact of the change in the memory usage trend on the threshold adjustment. When the memory usage trend changes, the larger the
[0079] value, the more obvious the degree of threshold adjustment.
[0080] S3.2. Dynamically adjust the threshold according to the CPU utilization rate and memory usage trend model monitored in real time. Its expression is:
[0081] ;
[0082] where, represents the adjusted memory usage rate threshold, represents the influence coefficient of CPU utilization rate on the adjustment of memory usage rate threshold, which is used to measure the influence degree of CPU utilization rate on the adjustment of memory usage rate threshold. U represents the current CPU utilization rate, reflecting the load degree of the CPU, represents the influence coefficient of memory usage trend on the adjustment of memory usage rate threshold, which is used to measure the influence degree of memory usage trend on the adjustment of memory usage rate threshold, represents the threshold adjustment parameter, which is used to control the amplitude of threshold adjustment, The larger the value of , the greater the degree of threshold adjustment,
[0083] Further explanation: By comprehensively considering the influence of CPU utilization rate and memory usage trend to adjust the threshold, the intelligence and flexibility of threshold setting can be improved. And the intelligent adjustment of the threshold helps to better manage memory usage and improve the overall performance and stability of the system.
[0084] Generally speaking, both the first preset threshold and the second preset threshold will be dynamically adjusted according to the actual situation during the operation of the system. When the CPU utilization rate exceeds the set CPU utilization rate threshold, the system will automatically lower the first preset threshold to relieve the burden on the CPU. This is because high CPU utilization rate may indicate that the system is facing greater pressure, and reducing the memory usage rate can help alleviate this situation. When the system detects a change in the memory usage trend, it will adjust the second preset threshold according to the predicted memory usage amount by the memory usage trend model. This adjustment is to better adapt to the change in memory usage and ensure that the threshold setting can reflect the current memory usage situation. The first preset threshold and the second preset threshold are functionally independent, but they jointly constitute a complete memory management strategy. The first preset threshold is used to detect memory leaks, while the second preset threshold helps to optimize the threshold setting. The two complement each other and jointly improve the stability and performance of the system.
[0085] When the first preset threshold is triggered but the second preset threshold is not triggered, the main focus is on starting the memory leak detection program and analyzing the cause of the memory leak.
[0086] When the second preset threshold is triggered but the first preset threshold is not, the main focus is on adjusting the threshold settings according to the memory usage trend, optimizing the threshold configuration, and continuing to monitor the memory usage for future needs.
[0087] S4. When the real-time monitored memory usage rate exceeds the first preset threshold, the system will start memory leak detection and use a hash table to record information on memory allocation and release. The hash table will track the allocation and release status of memory blocks to find un-released memory. Each memory block will be assigned a unique identifier and its allocation and release status will be recorded. Its expression is:
[0088] ;
[0089] Where, H represents the hash table, ID represents the unique identifier of the memory block, A represents allocated, B represents released, C represents leaked, represents whether the memory block is allocated at time , represents whether the memory block is released at time ; represents whether the memory block is marked as leaked at time ;
[0090] Further explanation, using a hash table can quickly find and locate memory leaks, improving the efficiency and accuracy of memory leak detection. By assigning a unique identifier to each memory block and recording its allocation and release status, the process of locating memory leaks can be simplified.
[0091] S4.1. Find the leaked memory blocks in the hash table. Its expression is:
[0092] ;
[0093] Where, Q represents the number of un-released memory blocks.
[0094] Further explanation, accurately finding the leaked memory blocks based on the hash table helps reduce unnecessary resource consumption and improve the overall performance of the system.
[0095] S5. Analyze the system logs and behavior records before and after the memory leak, build a causal relationship model, and analyze the causal relationship between the memory leak and other system behaviors. Its expression is:
[0096] ;
[0097] Where, represents the correlation score between the event set E and the memory leak, represents the frequency of event e, represents the importance of event e. Indicates the correlation between event e and memory leak, Indicates the time interval between the occurrence time of event e and the occurrence time of memory leak, Indicates the time decay coefficient, Indicates all possible event spaces;
[0098] The value range of R(E) is [0, ∞), which is adjusted to between [0, 1] through normalization. The closer the value is to 1, the stronger the correlation between the event set E and memory leak.
[0099] Analyze the causal relationship, and its expression is:
[0100] ;
[0101] Among them, Indicates the probability of memory leak C in the case where the event set E occurs, Indicates the probability of memory leak C in the case where event e occurs, Indicates the probability of event e occurring.
[0102] By calculating Quantify the causal relationship between memory leak and other system behaviors.
[0103] S6. Adjust the memory usage threshold according to the result of the causal relationship and optimize the threshold setting. Its expression is:
[0104] ;
[0105] Among them, Indicates the adjusted memory usage threshold, Indicates the trend adjustment coefficient function, which is used to adjust the threshold according to the memory usage trend;
[0106] Generate a report containing the memory leak location and its cause analysis. Its expression is:
[0107] ;
[0108] Among them, Indicates the comprehensive score for location L, Indicates at location The relevant event set Occurs, the conditional probability of memory leak C occurring, Indicates location The relevant event set The correlation score with memory leak, Indicates location The predicted memory usage at the corresponding current time point;
[0109] The threshold of is theoretically (−∞, +∞), but can be adjusted to between [0, 1] through appropriate normalization. The closer the value is to 1, the greater the likelihood of a memory leak occurring at location L.
[0110] Based on the obtained comprehensive location scores, prepare a report on the memory leak locations and their cause analysis. The report includes: the location where the memory leak is most likely to occur; for each location, give the specific set of events and their correlation with the memory leak; for each location, give the probability of a memory leak occurring when the set of events occurs; the specific values of the trend adjustment coefficient function and its impact on threshold adjustment; the adjusted memory usage threshold; the predicted future memory usage and recommended measures.
[0111] Example, according to the calculated comprehensive scores , sort all the monitored locations L. Select several locations with the highest comprehensive scores as the locations where memory leaks are most likely to occur. For example, we have three locations L1, L2, and L3, and their comprehensive scores are = 0.85, = 0.72, = 0.65. Then L1 is the location where a memory leak is most likely to occur.
[0112] For each high-risk location Li, list the associated set of events Ei and calculate the correlation score of each event with the memory leak . For example, for location L1, the set of events E1 = {e1, e2, e3}, where e1 represents a file read operation, e2 represents a network connection establishment, and e3 represents a database query. The correlation scores of these events are = 0.9, = 0.7, = 0.8.
[0113] Using the obtained set of events Ei and the calculated conditional probability , obtain the probability of a memory leak occurring when the set of events occurs. For example, for the set of events E1 of location L1, the conditional probabilities of a memory leak occurring are = 0.8, = 0.6, = 0.7.
[0114] For the specific values of the trend adjustment coefficient function and how it affects threshold adjustment, according to the designed trend adjustment coefficient function , give specific values and explain their impact on threshold adjustment. For example, set the trend sensitivity coefficient α = 0.5, and the predicted memory usage at the current time point = 80%, the original second preset threshold = 70%, then the trend adjustment coefficient function is = 0.4. This means that since the current memory usage trend is higher than the preset threshold, the threshold will be adjusted upward accordingly to adapt to the higher memory usage rate.
[0115] Based on the current memory usage trend and the frequency of event occurrence, predict the future memory usage situation, and accordingly propose preventive measures or suggestions. For example, it is predicted that the memory usage will continue to increase in the future for a period of time. It is recommended to take the following measures: increase the server resource allocation to cope with the higher load. Optimize the code logic to reduce unnecessary memory occupation. Implement a more strict memory management strategy, such as regularly cleaning up objects that are no longer in use. Monitor the memory usage at key positions to ensure that potential memory leak problems are detected and handled in a timely manner.
[0116] This embodiment also provides a computer device applicable to the situation of the method for locating memory leaks, including: a memory and a processor; the memory is used to store computer-executable instructions, and the processor is used to execute the computer-executable instructions to implement the method for locating memory leaks proposed in the above embodiment.
[0117] The computer device can be a terminal. The computer device includes a processor, a memory, a communication interface, a display screen, and an input device connected through a system bus. Among them, the processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and a computer program. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The communication interface of the computer device is used to communicate with an external terminal in a wired or wireless manner. The wireless manner can be implemented through WIFI, a carrier network, NFC (Near Field Communication), or other technologies. The display screen of the computer device can be a liquid crystal display screen or an electronic ink display screen. The input device of the computer device can be a touch layer covering the display screen, or a button, a trackball, or a touchpad provided on the housing of the computer device, or an external keyboard, touchpad, or mouse, etc.
[0118] This embodiment also provides a storage medium, on which a computer program is stored. When the program is executed by a processor, it implements the method for locating memory leaks proposed in the above embodiment; the storage medium can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as static random access memory (Static Random Access Memory, abbreviated as SRAM), electrically erasable programmable read-only memory (Electrically Erasable Programmable Read-Only Memory, abbreviated as EEPROM), erasable programmable read-only memory (Erasable Programmable Read Only Memory, abbreviated as EPROM), programmable read-only memory (Programmable Red-Only Memory, abbreviated as PROM), read-only memory (Read-Only Memory, abbreviated as ROM), magnetic memory, flash memory, magnetic disk or optical disc.
[0119] In summary, the present invention realizes the prediction of future memory usage by continuously monitoring the CPU utilization rate and memory usage and recording these data to establish a memory usage trend model. When the CPU utilization rate exceeds the set threshold, the first preset threshold is automatically reduced, and the second preset threshold is adjusted according to the memory usage trend, realizing the function of dynamically adjusting the memory usage rate threshold, improving the predictability and accuracy of memory management, reducing system instability and resource waste caused by memory leaks, enhancing the adaptive ability of the system, being able to better cope with memory management requirements in different scenarios, and reducing the false alarm rate and missed alarm rate.
[0120] Embodiment 2, referring to Table 1, is the second embodiment of the present invention. To further verify the technical solution of the present invention, experimental simulation data of the method for locating memory leaks is given.
[0121] To verify the effectiveness of the method for locating memory leaks of the present invention, the following series of tests were carried out. The test environment is a computer device configured with an Intel i7 processor and 16GB of RAM. The operating system uses the Windows 10 Pro version. The test software is an application program that simulates multi-threaded operations. This application program will generate a large number of memory allocation and release operations during operation and cause memory leaks under certain specific conditions.
[0122] Step 1: Initialize the memory usage monitoring module
[0123] Initialize the memory usage monitoring module, set the initial memory usage rate threshold to 70%, and define the threshold adjustment parameters: the CPU utilization rate threshold is 90%, and the memory usage trend threshold is 2%.
[0124] Set the threshold adjustment coefficient δ to 0.1, the trend adjustment coefficient ω to 0.5, the CPU utilization influence coefficient k_U to 0.6, the memory usage trend influence coefficient k_T to 0.4, the threshold adjustment parameter k_1 to 1.2, the threshold adjustment parameter k_2 to 1.1, and the base value k_3 of threshold adjustment to 0.05.
[0125] Step 2: Continuously monitor the CPU utilization and memory usage in real time
[0126] Use system calls to continuously monitor the CPU utilization and memory usage, and record the CPU utilization and memory usage amount into the historical data every second.
[0127] Based on the historical data, use the exponential smoothing method to establish a memory usage trend model to predict the future memory usage trend. The base value a of the initial memory usage amount is 4GB, the growth rate b of the memory usage amount over time is 0.01 / h, and the base value c of the memory usage amount is 3GB.
[0128] Step 3: Threshold adjustment
[0129] When the CPU utilization exceeds 90%, the first preset threshold T_first for automatically reducing the memory usage rate is 65%, and the adjusted first preset threshold T_adj is 60%.
[0130] When the system detects a change in the memory usage trend, adjust the second preset threshold T_sec to 68%, and the adjusted second preset threshold T_adj2 to 65%.
[0131] Dynamically adjust the threshold according to the real-time monitored CPU utilization and memory usage trend model. The adjusted memory usage rate threshold T_new(t) is 62%.
[0132] Step 4: Start memory leak detection
[0133] When the real-time monitored memory usage rate exceeds the first preset threshold of 65%, start memory leak detection, and use a hash table to record memory allocation and release information.
[0134] Assign a unique identifier to each memory block and record its allocation and release status. By looking up the hash table, count the number Q of memory blocks that have not been released.
[0135] Step 5: Build a causal relationship model
[0136] Analyze the system logs and behavior records before and after memory leak, build a causal relationship model, and analyze the causal relationship between memory leak and other system behaviors.
[0137] Adjust the memory usage threshold according to the results of the causal relationship, optimize the threshold setting, and generate a report containing the location of the memory leak and its cause analysis.
[0138] Step 6: Adjust the threshold and generate a report
[0139] Adjust the memory usage threshold according to the results of the causal relationship, and optimize the threshold setting. The adjusted memory usage threshold S_new is 60%.
[0140] Generate a report containing the location of the memory leak and its cause analysis. The report details information such as the most likely location of the memory leak, the specific set of events, and their correlation with the memory leak.
[0141] Specifically, as shown in Table 1:
[0142] Table 1 Comparison of the effect differences in handling memory leaks between the present invention and the prior art
[0143]
[0144] Data analysis
[0145] Description of the test object:
[0146] Benchmark: Represents the performance indicators of the prior art.
[0147] Invention 1: Represents the performance indicators of the present invention in one test.
[0148] Invention 2: Represents the performance indicators of the present invention in another test.
[0149] Analysis process:
[0150] CPU utilization rate: The CPU utilization rate of the benchmark is 85%, while those of Invention 1 and Invention 2 reach 90% and 92% respectively. This indicates that the present invention can still operate effectively under high load conditions.
[0151] Memory usage rate: The memory usage rate of the benchmark is 64%, while those of Invention 1 and Invention 2 are 68% and 72% respectively. This indicates that the present invention can operate at a higher memory usage rate while maintaining a lower memory usage threshold.
[0152] Memory usage trend: The memory usage trend of the benchmark is 2.5%, while those of Invention 1 and Invention 2 are 2.8% and 3.2% respectively. This indicates that the present invention can still effectively adjust the threshold in the face of a higher memory growth trend.
[0153] Adjusted Memory Usage Threshold: The adjusted threshold for the baseline is 65%, while the adjusted thresholds for Invention 1 and Invention 2 are 62% and 61% respectively. This indicates that the present invention can more effectively adjust the threshold to avoid memory leaks in the face of higher CPU utilization and memory usage trends.
[0154] Conclusion:
[0155] Through means such as real-time monitoring of memory usage, dynamic adjustment of thresholds, recording memory allocation and release information in a hash table, and constructing a causal relationship model, the present invention effectively reduces the memory usage threshold in the case of high CPU utilization and rapidly changing memory usage trends, thereby reducing the possibility of memory leaks. Compared with the prior art, the present invention can more accurately detect memory leaks and take timely measures to avoid problems, thus improving the stability and performance of the system.
[0156] It should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention and not to limit them. Although the present invention has been described in detail with reference to the preferred embodiments, those of ordinary skill in the art should understand that the technical solutions of the present invention can be modified or equivalently replaced without departing from the spirit and scope of the technical solutions of the present invention, and they should all be covered by the scope of the claims of the present invention.
Claims
1. A method for locating a memory leak, characterized in that: include: Initialize the memory usage monitoring module, set the initial memory usage threshold, and define the threshold adjustment parameters; Monitor CPU utilization and memory usage in real time, record historical data and build a memory usage trend model; When the CPU utilization exceeds the set CPU utilization threshold, lower the first preset threshold of the memory utilization, adjust the second preset threshold according to the memory usage trend, and comprehensively adjust the memory utilization threshold based on the adjusted first preset threshold and the second preset threshold; When the memory usage rate exceeds a first preset threshold, memory leak detection is started, a hash table is used to record memory allocation and release information, and unreleased memory is searched; Analyze the system logs and behavior records before and after the memory leak, build a causal relationship model, and analyze the causal relationship between the memory leak and other system behaviors; Adjust memory usage thresholds based on causal relationship results, optimize threshold settings, and generate reports containing analysis of memory leak locations and their causes.
2. The method for locating a memory leak according to claim 1, characterized in that: Set the initial memory usage threshold; The parameters in the threshold adjustment parameters are CPU utilization threshold and memory usage trend threshold; The first preset threshold is set according to the CPU utilization threshold, and the second preset threshold is set according to the memory usage trend threshold.
3. The method for locating a memory leak as claimed in claim 2, characterized in that: Use system calls to continuously monitor CPU utilization and memory usage; Record the CPU utilization and memory usage per second into historical data; Use historical data to build a memory usage trend model to predict future memory usage trends. The expression is: ; in, Indicates the memory usage trend predicted by the memory usage trend model. a represents the basic value of the initial memory usage, e represents the base of the natural logarithm, b represents the rate at which the memory usage increases over time, t represents the time interval starting from the initial moment, and c represents the basic value of the memory usage.
4. The method for locating a memory leak as claimed in claim 3, characterized in that: When the CPU utilization exceeds the initially set memory utilization threshold, the system automatically lowers the first preset threshold of memory utilization, which is expressed as: ; in, represents the adjusted first preset threshold, represents the original first pre-threshold value, Indicates the threshold adjustment coefficient when CPU utilization exceeds the limit. Indicates the CPU utilization that is exceeded. represents the threshold adjustment parameter; The second preset threshold is adjusted according to the memory usage trend. When the system detects that the memory usage trend changes, the system will adjust the second preset threshold accordingly. The expression is: ; in, represents the adjusted second preset threshold, represents the original second pre-threshold value, represents the trend adjustment coefficient.
5. The method for locating a memory leak as claimed in claim 4, characterized in that: The threshold is dynamically adjusted based on the real-time monitored CPU utilization and memory usage trend model. The expression is: ; in, Indicates the adjusted memory usage threshold. Indicates the influence coefficient of CPU utilization on the adjustment of memory utilization threshold. U indicates the current CPU utilization. Indicates the influence coefficient of memory usage trend on memory usage threshold adjustment. represents the threshold adjustment parameter, Indicates the base value for threshold adjustment.
6. The method for locating a memory leak as claimed in claim 5, characterized in that: When the real-time monitored memory usage exceeds the first preset threshold, the system will use a hash table to record the memory allocation and release information. Each memory block will be assigned a unique identifier and record its allocation and release status. The expression is: ; Among them, H represents the hash table, ID represents the unique identifier of the memory block, A represents allocated, B represents released, and C represents leaked. Indicates the memory block at time Is it assigned? Indicates memory block time whether it was released, Indicates the memory block at time Whether it is marked as leaked; Find the leaked memory block in the hash table, the expression is: ; Among them, Q represents the number of memory blocks that have not been released.
7. The method for locating a memory leak as claimed in claim 6, characterized in that: Analyze the system logs and behavior records before and after the memory leak, build a causal relationship model, and analyze the causal relationship between the memory leak and other system behaviors. The expression is: ; in, represents the correlation score between event set E and memory leak, represents the frequency of event e, Indicates the importance of event e, Indicates the correlation between event e and memory leak, Indicates the time interval between the occurrence of event e and the occurrence of memory leak. represents the time attenuation coefficient, represents the space of all possible events; Analyzing the causal relationship, the expression is: ; in, represents the probability of memory leak C when the event set E occurs, represents the probability of memory leak C when event e occurs, represents the probability of event e occurring.
8. The method for locating a memory leak as claimed in claim 7, characterized in that: According to the result of causality, the memory usage threshold is adjusted to optimize the threshold setting. The expression is: ; in, Indicates the adjusted memory usage threshold. represents the trend adjustment coefficient function; Generates a report containing the memory leak location and its cause analysis, the expression is: ; in, represents the comprehensive score of position L, Indicates at location A collection of related events The conditional probability of memory leak C occurring when Indicates location A collection of related events Correlation score with memory leaks, Indicates location The corresponding predicted memory usage at the current time point; Based on the comprehensive location score obtained, write an analysis report on the memory leak location and its cause.
9. A computer device comprising a memory and a processor, wherein the memory stores a computer program, wherein: When the processor executes the computer program, the steps of the method for locating memory leaks described in any one of claims 1 to 7 are implemented.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method for locating a memory leak according to any one of claims 1 to 7 are implemented.
Citation Information
Patent Citations
Positioning method and device for memory leakage
CN107844394A
Intelligent elastic scaling method and system based on CPU and memory occupancy rate
CN114428666A