Memory monitoring method and device
By collecting memory data at a set period, the memory growth trend is determined and the risk of memory leakage is calibrated, which solves the problems of high detection difficulty and affecting program operation in the existing technology, and achieves non-invasive, real-time memory leak detection.
Patent Information
- Application Number
- CN202410748211.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-11
- Publication Date
- 2024-09-27
AI Technical Summary
Existing technologies require intrusive code modifications or understanding of the process space structure when detecting memory leaks, which increases the difficulty and cost of detection, may affect program operation, and cannot be detected in real time.
Collect memory usage data at a set period to determine the memory growth trend of the process. Processes that exceed the preset trend are calibrated as memory leak risks and optimized to achieve non-intrusive and real-time detection.
It reduces the difficulty and cost of memory leak detection, avoids the impact on program running, and realizes real-time detection when the process is running.
Smart Images

Figure CN118708388A
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a memory monitoring method and device. Background Art
[0002] A memory leak refers to a situation where dynamically allocated heap memory is not released or cannot be released by the program for some reason during the running of an application. This will cause a waste of system memory, leading to serious consequences such as slowing down the program running speed or even system crashes.
[0003] In the related art, memory leaks are detected by inserting additional code into the programming language or by creating a virtual environment to execute the machine instructions of the program to monitor the memory access behavior. However, these methods require knowing the programming language of the program or at least understanding the spatial structure of the process and being able to stub the memory access behavior of the program. This not only increases the difficulty of detecting memory leaks and increases the detection cost, but may also affect the operation of the program. Summary of the invention
[0004] In view of this, the present invention provides a memory monitoring method and device, which determines the memory growth trend of a process based on the memory usage data collected in a set period, and marks the process whose memory growth trend exceeds the preset growth trend as a target process with a memory leak risk. This achieves the ability to monitor memory leaks without intrusive modification of the code of the monitored process or understanding the spatial structure of the process, and can perform real-time detection while the process is running without waiting for the process to exit, thereby reducing the difficulty and cost of detecting memory leaks and not affecting the operation of the program.
[0005] In order to solve the above technical problems, the present invention provides the following technical solutions:
[0006] In a first aspect, the present invention provides a memory monitoring method, comprising: collecting memory occupancy data corresponding to a plurality of processes respectively according to a set period, wherein the set period is dynamically adjusted according to the remaining memory of the system;
[0007] Determine the memory growth trends of the multiple processes in the historical time period and the predicted time period respectively according to the memory usage data;
[0008] Marking the process whose memory growth trend exceeds the preset growth trend as a target process with memory leakage risk;
[0009] The target process is optimized.
[0010] Optionally, the step of adjusting the set period includes:
[0011] Detecting the remaining memory of the system, and increasing the set period if the remaining memory of the system is greater than a preset memory threshold; and decreasing the set period if the remaining memory of the system is less than the preset memory threshold;
[0012] Optionally, a correspondence between the size of the remaining memory of the system and a plurality of set periods is determined; and the set period is adjusted according to the correspondence.
[0013] Optionally, determining the memory growth trends of the multiple processes in the historical time period and the predicted time period respectively according to the memory usage data includes:
[0014] Determine the historical time period according to a plurality of set periods before and after the adjustment, and determine the memory growth trends of the plurality of processes corresponding to the historical time period according to the memory usage data collected in the plurality of set periods;
[0015] The memory growth trend of the process in the predicted time period is predicted according to the memory growth trend corresponding to the process in the historical time period.
[0016] Optionally, the preset growth trend includes: linear growth, exponential growth, power function growth or logarithmic growth;
[0017] Determining the memory growth trends of the multiple processes respectively corresponding to the historical time period includes: performing, for each process: linear fitting of memory usage data collected in multiple set periods, and determining the memory growth trend of the process in the historical time period according to the linear fitting result;
[0018] The process of marking the process whose memory growth trend exceeds the preset growth trend as a target process with a memory leakage risk includes:
[0019] Determine whether the memory growth trend of the process conforms to any one of linear growth, exponential growth, power function growth or logarithmic growth; if so, determine the process as the target process.
[0020] Optionally, the preset growth trend further includes preset growth slopes corresponding to linear growth, exponential growth, power function growth or logarithmic growth, respectively. After determining that the memory growth trend of the process conforms to any one of linear growth, exponential growth, power function growth or logarithmic growth, the following further includes:
[0021] determining whether the slope of the memory growth trend of the process exceeds a preset growth slope corresponding to the preset growth trend, and if so, determining the process as the target process;
[0022] Optionally, predicting the memory growth trend of the process in the predicted time period according to the memory growth trend corresponding to the process in the historical time period includes:
[0023] The memory growth trend within the predicted time period is predicted based on the degree of overlap between the memory growth trend of the process within the historical time period and the preset growth trend, and / or the degree of overlap between the slope of the memory growth trend within the historical time period and the preset growth slope of the corresponding preset growth trend.
[0024] Optionally, when the remaining memory of the system is less than a preset memory threshold, the method further includes:
[0025] For each process execute:
[0026] determining a running time of the process;
[0027] Obtaining memory data requested by the process to be allocated from the operating system;
[0028] According to the running time and the memory data, updating the memory growth slope corresponding to the process;
[0029] When the memory growth slope is greater than a preset slope threshold, the process is determined as a target process.
[0030] Optionally, the optimizing the target process includes:
[0031] Release the memory occupied by the target process.
[0032] In a second aspect, an embodiment of the present invention provides a memory monitoring device, comprising: a collection module, a data analysis module and a processing module; wherein:
[0033] The collection module is used to collect memory usage data corresponding to multiple processes according to a set period, and the set period is dynamically adjusted according to the remaining memory of the system;
[0034] The data analysis module is used to determine the memory growth trends of the multiple processes in the historical time period and the predicted time period respectively according to the memory usage data;
[0035] The processing module is used to mark the process whose memory growth trend exceeds the preset growth trend as a target process with memory leakage risk, and optimize the target process.
[0036] In a third aspect, an embodiment of the present invention provides an electronic device for monitoring memory, comprising: one or more processors; a storage device for storing one or more programs, and when the one or more programs are executed by the one or more processors, the one or more processors implement a memory monitoring method of an embodiment of the present invention.
[0037] In a fourth aspect, an embodiment of the present invention provides a computer-readable storage medium having a computer program stored thereon, wherein the program, when executed by a processor, implements a memory monitoring method of an embodiment of the present invention.
[0038] The technical solution of the above invention has the following advantages or beneficial effects: the memory growth trend of the process is determined based on the memory usage data collected at a set period, and the process whose memory growth trend exceeds the preset growth trend is marked as a target process with a risk of memory leakage. This achieves the goal of monitoring memory leaks without intrusive modification of the code of the monitored process or understanding the spatial structure of the process, and can perform real-time detection while the process is running without waiting for the process to exit, thereby reducing the difficulty and cost of detecting memory leaks and not affecting the operation of the program. BRIEF DESCRIPTION OF THE DRAWINGS
[0039] Figure 1 It is a main flow chart of a memory monitoring method according to an embodiment of the present invention;
[0040] Figure 2 is a schematic diagram of main modules of a memory monitoring device according to an embodiment of the present invention;
[0041] Figure 3 is an exemplary system architecture diagram to which embodiments of the present invention may be applied;
[0042] Figure 4 It is a schematic diagram of the structure of a computer system suitable for implementing the embodiment of the present invention. DETAILED DESCRIPTION
[0043] The following is a description of exemplary embodiments of the present invention in conjunction with the accompanying drawings, including various details of the embodiments of the present invention to facilitate understanding, which should be considered as merely exemplary. Therefore, it should be recognized by those of ordinary skill in the art that various changes and modifications may be made to the embodiments described herein without departing from the scope and spirit of the present invention. Similarly, for clarity and conciseness, the description of well-known functions and structures is omitted in the following description.
[0044] It should be pointed out that the embodiments of the present invention and the technical features therein may be combined with each other without conflict.
[0045] Figure 11 is a schematic diagram of the main steps of a memory monitoring method according to an embodiment of the present invention, which mainly includes the following steps S101-S104:
[0046] Step S101: collecting memory usage data corresponding to a plurality of processes respectively according to a set period, wherein the set period is dynamically adjusted according to the remaining memory of the system.
[0047] In one embodiment of the present invention, the system remaining memory can be determined in each set period, and the next set period can be dynamically adjusted according to the system remaining memory. Exemplarily, after the system remaining memory is detected, the size between it and the set memory threshold can be compared. If the system remaining memory is greater than the preset memory threshold, the set period is extended; if the system remaining memory is less than the preset memory threshold, the set period is reduced. In the process of adjusting the set period, the next set period can be adjusted by finding the corresponding relationship between the size of the system remaining memory and multiple set periods. Generally, when the system remaining memory is less than the preset memory threshold, the next set period will be less than the current period, that is, in the corresponding relationship between the size of the remaining memory and multiple set periods, the smaller the remaining memory is, the smaller the set period is, and thus the next set period obtained according to the corresponding relationship will be less than the current period. In addition, the amplitude of increasing or decreasing the set period can also be pre-set. For example, if it is set to a fixed value of 1s or 5s, when the set period is adjusted, 1s or 5s is increased or decreased based on the current set period as the next set period; it can also be set to a fixed ratio. For example, if it is set to 5% or 10%, when the set period is adjusted, 5% or 10% is increased or decreased based on the current set period as the next set period.
[0048] Further, in order to improve the monitoring accuracy of memory leaks, in one embodiment of the present invention, if it has been determined that the remaining system memory is less than the preset memory threshold, the difference between the memory occupancy data collected in two adjacent set periods is also determined for each process, and if the size relationship between the difference and the preset value meets the preset condition, the continuous count value is incremented, and if the continuous count value is not less than the preset number, the set period is continued to be reduced. Take the adjustment of 4 consecutive periods as an example, where the preset memory threshold is 30%, the starting value of the continuous count value is 0, and the preset number is 2. The remaining system memory of period 1 is 35%, which is greater than the preset memory threshold, and there is no need to continue to reduce the set period; the remaining system memory of period 2 is 25%, which is less than the preset memory threshold, then not only is period 3 obtained by reducing 1s or 5s, or reducing 5% or 10% on the basis of period 2, but also the difference between the memory occupancy data collected in period 1 and period 2 is determined, and if the size relationship between the difference and the preset value meets the preset condition, the continuous count value is incremented from 0 to 1. The remaining system memory determined by cycle 3 is 23%. Similar to cycle 2, the difference between the memory occupancy data collected by cycle 2 and cycle 3 is determined. If the size relationship between the difference and the preset value meets the preset conditions, the continuous count value is incremented from 1 to 2. At this time, if the continuous count value is not less than the preset number, the set cycle is continued to be reduced. In this step, 1s or 5s can be reduced, or 5% or 10% can be reduced on the basis of cycle 3, and then the fixed value or fixed ratio can be further reduced to obtain cycle 4. The reduction range can also be directly increased. For example, when the continuous count value is less than the preset number, the reduction range is 5s or 10%, and after determining that the continuous count value is not less than the preset number, the reduction range is 7s or 13%. Of course, the maximum and minimum values of the set cycle can be pre-set so that the set cycle will not be infinitely increased or reduced in the process of adjusting the set cycle, thereby not only ensuring timely monitoring of memory leaks, but also avoiding excessive consumption of system resources.
[0049] Among them, the size relationship between the difference of memory occupancy data and the preset value can be divided into multiple situations. For example, if the difference is obtained by subtracting the memory occupancy data collected in the previous cycle from the memory occupancy data collected in the next cycle, the preset value can be set to 0 or a negative value, and the preset condition is that the difference is less than the preset value; if the difference is obtained by subtracting the memory occupancy data collected in the next cycle from the memory occupancy data collected in the previous cycle, the preset value can be set to a positive value, and the preset condition is that the difference is greater than the preset value. In other words, if the memory occupancy data collected later is greater than the memory occupancy data collected earlier and the difference between the two is greater than the absolute value of the preset value, it is determined that the preset condition is met. This shows that when the remaining memory of the system is small, the memory occupancy data collected for n consecutive cycles are increasing, indicating that there is a high probability of memory leakage. At this time, the set cycle is further reduced, so that the process can be monitored in time, which is convenient for timely detection of memory leakage, which is conducive to improving the accuracy of memory leakage.
[0050] In one embodiment of the present invention, the corresponding relationship between the size of the remaining system memory and a plurality of set periods can also be determined first, and then when determining the next set period, the set period can be adjusted according to the corresponding relationship. The corresponding relationship between the remaining system memory size and the plurality of set periods can be shown in Table 1, so after determining the remaining system memory, the corresponding set period can be determined according to the corresponding relationship shown in Table 1. If the remaining system memory determined by the current set period is 15%, the next set period is 30s, that is, the memory occupancy data of the process is collected again after an interval of 30s. In addition, it can be understood that when constructing the corresponding relationship between the remaining system memory and the set period, it can be determined according to the percentage of the remaining system memory as shown in Table 1, or it can be directly constructed according to the absolute value of the remaining system memory (such as 10G or 20G, etc.). In this embodiment, by constructing the corresponding relationship between the remaining system memory and the set period, the set period can be directly determined according to the corresponding relationship after determining the remaining system memory, without additional calculation, and the execution cost is low, which is conducive to the timely collection of memory occupancy data, thereby facilitating memory monitoring.
[0051] Table 1
[0052] Logo System remaining memory (%) Set cycle 1 0~5(exclusive) 5s 2 5~10(not included) 10s 3 10~40(not included) 30s 5 >0 60s
[0053] Step S102: determining memory growth trends of the multiple processes in a historical time period and a predicted time period respectively according to the memory usage data.
[0054] In this step, the historical time period can be determined according to the multiple set periods before and after the adjustment, and the memory growth trends of the multiple processes corresponding to the historical time period can be determined according to the memory occupancy data collected in the multiple set periods, and then the memory growth trend of the process in the predicted time period can be predicted based on the memory growth trend corresponding to the process in the historical time period. Among them, the memory occupancy data can include the resident memory size, swap space and page of the process memory space. The historical time period can be a time period composed of multiple consecutive set periods, or it can be a time period corresponding to the removal of noise data in multiple consecutive set periods; wherein, the noise data refers to the set period in which the collected memory occupancy data obviously has errors or abnormalities. Thus, the memory growth trend is determined by the memory occupancy data collected in multiple set periods, so that not only the absolute size of the current heap memory is considered, but also the memory growth trend of the process in the future (prediction time period) can be obtained, thereby combining the short-term and long-term trends of the process to obtain its memory growth trend, thereby improving the predictability and noise resistance of memory monitoring.
[0055] When determining the memory growth trend corresponding to the historical time period, the memory usage data collected in multiple set periods can be linearly fitted, and the memory growth trend of the process in the historical time period can be determined based on the fitting results. In the linear fitting process, the memory usage data collected in each set period can be projected into the coordinate system to obtain the discrete coordinates of each memory usage data in the coordinate system, and then an appropriate model function (such as a polynomial function or the least squares method, etc.) is selected to fit the discrete coordinates. After obtaining the fitting result, the fitting result can be directly used as the memory growth trend of the process in the historical time period, or the fitting result can be further smoothed and used as the memory growth trend.
[0056] Step S103: Marking the process whose memory growth trend exceeds the preset growth trend as a target process with memory leakage risk.
[0057] Among them, the preset growth trends include: linear growth, exponential growth, power function growth or logarithmic growth, etc. Linear growth includes full linear growth, oscillating rise, and overall linear growth but occasional decline; power function growth includes quadratic power function growth or 1 / 2 power function growth, etc. If the growth trend of the process conforms to any of linear growth, exponential growth, power function growth or logarithmic growth, it means that the process is likely to have a memory leak, and this process is determined as the target process. It can be understood that in addition to the above-mentioned possible trends such as linear growth, exponential growth, power function growth or logarithmic growth, the result of linear fitting may also be a straight line with a slope of 0 or a periodic change curve (such as a sine curve, a triangle wave curve or a square wave curve, etc.). In this case, it means that the memory usage data of the process has not changed or changes periodically and regularly, and there is no risk of memory leakage for the time being, so these processes do not belong to the target process.
[0058] After determining the memory growth trend in the historical time period, the memory growth trend of the process in the predicted time period can be determined according to the degree of overlap between the memory growth trend of the process in the historical time period and the preset growth trend. For example, if the memory growth trend of process A in the historical time period completely conforms to the exponential growth trend, that is, the memory growth trend of process A completely overlaps with the preset exponential growth trend, then it is very likely that it will also be an exponential growth trend in the subsequent period, and therefore it is determined that its memory growth trend in the predicted time period is also an exponential growth trend.
[0059] Among them, the preset growth trend can also include preset growth slopes corresponding to each growth trend, that is, preset growth slopes corresponding to linear growth, exponential growth, power function growth or logarithmic growth, so that it can be further determined whether the process has a memory leak risk based on the slope of the memory growth trend. Specifically, it can be determined whether the slope of the memory growth trend of the process exceeds the preset slope threshold corresponding to the corresponding preset growth trend. If so, it means that the process not only meets any of linear growth, exponential growth, power function growth or logarithmic growth, but also its memory usage data growth exceeds the preset growth slope of the corresponding growth trend. The process may have a memory leak risk, so the process is used as the target process. Similar to the memory growth trend, after determining the slope in the historical time period, the memory growth trend of the process in the predicted time period can also be predicted based on the degree of overlap with the preset slope threshold. For example, although the memory growth trend conforms to the oscillating rise, the overlap between the slope of the memory growth trend and the preset slope threshold is low (such as the slope of the memory growth trend is much smaller than the preset slope threshold), which means that the process may grow at a smaller rate or even no longer grow in the future period, and the memory growth trend of the process in the predicted time period can be determined accordingly. That is to say, in an embodiment of the present invention, the memory growth trend and the slope can be combined to determine whether the process has a memory leak risk. After determining that the memory growth trend of the process conforms to any one of linear growth, exponential growth, power function growth or logarithmic growth, it is also determined whether the slope of the memory growth trend of the process exceeds the corresponding preset slope under the trend. If so, the process is determined to be the target process. Therefore, by combining the memory growth trend and the slope, the judgment of the target process is more accurate, thereby improving the monitoring accuracy of memory leaks.
[0060] Among them, the preset slope threshold can also be dynamically adjusted according to the remaining memory of the system, such as when the remaining memory of the system is less than the preset memory threshold, the preset slope threshold is reduced; when the remaining memory of the system is greater than the preset memory threshold, the preset slope threshold is increased. Of course, the dynamic adjustment of the preset slope threshold can be combined with the dynamic adjustment of the set period, that is, the two can be performed synchronously according to their respective adjustment standards. Thus, when the system memory is tight (the remaining memory of the system is less than the preset memory threshold), the collection period is shortened and the slope threshold is lowered, and the highest score process with a score exceeding the threshold is killed, which is conducive to freeing up memory space. Among them, the score of the process is calculated based on the memory growth trend combined with the current remaining memory of the system. The higher the process score, the more likely the process may run out of memory at a faster rate, that is, the stronger the tendency of the process to leak memory, and the more likely the process is to be optimized as a target process. For example, the memory monitoring method provided in the embodiment of the present invention can be encapsulated as a memory monitoring algorithm, whose input is the memory monitoring data of each process collected in each set period, and the output is the score of each process.
[0061] In one embodiment of the present invention, the memory growth slope of the process can also be dynamically updated to accurately monitor memory leaks. Specifically, when the remaining system memory is less than the preset memory threshold, the following can be performed for each process: determining the running time of the process; obtaining the memory data requested by the process to be allocated to the operating system; updating the memory growth slope corresponding to the process according to the running time and the memory data; and determining the process as a target process when the slope is greater than the preset slope threshold. Among them, the running time of the process can monitor the process in the time dimension, while the memory data requested by the process to be allocated to the operating system monitors the process from the dimension of memory resources occupied by the process. Therefore, by combining the running time of the process and the memory data requested to be allocated, the memory growth slope corresponding to the process is updated, thereby realizing the monitoring of the process from multiple dimensions, and when the memory growth slope is greater than the preset growth slope, the process is determined to be a target process with a risk of memory leakage, thereby facilitating accurate monitoring of memory leakage. Among them, the preset slope threshold can be a preset growth slope corresponding to linear growth, exponential growth, power function growth or logarithmic growth, that is, after determining that the memory growth trend of the process conforms to any one of linear growth, exponential growth, power function growth or logarithmic growth, its memory growth slope can be updated according to the running time and memory data of the process, and the memory growth slope is compared with the preset growth slope of the corresponding growth trend. If the memory growth slope exceeds the preset growth slope, the process is killed. Alternatively, the preset slope threshold can also be other preset values, that is, if the memory growth trend of the process does not conform to any one of linear growth, exponential growth, power function growth or logarithmic growth, but when the remaining memory of the system is less than the preset memory threshold, if the memory growth slope of a process exceeds the preset slope threshold (the threshold is the limit of whether the process needs to be killed), the process is killed, thereby avoiding the long-term occupation of the system memory by processes that take a long time and occupy more memory data when the remaining memory of the system is small.
[0062] In addition, in order to save system resources, before obtaining memory data, it is possible to first determine whether the running time of the process is greater than the preset time threshold, thereby only obtaining the memory data requested by the process whose running time is greater than the preset time threshold, and further updating the memory growth slope of the process in combination with its running time and memory data. Compared with monitoring all processes, this reduces the frequency of obtaining memory data and the amount of calculation for updating the memory growth slope, thereby saving system resources.
[0063] Step S104: Optimizing the target process.
[0064] In this embodiment, optimizing the target process means releasing the memory occupied by the target process. For example, if the target process has a circular reference relationship with other processes, the memory can be released by removing the circular reference relationship. Alternatively, a prompt message about the target process can be issued so that the programmer can release the memory in time according to the prompt message. In addition, in order to avoid important processes being killed by mistake, a whitelist mechanism can be introduced. For processes in the whitelist, even if their memory growth trend conforms to any of linear growth, exponential growth, power function growth or logarithmic growth and the slope of the memory growth trend exceeds the preset growth slope, they will not be killed.
[0065] The memory monitoring method provided by the embodiment of the present invention can be used in the QNX operating system and can also be applied to the Linux system. In the specific implementation process, the memory monitoring method provided by the embodiment of the present invention can be encapsulated as a memory monitoring algorithm, whose input is the memory monitoring data of each process collected in each set period, and the output is the score of each process. Among them, the score of the process is calculated based on the memory growth trend combined with the current remaining memory of the system. The higher the process score, the faster the process may run out of memory, that is, the stronger the tendency of the process memory leak, and the more likely the process is to be optimized as the target process. In addition, during the implementation process, due to the busy CPU in the boot stage, the memory occupancy data of the process can be suspended to save system resources. In addition, if the memory monitoring method is applied to the QNX operating system, since most processes in the QNX operating system have a short running time, if all processes are scored, a large amount of memory needs to be frequently applied / released, and even exceptions are thrown. Therefore, in this case, only processes with a running time greater than the preset time can be monitored, that is, only processes with a long-term existence (CPU time greater than the threshold) can be monitored, thereby improving the system operation stability and avoiding system abnormalities. In addition, the whitelist mechanism can also be used to avoid accidental killing of processes. For example, due to the operating characteristics of some processes, the system resources they occupy are on an upward trend; or there are critical processes that cannot be killed even if there is a memory leak. Such processes can be written into the whitelist to avoid being closed during the monitoring process due to the linear growth of their memory usage data.
[0066] The descriptions provided in the steps of the above embodiments are only used to help understand the method, structure and core idea of the present invention. For ordinary technicians in the technical field, without departing from the principle of the present invention, several improvements and modifications can be made to the present invention, and these improvements and modifications also belong to the scope of protection of the claims of the present invention.
[0067] According to a memory monitoring method of an embodiment of the present invention, it can be seen that the memory growth trend of the process is determined based on the memory usage data collected in a set period, and the process whose memory growth trend exceeds the preset growth trend is marked as a target process with a memory leak risk. This achieves the ability to monitor memory leaks without intrusive modification of the code of the monitored process or understanding the spatial structure of the process, and can perform real-time detection while the process is running without waiting for the process to exit, thereby reducing the difficulty and cost of detecting memory leaks and not affecting the operation of the program.
[0068] Figure 2 is a schematic diagram of main modules of a memory monitoring device according to an embodiment of the present invention, such as Figure 2 As shown, the memory monitoring device 200 of the embodiment of the present invention includes: a collection module 201, a data analysis module 202 and a processing module 203; wherein,
[0069] The collection module 201 is used to collect memory usage data corresponding to multiple processes according to a set period, and the set period is dynamically adjusted according to the remaining memory of the system;
[0070] The data analysis module 202 is used to determine the memory growth trends of the multiple processes in the historical time period and the predicted time period respectively according to the memory usage data;
[0071] The processing module 203 is used to mark the process whose memory growth trend exceeds the preset growth trend as a target process with memory leakage risk, and optimize the target process.
[0072] In one embodiment of the present invention, the acquisition module 201 is used to detect the remaining memory of the system, and when the remaining memory of the system is greater than a preset memory threshold, increase the set period; when the remaining memory of the system is less than the preset memory threshold, reduce the set period.
[0073] In one embodiment of the present invention, the acquisition module 201 is used to determine the corresponding relationship between the size of the remaining memory of the system and a plurality of set periods; and adjust the set period according to the corresponding relationship.
[0074] In one embodiment of the present invention, the data analysis module 202 is used to determine the historical time period based on multiple set periods before and after adjustment, and determine the memory growth trends of the multiple processes corresponding to the historical time period based on the memory usage data collected in the multiple set periods; based on the memory growth trends of the processes corresponding to the historical time period, predict the memory growth trends of the processes within the predicted time period.
[0075] In one embodiment of the present invention, the preset growth trend includes: linear growth, exponential growth, power function growth or logarithmic growth; the data analysis module 202 is used to execute for each process: linear fitting of the memory usage data collected in multiple set periods, and determining the memory growth trend of the process in the historical time period based on the linear fitting result; the processing module 203 is used to determine whether the memory growth trend of the process conforms to any one of linear growth, exponential growth, power function growth or logarithmic growth, and if so, determine the process as the target process.
[0076] In one embodiment of the present invention, the preset growth trend also includes preset growth slopes corresponding to linear growth, exponential growth, power function growth or logarithmic growth, respectively. The processing module 203 is also used to determine whether the slope of the memory growth trend of the process exceeds the preset growth slope corresponding to the preset growth trend after determining that the memory growth trend of the process conforms to any one of linear growth, exponential growth, power function growth or logarithmic growth. If so, the process is determined as the target process.
[0077] In one embodiment of the present invention, the data analysis module 202 is used to predict the memory growth trend within the predicted time period based on the degree of overlap between the memory growth trend of the process within the historical time period and the preset growth trend, and / or the degree of overlap between the slope of the memory growth trend within the historical time period and the preset growth slope of the corresponding preset growth trend.
[0078] In one embodiment of the present invention, the data analysis module 202 is also used to, when the remaining memory of the system is less than a preset memory threshold, execute, for each process: determine the running time of the process; obtain the memory data requested by the process to be allocated to the operating system; update the memory growth slope corresponding to the process based on the running time and the memory data; and, when the memory growth slope is greater than a preset slope threshold, determine the process as a target process.
[0079] In one embodiment of the present invention, the processing module 203 is used to release the memory occupied by the target process.
[0080] According to a memory monitoring device according to an embodiment of the present invention, it can be seen that the memory growth trend of the process is determined based on the memory usage data collected at a set period, and the process whose memory growth trend exceeds the preset growth trend is marked as a target process with a memory leak risk. This achieves the ability to monitor memory leaks without intrusive modification of the code of the monitored process or understanding the spatial structure of the process, and can perform real-time detection while the process is running without waiting for the process to exit, thereby reducing the difficulty and cost of detecting memory leaks and not affecting the operation of the program.
[0081] Figure 3 FIG. 3 shows an exemplary system architecture 300 to which the memory monitoring method or memory monitoring device according to an embodiment of the present invention can be applied. Figure 3 As shown, the system architecture 300 may include terminal devices 301, 302, 303, a network 304 and a server 305. The network 304 is used to provide a medium for communication links between the terminal devices 301, 302, 303 and the server 305. The network 304 may include various connection types, such as wired, wireless communication links or optical fiber cables, etc.
[0082] Users can use terminal devices 301, 302, 303 to interact with server 305 through network 304 to receive or send messages, etc. Terminal devices 301, 302, 303 can be installed with various communication client applications and control programs, such as programs that can control process status.
[0083] The terminal devices 301, 302, 303 may be various electronic devices with display screens and supporting web browsing, including but not limited to smart phones, tablet computers, laptop computers, desktop computers, etc. The server 305 may be a server that provides various services, such as a background management server that can run various processes and collect process memory usage data in real time, and feed back monitoring results (such as information of the target process) to the terminal device.
[0084] It should be noted that the memory monitoring method provided in the embodiment of the present invention is generally executed by the server 305, and accordingly, the memory monitoring device is generally arranged in the server 305. It should be understood that Figure 3 The number of terminal devices, networks and servers in the embodiment is only for illustration. Any number of terminal devices, networks and servers may be provided according to the implementation requirements.
[0085] Reference below Figure 4 , which shows a schematic diagram of the structure of a computer system 400 suitable for implementing an embodiment of the present invention. Figure 4 The computer system shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present invention.
[0086] like Figure 4As shown, the computer system 400 includes a central processing unit (CPU) 401, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 402 or a program loaded from a storage part 408 into a random access memory (RAM) 403. In the RAM 403, various programs and data required for the operation of the system 400 are also stored. The CPU 401, the ROM 402, and the RAM 403 are connected to each other via a bus 404. An input / output (I / O) interface 405 is also connected to the bus 404.
[0087] The following components are connected to the I / O interface 405: an input section 406 including a keyboard, a mouse, etc.; an output section 407 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker, etc.; a storage section 408 including a hard disk, etc.; and a communication section 409 including a network interface card such as a LAN card, a modem, etc. The communication section 409 performs communication processing via a network such as the Internet. A drive 410 is also connected to the I / O interface 1005 as needed. A removable medium 1011, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed on the drive 410 as needed, so that a computer program read therefrom is installed into the storage section 408 as needed.
[0088] In particular, according to the embodiments disclosed in the present invention, the process described above with reference to the flowchart can be implemented as a computer software program. For example, the embodiments disclosed in the present invention include a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program includes a program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from the network through the communication part 409, and / or installed from the removable medium 411. When the computer program is executed by the central processing unit (CPU) 401, the above-mentioned functions defined in the system of the present invention are executed.
[0089] It should be noted that the computer-readable medium shown in the present invention may be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. The computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present invention, a computer-readable storage medium may be any tangible medium containing or storing a program that can be used by or in combination with an instruction execution system, device or device. In the present invention, a computer-readable signal medium may include a data signal propagated in a baseband or as part of a carrier wave, which carries a computer-readable program code. This propagated data signal may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. Computer-readable signal media may also be any computer-readable medium other than computer-readable storage media, which may send, propagate or transmit a program for use by or in conjunction with an instruction execution system, apparatus or device. The program code contained on the computer-readable medium may be transmitted using any appropriate medium, including but not limited to: wireless, wire, optical cable, RF, etc., or any suitable combination of the above.
[0090] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present invention. In this regard, each box in the flow chart or block diagram can represent a module, a program segment, or a part of a code, and the above-mentioned module, program segment, or a part of a code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order from the order marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram or flow chart, and the combination of the boxes in the block diagram or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0091] The modules involved in the embodiments of the present invention may be implemented by software or hardware. The modules described may also be set in a processor, for example, it may be described as: a processor includes an acquisition module, a data analysis module and a processing module. The names of these modules do not constitute a limitation on the modules themselves in some cases, for example, the acquisition module may also be described as "a module for collecting memory usage data corresponding to multiple processes according to a set period".
[0092] As another aspect, the present invention also provides a computer-readable medium, which may be included in the device described in the above embodiment; or it may exist independently without being assembled into the device. The above computer-readable medium carries one or more programs, and when the above one or more programs are executed by a device, the device includes: collecting memory usage data corresponding to multiple processes according to a set period, and the set period is dynamically adjusted according to the remaining memory of the system; determining the memory growth trend of the multiple processes in the historical time period and the predicted time period according to the memory usage data; marking the process whose memory growth trend exceeds the preset growth trend as a target process with a memory leakage risk; and optimizing the target process.
[0093] According to the technical solution of the embodiment of the present invention, the memory growth trend of the process is determined based on the memory usage data collected in a set period, and the process whose memory growth trend exceeds the preset growth trend is marked as a target process with a memory leak risk. This achieves the ability to monitor memory leaks without intrusive modification of the code of the monitored process or understanding the spatial structure of the process, and can perform real-time detection while the process is running without waiting for the process to exit, thereby reducing the difficulty and cost of detecting memory leaks and not affecting the operation of the program.
[0094] The above specific implementations do not constitute a limitation on the protection scope of the present invention. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions may occur depending on design requirements and other factors. Any modification, equivalent substitution and improvement made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.
Claims
1. A memory monitoring method, It is characterized in that include: Collect memory usage data corresponding to multiple processes according to a set period, where the set period is dynamically adjusted according to the remaining memory of the system; Determine the memory growth trends of the multiple processes in the historical time period and the predicted time period respectively according to the memory usage data; Marking the process whose memory growth trend exceeds the preset growth trend as a target process with memory leakage risk; The target process is optimized.
2. The method according to claim 1, It is characterized in that The step of adjusting the set period includes: Detecting the remaining memory of the system, and increasing the set period if the remaining memory of the system is greater than a preset memory threshold; and decreasing the set period if the remaining memory of the system is less than the preset memory threshold; and / or, Determine the correspondence between the size of the remaining memory of the system and a plurality of set periods; and adjust the set period according to the correspondence.
3. The method according to claim 2, It is characterized in that Determining the memory growth trends of the multiple processes in the historical time period and the predicted time period respectively according to the memory usage data includes: Determine the historical time period according to a plurality of set periods before and after the adjustment, and determine the memory growth trends of the plurality of processes corresponding to the historical time period according to the memory usage data collected in the plurality of set periods; The memory growth trend of the process in the predicted time period is predicted according to the memory growth trend corresponding to the process in the historical time period.
4. The method according to claim 3, It is characterized in that The preset growth trend includes: linear growth, exponential growth, power function growth or logarithmic growth; Determining the memory growth trends of the multiple processes respectively corresponding to the historical time period includes: performing, for each process: linear fitting of memory usage data collected in multiple set periods, and determining the memory growth trend of the process in the historical time period according to the linear fitting result; The process of marking the process whose memory growth trend exceeds the preset growth trend as a target process with a memory leakage risk includes: Determine whether the memory growth trend of the process conforms to any one of linear growth, exponential growth, power function growth or logarithmic growth; if so, determine the process as the target process.
5. The method according to claim 4, It is characterized in that The preset growth trend also includes preset growth slopes corresponding to linear growth, exponential growth, power function growth or logarithmic growth, respectively. After determining that the memory growth trend of the process conforms to any one of linear growth, exponential growth, power function growth or logarithmic growth, the following is further included: determining whether the slope of the memory growth trend of the process exceeds a preset growth slope corresponding to the preset growth trend, and if so, determining the process as the target process; and / or, Predicting the memory growth trend of the process in the predicted time period according to the memory growth trend corresponding to the process in the historical time period includes: The memory growth trend within the predicted time period is predicted based on the degree of overlap between the memory growth trend of the process within the historical time period and the preset growth trend, and / or the degree of overlap between the slope of the memory growth trend within the historical time period and the preset growth slope of the corresponding preset growth trend.
6. The method according to claim 2, It is characterized in that When the remaining memory of the system is less than a preset memory threshold, the method further includes: For each process execute: determining a running time of the process; Obtaining memory data requested by the process to be allocated from the operating system; According to the running time and the memory data, updating the memory growth slope corresponding to the process; When the memory growth slope is greater than a preset slope threshold, the process is determined as a target process.
7. The method according to claim 1, It is characterized in that The optimizing the target process includes: Release the memory occupied by the target process.
8. A memory monitoring device, It is characterized in that include: Acquisition module, data analysis module and processing module; among them, The collection module is used to collect memory usage data corresponding to multiple processes according to a set period, and the set period is dynamically adjusted according to the remaining memory of the system; The data analysis module is used to determine the memory growth trends of the multiple processes in the historical time period and the predicted time period respectively according to the memory usage data; The processing module is used to mark the process whose memory growth trend exceeds the preset growth trend as a target process with memory leakage risk, and optimize the target process.
9. An electronic device for monitoring memory, It is characterized in that include: one or more processors; a storage device for storing one or more programs, When the one or more programs are executed by the one or more processors, the one or more processors implement the memory monitoring method as described in any one of claims 1-7.
10. A computer readable medium having a computer program stored thereon, It is characterized in that When the program is executed by a processor, the memory monitoring method as described in any one of claims 1-7 is implemented.
Citation Information
Cited By
Dual-system memory monitoring method, vehicle-mounted system, vehicle, equipment and storage medium
CN119226096A
Dual system memory monitoring method, vehicle-mounted system, vehicle, equipment and storage medium
CN119226096B
Application CPU occupation monitoring method and system and electronic equipment
CN119226097A
Android vehicle-mounted system CPU load monitoring method and device and Android vehicle-mounted system
CN119576702A
Periodic memory leak detection method, device and system
CN119807012A
