A memory leakage detection method, device, equipment and storage medium
By analyzing memory data in real time and calculating memory growth rate and leakage amount, the accuracy and real-time performance issues of memory leak detection in existing technologies are resolved. This achieves efficient and accurate memory leak detection, reduces manual intervention, and improves system stability and user experience.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- 深圳市云希谷科技有限公司
- Filing Date
- 2024-11-25
- Publication Date
- 2026-07-21
Smart Images

Figure CN119597629B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer technology, and in particular to a method, apparatus, device, and storage medium for detecting memory leaks. Background Technology
[0002] Currently, with the rapid development of computer technology, memory leaks have increasingly become a significant factor affecting the stability and performance of computer systems. A memory leak occurs when a program, after requesting memory, fails to release the allocated memory space, leading to the gradual depletion of memory resources and potentially causing program crashes or system freezes.
[0003] In existing technologies, memory leak detection methods often rely on manual analysis or simple threshold judgments.
[0004] Existing technologies cannot accurately and in real time detect memory leaks, resulting in memory leaks not being handled promptly, quickly, or accurately. Summary of the Invention
[0005] This invention provides a method, apparatus, device, and storage medium for detecting memory leaks, enabling accurate, rapid, and real-time detection of memory leaks, timely discovery and handling of memory leak problems, and improving detection efficiency and accuracy.
[0006] Firstly, in order to solve the above-mentioned technical problems, the present invention provides a method for detecting memory leaks, comprising:
[0007] Get the first memory data;
[0008] Based on the first memory data, a first growth rate is determined to obtain memory data for the first time period, which is then used as the second memory data.
[0009] Based on the second memory data, calculations are performed to obtain the first average growth rate and the second average growth rate;
[0010] Based on the first average growth rate and the second average growth rate, a second growth rate determination is made to obtain the memory data for the second time period, which is then used as the third memory data.
[0011] Based on the third memory data, a third average growth rate is calculated.
[0012] The first growth amount is obtained by calculating based on the third memory data and the third average growth rate;
[0013] Based on the first growth amount and the third average growth rate, a memory leak is determined, and the memory leak result is obtained.
[0014] In one optional implementation, the step of determining a first growth rate based on the first memory data to obtain memory data for a first time period, which is then used as the second memory data, includes:
[0015] Based on the first memory data, determine whether there is a time period in which the memory growth rate is significantly greater than the normal memory growth rate, and use it as the first time period. If so, use the memory data of the first time period as the second memory data. If not, reacquire the first memory data.
[0016] The first memory data includes the memory growth rate, normal memory growth rate, and memory growth amount of each memory data in each time period.
[0017] In one optional implementation, the step of calculating the first average growth rate and the second average growth rate based on the second memory data includes:
[0018] Calculate the average growth rate of the second memory data and determine it as the first average growth rate; calculate the average growth rate of the closest stable period before the start of the first time period and take it as the second average growth rate.
[0019] The first average growth rate and the second average growth rate are calculated using the following formulas:
[0020]
[0021] Where G1 is the first average growth rate, and k1 is the preset memory growth coefficient. G1 represents the variance of memory growth during the first time period, G2 represents the second average growth rate, and k2 represents a preset memory growth coefficient. The variance of memory growth is the closest stationary period preceding the first time period.
[0022] in,
[0023] Among them, X i This represents the memory growth during the first time period. The average value of the memory growth is given, and n is the number of observations within the first time period.
[0024] in,
[0025] Among them, X j This refers to the memory growth rate of the closest stable time period preceding the first time period. is the average value of the memory growth, and m is the number of observations in the closest stable period before the first time period.
[0026] In one optional implementation, the step of calculating the third average growth rate based on the third memory data includes:
[0027] Calculate the average growth rate of the closest stationary period after the second time period, and use it as the third average growth rate.
[0028] The third average growth rate is calculated using the following formula:
[0029]
[0030] Where B is the third average growth rate, ΔM i denoted as , where is the memory growth at the i-th time point within the closest stable time period following the second time period; T is the total duration of the closest stable time period following the second time period; and z is the number of observations within the closest stable time period following the second time period.
[0031] In one optional implementation, the step of calculating the growth amount of the third memory data based on the third memory data and the third average growth rate includes:
[0032] Based on the third memory data, calculate the memory growth amount after the memory growth rate decreases in the second time period, and use it as the first growth amount;
[0033] The first increase is calculated using the following formula:
[0034] A2 = A1 – B*(t2 – t3)
[0035] Where A2 is the first growth amount, A1 is the memory growth amount in the second time period, B is the third average growth rate, t2 is the second time period, and t3 is the time period after the second time period where the memory growth rate is significantly higher than the normal growth rate again.
[0036] In one optional implementation, the step of determining a memory leak based on the first growth amount and the second average growth rate data to obtain a memory leak result includes:
[0037] Determine whether there is a memory leak based on the first growth amount and the second average growth rate data: if the first growth amount is negative and the first average growth rate is lower than the third average growth rate, then there is a memory leak; if the first growth amount is not negative, then the growth rate of the first memory data fluctuates normally and there is no memory leak.
[0038] Secondly, the present invention provides a memory leak detection device, comprising:
[0039] The data acquisition module is used to acquire the first memory data;
[0040] The first growth rate judgment module is used to make a first growth rate judgment based on the first memory data to obtain the memory data of the first time period, which is used as the second memory data.
[0041] The first data calculation module is used to perform calculations based on the second memory data to obtain the first average growth rate and the second average growth rate.
[0042] The second growth rate judgment module is used to make a second growth rate judgment based on the first average growth rate and the second average growth rate to obtain the memory data of the second time period as the third memory data.
[0043] The second data calculation module is used to perform calculations based on the third memory data to obtain the third average growth rate.
[0044] The third data calculation module is used to calculate the first growth amount based on the third memory data and the third average growth rate.
[0045] The memory leak detection module is used to detect memory leaks based on the first growth data and the third average growth rate, and obtain the memory leak result.
[0046] Thirdly, the present invention also provides an electronic device, including a processor, a memory, and a computer program stored in the memory and configured to be executed by the processor, wherein the processor, when executing the computer program, implements a memory leak detection method as described in any one of the above.
[0047] Fourthly, the present invention also provides a computer-readable storage medium comprising a stored computer program, wherein, when the computer program is executed, it controls the device where the computer-readable storage medium is located to perform a memory leak detection method as described in any one of the above.
[0048] This invention proposes a method for detecting memory leaks. It intelligently identifies memory growth trends by analyzing memory data in real time and accurately calculates the amount of memory growth and leakage. The method includes real-time reading and analysis of memory data, calculating the average growth rate over a specific time period, and comparing it with the average growth rate over a stable period. Through a series of calculation steps, including recording memory growth at key time points, calculating the average memory growth during stable periods, and calculating the memory growth value after a decrease in the memory growth rate, this invention can comprehensively determine the memory leak situation and output the memory leak rate. Furthermore, this invention also provides corresponding detection devices and electronic equipment, as well as storage media, to automate and intelligently implement this detection method.
[0049] Existing technologies for memory leak detection have several shortcomings, such as the inability to accurately determine the occurrence time, leakage amount, and cumulative amount of memory leaks, the inability to automate detection, and the need for significant manual intervention. This invention addresses these shortcomings by monitoring changes in memory data in real time to obtain first memory data. Based on this first memory data, a first growth rate is determined to obtain second memory data. Based on the second memory data, calculations are performed to obtain a first average growth rate and a second average growth rate. Based on the first and second average growth rates, a second growth rate is determined to obtain third memory data. Based on the third memory data, a third average growth rate is calculated. Based on the third memory data and the third average growth rate, a first growth amount is calculated. Based on the first growth amount and the third average growth rate, a memory leak is determined, yielding the memory leak result. This method not only improves detection accuracy but also reduces manual intervention, achieving automation in memory leak detection. By calculating the growth rate and memory growth amount and combining this with a comprehensive judgment based on actual conditions, this invention can promptly detect memory leaks and provide quantitative leak data, thus providing strong technical support for system maintenance and optimization.
[0050] Compared to existing technologies, this invention offers the following advantages: First, it can monitor and analyze memory data in real time, promptly detecting memory leaks and improving the efficiency and accuracy of memory management. Second, by accurately calculating memory growth and leakage, this invention provides a quantitative method for memory leak detection, enabling maintenance personnel to more accurately understand the system's memory status. Furthermore, the automated and intelligent detection device of this invention reduces manual intervention, saving manpower and material costs and shortening testing time. Finally, when a memory leak is detected, this invention can issue a warning signal to the user, improving system stability and user experience. In summary, this invention, through its innovative detection method and device, provides an efficient, accurate, and automated solution for memory leak detection. Attached Figure Description
[0051] Figure 1 This is a schematic flowchart of a memory leak detection method provided in the first embodiment of the present invention;
[0052] Figure 2 This is a schematic diagram of a memory leak detection device provided in the second embodiment of the present invention. Detailed Implementation
[0053] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0054] Currently, with the rapid development of computer technology, memory leaks have increasingly become a significant factor affecting the stability and performance of computer systems. A memory leak occurs when a program, after requesting memory, fails to release the allocated memory space, leading to the gradual depletion of memory resources and potentially causing program crashes or system freezes.
[0055] In existing technologies, memory leak detection methods often rely on manual analysis or simple threshold judgments.
[0056] Existing technologies cannot accurately and in real time detect memory leaks, resulting in memory leaks not being handled promptly, quickly, or accurately.
[0057] To solve the above technical problems, refer to Figure 1 The first embodiment of the present invention provides a method for detecting memory leaks, comprising the following steps:
[0058] S11, Obtain the first memory data;
[0059] S12, based on the first memory data, perform a first growth rate judgment to obtain the memory data for the first time period, which is then used as the second memory data;
[0060] S13, Calculate based on the second memory data to obtain the first average growth rate and the second average growth rate;
[0061] S14, based on the first average growth rate and the second average growth rate, a second growth rate judgment is made to obtain the memory data of the second time period, which is used as the third memory data;
[0062] S15, Calculate the third average growth rate based on the third memory data;
[0063] S16, Calculate the first growth amount based on the third memory data and the third average growth rate;
[0064] S17. Based on the first growth amount and the third average growth rate, a memory leak judgment is made to obtain the memory leak result.
[0065] In step S11, the first memory data is obtained.
[0066] In this context, the first memory data refers to the memory growth rate, normal memory growth rate, and memory growth amount at various time periods. This first memory data is the foundation of memory leak detection methods, reflecting the system's memory usage at different points in time. The memory growth rate indicates the trend of memory usage changes, while the normal memory growth rate and memory growth amount provide reference benchmarks for judging memory leaks.
[0067] It's important to note that the memory growth rate, normal memory growth rate, and total memory increase for each time period are obtained by monitoring and recording the system's memory usage during operation. Specifically, this can be achieved by reading memory usage data provided by the operating system in real time, or by using dedicated performance monitoring tools to capture this information. This data includes detailed records of memory allocation, release, and occupation, as well as memory usage statistics over time. By analyzing this data, the memory growth rate and total memory increase can be calculated, thus yielding the normal memory growth rate.
[0068] The initial memory data acquired in this step is primarily used for subsequent memory leak detection. This initial data serves as input to facilitate more in-depth analysis and comparison in later steps. For example, comparing the actual memory growth rate with the normal memory growth rate can identify potential signs of memory leaks. In this step, the use of the normal memory growth rate is fundamental, providing necessary data support for subsequent steps such as growth rate determination and average growth rate calculation.
[0069] In step S12, a first growth rate is determined based on the first memory data to obtain the memory data for the first time period, which is then used as the second memory data.
[0070] The core of this step lies in identifying and judging abnormal growth rates in memory data. Identifying abnormal growth rates is achieved by analyzing memory usage during program execution, particularly whether the memory growth rate is significantly higher than normal.
[0071] It's important to note that determining the first growth rate requires analyzing initial memory data, including memory growth rates over different time periods, normal memory growth rates, and the total amount of memory increase. By monitoring and recording this data, changes in memory usage over time can be captured. This process typically involves real-time monitoring tools and built-in operating system functions that can track and record memory usage, providing raw data for subsequent analysis.
[0072] Specifically, the first growth rate assessment involves determining if there exists a timeframe where the memory growth rate significantly exceeds the normal memory growth rate. If such a timeframe exists, the memory data during that period will be marked as abnormal and used as the second set of memory data for further analysis. This assessment process is based on a preset normal memory growth rate threshold; by comparing the actual observed growth rate with this threshold, it determines whether abnormal growth has occurred. This method helps to promptly identify potential memory leaks, allowing for appropriate remediation or optimization measures.
[0073] In step S13, calculations are performed based on the second memory data to obtain the first average growth rate and the second average growth rate.
[0074] This step involves calculating two average growth rates from the second set of memory data. These two growth rates represent the average rate of memory growth over different time periods. The first average growth rate refers to the average growth rate within the first time period where the memory growth anomaly was identified, while the second average growth rate refers to the average growth rate over a relatively stable period preceding the anomaly. Obtaining these two growth rates is crucial for subsequent memory leak detection, as they help distinguish between normal memory usage patterns and potential memory leaks.
[0075] Specifically, the calculation method is implemented through statistical analysis. First, for the first time period—the period of abnormal memory growth—the growth amount of all memory data within this period is calculated, and the average of these growth amounts is obtained. Combined with the variance of the memory data growth, the first average growth rate is derived. Similarly, for the closest stable time period preceding the first time period, its average growth rate is calculated using the same method, which is the second average growth rate. The key to obtaining the first and second average growth rates lies in correctly identifying the stable time periods and ensuring that the selected time periods truly reflect the memory growth during normal system operation, thus providing a benchmark for subsequent comparisons.
[0076] The first average growth rate and the second average growth rate are calculated using the following formulas:
[0077]
[0078] Where G1 is the first average growth rate, and k1 is the preset memory growth coefficient. G1 represents the variance of memory growth during the first time period, G2 represents the second average growth rate, and k2 represents a preset memory growth coefficient. The variance of memory growth is the closest stationary period preceding the first time period.
[0079] in,
[0080] Among them, X i This represents the memory growth during the first time period. The average value of the memory growth is given, and n is the number of observations within the first time period.
[0081] in,
[0082] Among them, X j This refers to the memory growth rate of the closest stable time period preceding the first time period. is the average value of the memory growth, and m is the number of observations in the closest stable period before the first time period.
[0083] In step S14, a second growth rate is determined based on the first average growth rate and the second average growth rate to obtain the memory data for the second time period, which is then used as the third memory data.
[0084] This step involves analyzing the growth rates of memory data over two time periods—the first average growth rate and the second average growth rate—to determine a new time period. This new time period represents the period within the first time period where the memory growth rate fluctuates the most. Specifically, by analyzing the changes in the memory growth rate within the first time period, several periods with abnormal growth rate changes are identified. By comparison, the time interval between the fastest and slowest memory growth rates is found and defined as the second time period. Memory data within this second time period is defined as the third memory data. The purpose of this step is to further refine the trend of memory changes and provide more accurate data support for subsequent analysis.
[0085] It should be noted that the third memory data primarily relies on calculating the first and second average growth rates and judging based on their relationship. Specifically, firstly, the first average growth rate needs to be obtained, which refers to the average growth rate within the previously determined period of abnormal memory growth rate, i.e., the first time period. Secondly, the second average growth rate refers to the average growth rate within a relatively stable period prior to the first time period. By comparing the relationship between these two growth rates, a second growth rate judgment can be made. If the product of the first and second average growth rates is less than zero, it indicates a change in the direction of the growth rate, and the growth rate needs to be recalculated and the judgment continued; if the product equals zero, the detection stops; if the product is greater than zero, it indicates that the growth rates are in the same direction, and the abnormal data in the first time period needs to be analyzed. The second time period is determined by analyzing the period with the largest change in memory growth rate in the first time period. Specifically, by analyzing the changes in memory growth rate in the first time period, several periods with abnormal growth rate changes are identified, and the time interval from the fastest to the slowest memory growth rate is found by comparison. This interval is defined as the second time period, and the memory data of the second time period provided by the operating system is read by the computer and defined as the third memory data.
[0086] Optionally, this step involves a judgment process based on mathematical and statistical principles, identifying abnormal patterns in memory usage by comparing the changing trends of two growth rates. This method helps to more accurately pinpoint the problematic time period during memory leak detection, providing crucial information for subsequent memory leak assessment.
[0087] The specific method for determining the second growth rate is as follows:
[0088] If a×b<0, then recalculate the first average growth rate and the second average growth rate, and continue to make the second growth rate judgment;
[0089] If a × b = 0, then stop the detection;
[0090] If a×b>0, then record the time period from the fastest growth rate to the slowest growth rate within the first time period as the second time period, and use the memory data of the second time period as the third memory data;
[0091] Where a is the first average growth rate and b is the second average growth rate.
[0092] In step S15, a third average growth rate is calculated based on the third memory data.
[0093] This step primarily focuses on the change in the growth rate of memory data after the second time period. The third average growth rate is determined by analyzing the memory growth during the immediately following stable period after the second time period, thus establishing the average growth rate over that period.
[0094] It should be noted that the third average growth rate is achieved by calculating the memory growth rate over the closest stable period following the second time period. This process involves calculating the memory growth at each point in time within that period and then averaging these growth rates. Specifically, the total duration and number of observations of that period need to be determined, and then this data is used to calculate the average growth rate. This calculation process is based on time series analysis methods, using statistical and mathematical processing to quantify the trend of memory growth.
[0095] Optionally, the calculated third average growth rate can be directly used to assess the stability of memory usage during this period. For example, by observing the magnitude of the third average growth rate, one can initially determine the fluctuations in memory usage during this period. If the third average growth rate remains within a small range, it indicates that memory usage is relatively stable during this period; conversely, if the third average growth rate is large, it indicates that there may be significant memory fluctuations. Therefore, the calculation of the third average growth rate is an important indicator for measuring memory growth after the second time period. This indicator helps in further analyzing memory usage patterns and provides data support for determining whether memory leaks exist.
[0096] The third average growth rate is calculated using the following formula:
[0097]
[0098] Where B is the third average growth rate, ΔM i denoted as , where is the memory growth at the i-th time point within the closest stable time period following the second time period; T is the total duration of the closest stable time period following the second time period; and z is the number of observations within the closest stable time period following the second time period.
[0099] In step S16, the growth amount of the third memory data is calculated based on the third memory data and the third average growth rate.
[0100] This step involves quantifying the growth and changes in memory data. Specifically, after identifying time windows with abnormal memory growth rates, it calculates the amount of memory data growth during that period. The core of quantifying memory data growth and changes lies in using mathematical calculations to accurately measure the actual increase in memory usage under specific conditions, thereby providing crucial data for further memory leak analysis.
[0101] The growth of the third memory data involves in-depth analysis of this data. Specifically, firstly, it is necessary to determine a stable period following the second time period and calculate the average growth rate within that period, i.e., the third average growth rate. Then, using this average growth rate and the memory data within a specific time window, the growth amount of the third memory data is calculated using a formula.
[0102] It should be noted that the increase in the third memory data is used to identify the difference between this increase and the memory growth pattern during normal system operation. In this step, the increase in the third memory data is compared with the third average growth rate to determine whether the memory growth within a specific time period exceeds the normal fluctuation range. This method helps identify memory leaks that may not be detected by conventional monitoring methods because it provides a quantitative indicator to measure the actual growth in memory usage. Through analysis, potential memory leaks can be located more precisely, allowing for appropriate optimization measures to be taken.
[0103] The first growth amount is calculated using the following formula:
[0104] A2 = A1 – B*(t2 – t3)
[0105] Where A2 is the first growth amount, A1 is the memory growth amount in the second time period, B is the third average growth rate, t2 is the second time period, and t3 is the time period after the second time period where the memory growth rate is significantly higher than the normal growth rate again.
[0106] In step S17, a memory leak is determined based on the first growth amount and the second average growth rate data, and a memory leak result is obtained.
[0107] This step involves comprehensively analyzing the increase in memory data and the average growth rate to determine if a memory leak exists in the system. The core of this step is to determine whether there is an abnormal growth pattern in memory usage, i.e., a memory leak, by comparing the initial increase and the second average growth rate.
[0108] It is worth noting that this step first uses the first growth amount, the first average growth rate, and the third average growth rate as a basis. By calculating and comparing these data, key indicators for judging memory leaks are obtained.
[0109] It should be noted that the memory leak detection is based on the mathematical relationship between the first growth amount, the first average growth rate, and the third average growth rate. Specifically, if the first growth amount is negative and the first average growth rate is lower than the third average growth rate, then a memory leak can be identified. This judgment is based on the abnormal changes in the memory growth amount and growth rate, indicating a problem in the memory allocation and release process. If the first growth amount is not negative, it indicates that the growth rate of memory data is within normal fluctuations, and there is no sign of a memory leak.
[0110] In summary, this invention proposes a method for detecting memory leaks. It intelligently identifies memory growth trends by analyzing memory data in real time and accurately calculates the amount of memory growth and leakage. The method includes real-time reading and analysis of memory data, calculation of the average growth rate over a specific time period, and comparison with the average growth rate over a stable period. Through a series of calculation steps, including recording memory growth at key time points, calculating the average memory growth over a stable period, and calculating the memory growth value after the memory growth rate decreases, this invention can comprehensively determine the memory leak situation and output the memory leak rate. Furthermore, this invention also provides corresponding detection devices and electronic equipment, as well as storage media, to automate and intelligently implement this detection method.
[0111] Existing technologies for memory leak detection have several shortcomings, such as the inability to accurately determine the occurrence time, leakage amount, and cumulative amount of memory leaks, the inability to automate detection, and the need for significant manual intervention. This invention addresses these shortcomings by monitoring changes in memory data in real time to obtain first memory data. Based on this first memory data, a first growth rate is determined to obtain second memory data. Based on the second memory data, calculations are performed to obtain a first average growth rate and a second average growth rate. Based on the first and second average growth rates, a second growth rate is determined to obtain third memory data. Based on the third memory data, a third average growth rate is calculated. Based on the third memory data and the third average growth rate, a first growth amount is calculated. Based on the first growth amount and the third average growth rate, a memory leak is determined, yielding the memory leak result. This method not only improves detection accuracy but also reduces manual intervention, achieving automation in memory leak detection. By calculating the growth rate and memory growth amount and combining this with a comprehensive judgment based on actual conditions, this invention can promptly detect memory leaks and provide quantitative leak data, thus providing strong technical support for system maintenance and optimization.
[0112] Compared to existing technologies, this invention offers the following advantages: First, it can monitor and analyze memory data in real time, promptly detecting memory leaks and improving the efficiency and accuracy of memory management. Second, by accurately calculating memory growth and leakage, this invention provides a quantitative method for memory leak detection, enabling maintenance personnel to more accurately understand the system's memory status. Furthermore, the automated and intelligent detection device of this invention reduces manual intervention, saving manpower and material costs and shortening testing time. Finally, when a memory leak is detected, this invention can issue a warning signal to the user, improving system stability and user experience. In summary, this invention, through its innovative detection method and device, provides an efficient, accurate, and automated solution for memory leak detection.
[0113] Reference Figure 2 The second embodiment of the present invention provides a memory leak detection device, comprising:
[0114] The data acquisition module is used to acquire the first memory data;
[0115] The first growth rate judgment module is used to make a first growth rate judgment based on the first memory data to obtain the memory data of the first time period, which is used as the second memory data.
[0116] The first data calculation module is used to perform calculations based on the second memory data to obtain the first average growth rate and the second average growth rate.
[0117] The second growth rate judgment module is used to make a second growth rate judgment based on the first average growth rate and the second average growth rate to obtain the memory data of the second time period as the third memory data.
[0118] The second data calculation module is used to perform calculations based on the third memory data to obtain the third average growth rate.
[0119] The third data calculation module is used to calculate the first growth amount based on the third memory data and the third average growth rate.
[0120] The memory leak detection module is used to detect memory leaks based on the first growth data and the third average growth rate, and obtain the memory leak result.
[0121] In one optional implementation, the step of determining a first growth rate based on the first memory data to obtain memory data for a first time period, which is then used as the second memory data, includes:
[0122] Based on the first memory data, determine whether there is a time period in which the memory growth rate is significantly greater than the normal memory growth rate, and use it as the first time period. If so, use the memory data of the first time period as the second memory data. If not, reacquire the first memory data.
[0123] The first memory data includes the memory growth rate, normal memory growth rate, and memory growth amount of each memory data in each time period.
[0124] In one optional implementation, the step of calculating the first average growth rate and the second average growth rate based on the second memory data includes:
[0125] Calculate the average growth rate of the second memory data and determine it as the first average growth rate; calculate the average growth rate of the closest stable period before the start of the first time period and take it as the second average growth rate.
[0126] The first average growth rate and the second average growth rate are calculated using the following formulas:
[0127]
[0128] Where G1 is the first average growth rate, and k1 is the preset memory growth coefficient. G1 represents the variance of memory growth during the first time period, G2 represents the second average growth rate, and k2 represents a preset memory growth coefficient. The variance of memory growth is the closest stationary period preceding the first time period.
[0129] in,
[0130] Among them, X i This represents the memory growth during the first time period. The average value of the memory growth is given, and n is the number of observations within the first time period.
[0131] in,
[0132] Among them, X j This refers to the memory growth rate of the closest stable period preceding the first time period. is the average value of the memory growth, and m is the number of observations in the closest stable period before the first time period.
[0133] In one optional implementation, the step of calculating the third average growth rate based on the third memory data includes:
[0134] Calculate the average growth rate of the closest stationary period after the second time period, and use it as the third average growth rate.
[0135] The third average growth rate is calculated using the following formula:
[0136]
[0137] Where B is the third average growth rate, ΔM i denoted as , where is the memory growth at the i-th time point within the closest stable time period following the second time period; T is the total duration of the closest stable time period following the second time period; and z is the number of observations within the closest stable time period following the second time period.
[0138] In one optional implementation, the step of calculating the growth amount of the third memory data based on the third memory data and the third average growth rate includes:
[0139] Based on the third memory data, calculate the memory growth amount after the memory growth rate decreases in the second time period, and use it as the first growth amount;
[0140] The first increase is calculated using the following formula:
[0141] A2 = A1 – B*(t2 – t3)
[0142] Where A2 is the first growth amount, A1 is the memory growth amount in the second time period, B is the third average growth rate, t2 is the second time period, and t3 is the time period after the second time period where the memory growth rate is significantly higher than the normal growth rate again.
[0143] In one optional implementation, the step of determining a memory leak based on the first growth amount and the second average growth rate data to obtain a memory leak result includes:
[0144] Determine whether there is a memory leak based on the first growth amount and the second average growth rate data: if the first growth amount is negative and the first average growth rate is lower than the third average growth rate, then there is a memory leak; if the first growth amount is not negative, then the growth rate of the first memory data fluctuates normally and there is no memory leak.
[0145] It should be noted that the memory leak detection device provided in this embodiment of the invention is used to execute all the process steps of the memory leak detection method in the above embodiment. The working principle and beneficial effect of the two are one-to-one, so they will not be described again.
[0146] This invention also provides an electronic device. The electronic device includes a processor, a memory, and a computer program stored in the memory and executable on the processor, such as a first memory data acquisition program. When the processor executes the computer program, it implements the steps described in the above-described memory leak detection method embodiment, for example... Figure 1 The step S11 shown. Alternatively, when the processor executes the computer program, it implements the functions of each module / unit in the above-described device embodiments, such as the data acquisition module.
[0147] For example, the computer program may be divided into one or more modules, which are stored in the memory and executed by the processor to complete the present invention. The one or more modules may be a series of computer program instruction segments capable of performing a specific function, which describe the execution process of the computer program in the electronic device.
[0148] The electronic device may be a desktop computer, laptop, handheld computer, or smart tablet, etc. The electronic device may include, but is not limited to, a processor and memory. Those skilled in the art will understand that the above components are merely examples of electronic devices and do not constitute a limitation on the electronic device. It may include more or fewer components than described above, or combine certain components, or different components. For example, the electronic device may also include input / output devices, network access devices, buses, etc.
[0149] The processor can be a Central Processing Unit (CPU), or other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. A general-purpose processor can be a microprocessor or any conventional processor. The processor is the control center of the electronic device, connecting all parts of the electronic device via various interfaces and lines.
[0150] The memory can be used to store the computer programs and / or modules. The processor implements various functions of the electronic device by running or executing the computer programs and / or modules stored in the memory and by calling data stored in the memory. The memory may mainly include a program storage area and a data storage area. The program storage area may store the operating system, at least one application program required for a function (such as sound playback function, image playback function, etc.), etc.; the data storage area may store data created according to the use of the mobile phone (such as audio data, phonebook, etc.). In addition, the memory may include high-speed random access memory, and may also include non-volatile memory, such as hard disk, RAM, plug-in hard disk, smart media card (SMC), secure digital (SD) card, flash card, at least one disk storage device, flash memory device, or other volatile solid-state storage device.
[0151] Wherein, if the modules / units integrated in the electronic device are implemented as software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, all or part of the processes in the methods of the above embodiments of the present invention can also be implemented by a computer program instructing related hardware. The computer program can be stored in a computer-readable storage medium, and when executed by a processor, it can implement the steps of the various method embodiments described above. The computer program includes computer program code, which can be in the form of source code, object code, executable files, or certain intermediate forms. The computer-readable medium can include: any entity or device capable of carrying the computer program code, recording media, USB flash drives, portable hard drives, magnetic disks, optical disks, computer memory, read-only memory (ROM), random access memory (RAM), electrical carrier signals, telecommunication signals, and software distribution media, etc. It should be noted that the content included in the computer-readable medium can be appropriately added or removed according to the requirements of legislation and patent practice in the jurisdiction. For example, in some jurisdictions, according to legislation and patent practice, computer-readable media do not include electrical carrier signals and telecommunication signals.
[0152] It should be noted that the device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate, and the components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs. Furthermore, in the accompanying drawings of the device embodiments provided by this invention, the connection relationships between modules indicate that they have communication connections, which can be specifically implemented as one or more communication buses or signal lines. Those skilled in the art can understand and implement this without any creative effort.
[0153] The specific embodiments described above further illustrate the purpose, technical solution, and beneficial effects of the present invention. It should be understood that the above descriptions are merely specific embodiments of the present invention and are not intended to limit the scope of protection of the present invention. In particular, it should be noted that any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the scope of protection of the present invention for those skilled in the art.
Claims
1. A method for detecting memory leaks, characterized in that, Executed by a computer, including: Get the first memory data; Based on the first memory data, a first growth rate is determined to obtain memory data for the first time period, which is then used as the second memory data. Based on the second memory data, calculations are performed to obtain the first average growth rate and the second average growth rate; wherein, the first average growth rate refers to the average growth rate during the first time period in which the memory growth rate anomaly has been identified, and the second average growth rate refers to the average growth rate during a relatively stable period before the first time period of the anomaly. Based on the first average growth rate and the second average growth rate, a second growth rate determination is made to obtain the memory data for the second time period, which is then used as the third memory data. Based on the third memory data, a third average growth rate is calculated. The first growth amount is obtained by calculating based on the third memory data and the third average growth rate; Based on the first growth amount and the third average growth rate, a memory leak is determined, and the memory leak result is obtained. Specifically, based on the first average growth rate and the second average growth rate, a second growth rate determination is performed to obtain the memory data for the second time period, which serves as the third memory data, including: If the product of the first average growth rate and the second average growth rate is less than zero, the growth rate is recalculated and the judgment continues; if the product of the first average growth rate and the second average growth rate is equal to zero, the detection stops; if the product of the first average growth rate and the second average growth rate is greater than zero, the abnormal data in the first time period is analyzed. By analyzing the changes in the memory growth rate in the first time period, several time intervals with abnormal growth rate changes are determined. By comparison, the time interval from the fastest to the slowest memory growth rate is found, and the time interval is defined as the second time period. The memory data in the second time period is defined as the third memory data. The step of calculating the third average growth rate based on the third memory data includes: calculating the average growth rate of the closest stable time period after the second time period as the third average growth rate; and calculating the third average growth rate using the following formula: ; in, It is the third average growth rate. The second time period is the closest to the second time period within a stable time period. Memory growth at each point in time The total duration of the closest stable period following the second time period. This refers to the number of observations within the closest stable time period following the second time period.
2. The method for detecting memory leaks according to claim 1, characterized in that, The step of determining the first growth rate based on the first memory data to obtain memory data for a first time period, which is then used as the second memory data, includes: Based on the first memory data, determine whether there is a time period in which the memory growth rate is significantly greater than the normal memory growth rate, and use it as the first time period. If so, use the memory data of the first time period as the second memory data. If not, reacquire the first memory data. The first memory data includes the memory growth rate, normal memory growth rate, and memory growth amount of each memory data in each time period.
3. The method for detecting memory leaks according to claim 1, characterized in that, The calculation of the first growth amount based on the third memory data and the third average growth rate includes: Based on the third memory data, calculate the memory growth amount after the memory growth rate decreases in the second time period, and use it as the first growth amount; The first increase is calculated using the following formula: A2 = A1 – B*(t2 – t3) Where A2 is the first growth amount, A1 is the memory growth amount in the second time period, B is the third average growth rate, t2 is the end time of the second time period, and t3 is the start time of the time period after the second time period when the memory growth rate is significantly higher than the normal growth rate again appears.
4. The method for detecting memory leaks according to claim 1, characterized in that, The step of determining memory leaks based on the first growth amount and the third average growth rate, and obtaining the memory leak result, includes: The presence of a memory leak is determined based on the first growth amount and the third average growth rate: if the first growth amount is negative and the first average growth rate is lower than the third average growth rate, then a memory leak exists; if the first growth amount is not negative, then the growth rate of the first memory data fluctuates normally and there is no memory leak.
5. A memory leak detection device, characterized in that, include: The data acquisition module is used to acquire the first memory data; The first growth rate judgment module is used to make a first growth rate judgment based on the first memory data to obtain the memory data of the first time period, which is used as the second memory data. The first data calculation module is used to calculate based on the second memory data to obtain a first average growth rate and a second average growth rate; wherein, the first average growth rate refers to the average growth rate during the first time period in which the memory growth rate abnormality has been identified, and the second average growth rate refers to the average growth rate during a relatively stable period before the first time period of the abnormality. The second growth rate judgment module is used to make a second growth rate judgment based on the first average growth rate and the second average growth rate to obtain the memory data of the second time period as the third memory data. The second data calculation module is used to perform calculations based on the third memory data to obtain the third average growth rate. The third data calculation module is used to calculate the first growth amount based on the third memory data and the third average growth rate. The memory leak detection module is used to detect memory leaks based on the first growth amount data and the third average growth rate, and obtain the memory leak result. Specifically, based on the first average growth rate and the second average growth rate, a second growth rate determination is performed to obtain the memory data for the second time period, which serves as the third memory data, including: If the product of the first average growth rate and the second average growth rate is less than zero, the growth rate is recalculated and the judgment continues; if the product of the first average growth rate and the second average growth rate is equal to zero, the detection stops; if the product of the first average growth rate and the second average growth rate is greater than zero, the abnormal data in the first time period is analyzed. By analyzing the changes in the memory growth rate in the first time period, several time intervals with abnormal growth rate changes are determined. By comparison, the time interval from the fastest to the slowest memory growth rate is found, and the time interval is defined as the second time period. The memory data in the second time period is defined as the third memory data. The step of calculating the third average growth rate based on the third memory data includes: calculating the average growth rate of the closest stable time period after the second time period as the third average growth rate; and calculating the third average growth rate using the following formula: ; in, It is the third average growth rate. The second time period is the closest to the second time period within a stable time period. Memory growth at each point in time The total duration of the closest stable period following the second time period. This refers to the number of observations within the closest stable time period following the second time period.
6. An electronic device, characterized in that, The system includes a processor, a memory, and a computer program stored in the memory and configured to be executed by the processor, wherein the processor, when executing the computer program, implements a memory leak detection method as described in any one of claims 1 to 4.
7. A computer-readable storage medium, characterized in that, The computer-readable storage medium includes a stored computer program, wherein, when the computer program is executed, it controls the device on which the computer-readable storage medium is located to perform a memory leak detection method as described in any one of claims 1 to 4.