Memory leak detection method
By setting sampling interval conditions in the memory application sequence, only memory application information within the preset sampled memory size range is recorded, and this information is statistically analyzed to identify potential memory leakage points, solving the problem that the existing technology cannot effectively detect memory leakage in embedded devices, and achieving efficient and accurate memory leakage detection.
Patent Information
- Application Number
- CN202510064098.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-01-15
- Publication Date
- 2025-05-13
AI Technical Summary
The existing memory leak detection technology cannot be effectively applied to embedded devices with low memory and weak CPUs, making it difficult to accurately detect and resolve memory leak problems on these devices.
A memory leak detection method is adopted, including the sampling phase and the statistical phase. In the sampling stage, by setting the sampling interval conditions in the memory application sequence, only memory application information within the preset sampling memory size range is recorded. During the statistical phase, by statistically analyzing the information collected in the sampling phase, potential memory leak points are identified and memory leak reports are generated.
This approach significantly reduces the additional memory and CPU resources required for memory application stack recording, reduces detection overhead, and allows it to run efficiently on embedded devices with low-performance CPUs. By focusing on potential memory leak points, detection efficiency and accuracy are improved, and the possibility of false positives and underreports is reduced.
Smart Images

Figure CN119988196A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of memory leak detection, and in particular to a memory leak detection method. Background Art
[0002] Memory leak refers to the phenomenon that dynamically allocated memory blocks cannot be reused because they are not released in time during program execution. As time goes by, the unreleased memory gradually accumulates, which may eventually lead to system memory exhaustion, affecting program stability and performance, or even causing program crashes. Memory leaks are particularly prominent in systems that run for a long time, such as server applications or embedded devices.
[0003] In order to deal with the problem of memory leaks, the industry has designed and developed a large number of detection tools and methods, such as tcmalloc, jemalloc, memwatch, Sanitizer, valgrind, and perf. Although these tools and technologies can cover most memory leak scenarios, their practicality is significantly reduced in the field of resource-constrained embedded devices. The intrusiveness of memwatch requires modifying the source code, which is unacceptable in many embedded systems because these systems are often based on fixed, rigorously tested firmware versions. The limitation of Sanitizer is that it only reports memory leaks when the program exits, while embedded devices are often designed to run for a long time or even permanently, and rarely exit actively. The noise problem of jemalloc, its detailed memory usage statistics may generate a large number of invalid memory leak reports, increasing the difficulty of troubleshooting. The performance overhead of valgrind, embedded devices usually have limited computing resources and memory, and the high overhead of valgrind will seriously affect system performance and even cause the system to fail to operate normally. The most fundamental problem is that these tools have high requirements for device memory and CPU, which directly limits their application in embedded devices with low memory and weak CPU.
[0004] Given that embedded devices often need to run continuously for months or even years, long-term operation means that even a small memory leak may gradually accumulate to a level sufficient to affect the stable operation of the system. However, due to the above limitations, existing detection tools and methods are difficult to work effectively on these devices, thus exacerbating the complexity of the memory leak problem. Summary of the invention
[0005] The present invention aims to provide a memory leak detection method to solve the problem that the existing memory leak detection technology cannot be applied to embedded devices with low memory and weak CPU.
[0006] To achieve the above object, the present invention adopts the following technical solution: a memory leak detection method, including a sampling stage and a statistical stage;
[0007] In the sampling phase, on a continuous memory application sequence, the memory application at the corresponding position on the sequence is screened according to the sampling interval condition, and its memory application information is recorded;
[0008] The sampling interval condition is specifically as follows: on the memory application sequence, the sampling point is determined according to the size of the preset sampling memory as the interval, starting from the starting position of the memory application sequence, a sampling point is determined every other sampling memory; the memory application sequence is traversed, and for each memory application, it is determined whether it is marked with a sampling point; if it is satisfied, the data information of the memory application is recorded; if not satisfied, the memory application is skipped;
[0009] In the statistical phase, the memory application information collected in the sampling phase is statistically analyzed to identify potential memory leaks and generate a memory leak report.
[0010] The principle and advantage of this solution are: in the memory application sequence, not all memory applications are monitored, but memory applications are screened according to the preset sampling interval conditions. The sampling interval conditions are determined based on the size of the "sampling memory", that is, one memory application is selected as a sampling point every certain number of memory applications (the number is determined by the sampling memory size). Starting from the starting position of the memory application sequence, each memory application is judged in turn to see whether it meets the sampling conditions (that is, whether it is a sampling point). If the sampling conditions are met, the detailed information of the memory application (such as application size, application time, application location, etc.) is recorded. If the sampling conditions are not met, the memory application is skipped to reduce the burden of data collection and processing.
[0011] Perform statistical analysis on the memory application information collected during the sampling phase. Identify potential memory leaks by comparing memory usage at different time points. Generate a memory leak report to indicate which memory areas may have leaks and the extent of the leaks.
[0012] Since only the memory application information of the sampling point is recorded, the additional memory required to record the memory application stack is significantly reduced. This is especially important for embedded devices with limited memory resources. Compared with full statistics, sampling statistics greatly reduces the number of memory applications that need to be processed, thereby reducing the CPU usage and enabling the detection method to run efficiently on low-performance CPUs. Through sampling statistics, the number of stacks counted is reduced, making the analysis results more focused on potential memory leaks. This avoids the tedious work of screening through a large number of stacks and improves detection efficiency and accuracy.
[0013] Taking advantage of the fact that memory leaks will always leak and will not stop leaking after leaking to a certain extent, the sampling method based on memory size rather than time interval can better reflect the changes in memory usage patterns. In embedded devices, memory usage is often closely related to task execution rather than uniform distribution. Therefore, this sampling method is more adaptable to the actual memory usage scenarios of embedded devices, and the traditional sampling method using time intervals may not accurately reflect the actual memory usage. The method of the present invention not only improves the detection efficiency by reducing statistics and focusing on leakage points, but also reduces the possibility of false alarms and missed alarms. This is especially important for resource-constrained embedded devices, because both false alarms and missed alarms may lead to unpredictable system behavior or performance degradation.
[0014] Preferably, as an improvement, the content of the memory leak report includes the memory application stack, the size of the leaked memory, the number of memory applications for the same application stack and the residence time of the leaked memory.
[0015] The beneficial effects of this improvement are as follows: the memory application stack provides the application path of the leaked memory, which helps developers quickly locate the specific location of the leaked code; the size of the leaked memory directly reflects the severity of the leak, which facilitates the assessment of the impact of the leak on system performance; the number of memory applications in the same application stack reveals the frequency of the leak, which helps to identify high-frequency leak points, which are often the key to performance optimization; the residence time of the leaked memory reflects the duration that the leaked memory is occupied and not released, which helps to understand how the leaked memory accumulates over time.
[0016] Detailed leak information helps reduce false positives and false negatives. For example, by comparing the residence time of the leaked memory with the system's running time, you can exclude memory that is naturally released due to system restart or completion of a specific task. The number of memory requests for the same request stack provides an additional dimension to assess the severity of the leak, helping to distinguish between occasional leaks and persistent leaks. Although the content of the report has been increased, the resource consumption during the sampling phase has not increased. All additional information comes from in-depth analysis of the data collected during the sampling phase, so there is no additional burden on system performance. Detailed leak information helps developers locate and resolve leaks more accurately, thereby avoiding unnecessary waste of resources and performance degradation.
[0017] Preferably, as an improvement, the memory leak report only outputs leaked memory whose residence time is greater than a preset residence time.
[0018] The beneficial effect of this improvement is that since memory leaks themselves have the characteristic of not releasing memory for a long time, by setting the residence time threshold, those memory applications that are occupied briefly and released quickly can be filtered out, thereby reducing the noise in the report. Only the memory application stack with a long residence time is retained, making the report more concise and clear, helping developers quickly focus on the real leakage problem.
[0019] Short-term memory applications may appear frequently due to various reasons (such as normal task execution, temporary data storage, etc.), and these applications do not constitute real leaks. By setting the residence time threshold, these short-term memory applications can be excluded, thereby reducing the false alarm rate and improving the accuracy of detection. Unnecessary memory and CPU consumption is reduced because there is no need to record and count those short-term memory applications.
[0020] Preferably, as an improvement, when the program is started and the initialization operation is performed, a sampling delay time counter is started at the same time, and when the sampling delay time counter reaches a preset time threshold, the sampling phase is formally entered.
[0021] The beneficial effect of this improvement is that at the beginning of program startup, since the system is undergoing initialization operations, the memory usage pattern is often unstable and a large amount of resident memory is applied for and configured. Introducing the sampling delay time can give the program a buffer period so that it can complete initialization and enter a stable running state. In a stable state, the memory usage pattern is clearer, and dynamically applied memory fragments become the focus of memory leak detection. By avoiding sampling at the beginning of program startup, the error sampling caused by unstable state can be reduced, thereby improving the accuracy of memory leak detection.
[0022] At the beginning of program startup, the utilization of system resources (such as CPU and memory) is usually high, and sampling at this time may increase the system burden. Introducing a sampling delay time can delay the start of the sampling process and avoid sampling at a time when resources are tight, thereby reducing resource consumption. Sampling during the stable operation period of the program can more accurately reflect the actual memory usage and reduce unnecessary sampling data. Streamlined sampling data helps improve detection efficiency, allowing developers to locate and solve memory leaks more quickly.
[0023] Preferably, as an improvement, the statistical phase generates memory leak reports periodically according to a preset periodic time.
[0024] The beneficial effect of this improvement is that traditional memory leak detection usually outputs a report after the program stops running, which means that for embedded devices that need to run continuously, memory leaks may not be discovered for a long time. Regularly generating memory leak reports can ensure that memory leaks are discovered in time during program operation, thereby avoiding leakage accumulation that leads to system performance degradation or crashes.
[0025] Preferably, as an improvement, the LD_PRELOAD library function hijacking technology is used to count the application and release of process memory.
[0026] The beneficial effect of this improvement is that through the LD_PRELOAD library function hijacking technology, the application and release of process memory can be monitored in real time to ensure the real-time and accuracy of data. This technology can cover all memory application and release operations in the process, avoiding the omission problem that may exist in traditional methods. Using LD_PRELOAD technology, memory management functions can be easily encapsulated and modified to meet different memory leak detection requirements. By recording detailed information on memory application and release, including application stacks, etc., the debugging process of memory leak problems can be greatly simplified.
[0027] Preferably, as an improvement, the sampling memory used for interval sampling is 1 MB.
[0028] The beneficial effect of this improvement is that by selecting 1MB as the sampling memory size, the impact on system performance can be reduced while ensuring the detection effect, and the overhead of memory leak detection can be reduced. Through a reasonable sampling interval and sampling memory size, memory leak problems can be detected more efficiently and unnecessary waste of resources can be avoided. The sampling memory size of 1MB is suitable for a variety of scenarios and different types of programs, and has strong versatility and adaptability.
[0029] Preferably, as an improvement, the memory leak detection method further includes an analysis phase;
[0030] During the analysis phase, R&D personnel receive periodic memory leak reports and locate the specific code location based on the leaked memory application stack information in the report, analyze the cause of the memory leak and fix it.
[0031] The beneficial effect of this improvement is that R&D personnel can quickly locate the specific code location based on the leaked memory application stack information in the periodically output memory leak report, providing strong support for the repair of memory leaks. By analyzing the cause of the memory leak, R&D personnel can gain an in-depth understanding of the program's memory usage and potential problems, providing a basis for subsequent optimization and improvement. Based on the detailed memory leak report and analysis results, R&D personnel can formulate repair plans more specifically and improve the efficiency of repairing memory leak problems. Through regular memory leak detection and analysis, R&D personnel can promptly discover and repair new memory leak problems to ensure continuous optimization of program stability and performance. BRIEF DESCRIPTION OF THE DRAWINGS
[0032] Figure 1 It is a schematic structural diagram of an embodiment of the present invention. DETAILED DESCRIPTION
[0033] The following is further described in detail through specific implementation methods:
[0034] Example
[0035] Basically as attached Figure 1 As shown, a memory leak detection method is used to detect memory leaks in a resource-constrained system. Resource constraints include small memory, weak CPU processing power, etc.; it can effectively solve the memory leak detection problem of embedded devices, C / C++ programs, Linux system programs, etc.
[0036] A memory leak detection method uses the LD_PRELOAD library function hijacking technology of the GCC (GNU Compiler Collection) GNU compiler suite to count the application and release of the program's process memory. LD_PRELOAD is an environment variable in the Linux system, which allows users to specify one or more shared libraries (shared libraries) when the program starts. These libraries will be loaded before the default library. This mechanism can be used to overwrite (or "hijack") the standard library function originally called by the program. In this way, the application and release statistics of the process memory are realized.
[0037] Using the LD_PRELOAD library function hijacking technology, a dynamic library preloaded by the process is written. The dynamic library will hijack standard C / C++ interfaces such as malloc / alloc / realloc / free / new / delete, aiming to be able to count the application and release of process memory. Whenever the program calls the memory application function and meets the sampling conditions, the program's memory application call stack will be recorded, and then the possible leaked memory application stack log will be periodically output to provide problem analysis and solution.
[0038] As attached Figure 1 As shown, a memory leak detection method includes:
[0039] During the sampling phase, detailed information about the program's memory applications is captured with sampling interval conditions; continuous memory applications are discretized, and data statistics are collected only for memory applications that meet the sampling interval conditions.
[0040] The sampling interval condition is: in the memory application sequence, the sampling points are determined according to the preset sampling memory size as the interval. In the memory application sequence, starting from the starting position, a sampling point is determined every preset sampling memory size. For each sampling point, the information of the memory application to which its corresponding position belongs needs to be recorded. Traverse the memory application sequence, and for each memory application, determine whether it is marked with a sampling point. If satisfied, the detailed information of the memory application is recorded, including key data such as application size and application stack. If not satisfied, skip the memory application.
[0041] For example, the sampling memory is set to 1MB, that is, starting from the start position of the memory application sequence, a sampling point is determined every 1MB, then the sampling sequence is [S1 (1st MB), S2 (2nd MB), S3 (3rd MB), S4 (4th MB), ...].
[0042] When the memory request sequence is [R1(1MB), R2(0.5MB), R3(0.5MB), R4(0.1MB), R5(2MB)], R1(1MB) satisfies S1(1st MB) sampling;
[0043] R2 (0.5MB) is located at the (1,1.5] position of the memory request sequence. No sampling point falls within this position interval, so the acquisition interval condition is not met and it is not acquired.
[0044] R3 (0.5MB) is located at the (1.5,2] position of the memory request sequence, satisfying S2 (2nd MB) sampling;
[0045] R4 (0.1MB) is located at the (2,2.1] position of the memory request sequence. No sampling point falls within this position interval, so the acquisition interval condition is not met and it is not acquired.
[0046] R5 (2MB) is located at the (2.1,4.1] position of the memory application sequence. The sequence position range has sampling point S3 (3rd MB) and sampling point S4 (4th MB), which meets the sampling interval condition;
[0047] In summary, R1, R3, and R5 will be sampled, and the rest of the memory requests will be sampled in the same way.
[0048] This sampling interval condition uses the fact that memory leaks will continue to occur to perform sampling statistics on memory applications instead of full statistics. This reduces the extra memory and CPU resources required to record memory application stacks. This focuses on the leak point and reduces the workload of screening in a large number of stacks.
[0049] Among them, as attached Figure 1 As shown in the figure, the program includes the initial startup and stable operation period. In the initial startup of the program, the system usually performs a series of initialization operations, including loading necessary library files, configuring the system environment, and applying for a large amount of resident memory (Persistent Memory) to store data structures that need to be maintained for a long time during program operation. Memory applications at this stage are often large, continuous, and frequent, resulting in a rapid increase in memory usage.
[0050] Once the program enters the stable operation period, its memory usage pattern will tend to be stable. At this time, most memory application operations will be transformed into dynamic memory fragments, which are used to process temporary data or respond to user requests. These dynamic memory applications usually have the characteristics of short life cycle and high application frequency, and are easy to become the focus of memory leak detection.
[0051] A memory leak detection method adopts a sampling delay strategy in the sampling phase. By setting a reasonable delay time, it ensures that the sampling tool starts sampling after the program enters a stable operation period. Specifically, when the program starts and performs initialization operations, a sampling delay time counter is started at the same time. This sampling delay time is to give the program a buffer period so that the program can stabilize and enter a normal operation state, avoiding incorrect sampling caused by an unstable state at the beginning of the program startup. The specific length of the sampling delay time can be configured according to the characteristics and requirements of the program to ensure that the program has been fully initialized and is running stably before sampling begins.
[0052] When the sampling delay time counter reaches the preset time threshold, it means that the sampling delay time has been completed. The program will officially enter the sampling phase. In the sampling phase, the program will sample and record key information such as memory requests according to the preset sampling rules and strategies, and the sampling process will continue. In this way, unnecessary statistical memory consumption and CPU overhead are reduced, and memory sampling is avoided at the beginning of program startup to avoid the stage where memory usage changes sharply, so that the stack information of memory leaks can be captured and counted more accurately.
[0053] In the statistical phase, the memory application information collected in the sampling phase is statistically analyzed to identify potential memory leaks, and a memory leak report is generated regularly at a preset periodic time; the content of the memory leak report includes the memory application stack, the size of the leaked memory, the number of memory applications for the same application stack, and the residence time of the leaked memory. Due to the characteristic that memory leaks will not be released for a long time, the memory leak report only outputs the memory application stack whose residence time is greater than the preset residence time. The preset residence time is configured according to actual needs. This further simplifies the report content and improves readability.
[0054] During the analysis phase, R&D personnel receive periodic memory leak reports and locate specific code locations based on the leaked memory application stack information in the reports. Analyze the code to find out the cause of the memory leak. Perform code repairs for the located memory leak points. Verify the repair results to ensure that the memory leak problem is resolved.
[0055] The above is only an embodiment of the present invention, and the common knowledge such as the known specific technical solutions and / or characteristics in the solution is not described in detail here. It should be pointed out that for those skilled in the art, without departing from the technical solution of the present invention, several modifications and improvements can be made, which should also be regarded as the protection scope of the present invention, and these will not affect the effect of the implementation of the present invention and the practicality of the patent. The scope of protection required by this application shall be based on the content of its claims, and the specific implementation methods and other records in the specification can be used to interpret the content of the claims.
Claims
1. A memory leak detection method, characterized in that: It includes sampling stage and statistical stage; In the sampling phase, on a continuous memory application sequence, the memory application at the corresponding position on the sequence is screened according to the sampling interval condition, and its memory application information is recorded; The sampling interval condition is specifically as follows: on the memory application sequence, the sampling point is determined according to the size of the preset sampling memory as the interval, starting from the starting position of the memory application sequence, a sampling point is determined for every sampling memory; the memory application sequence is traversed, and for each memory application, it is determined whether it is marked with a sampling point; if it is satisfied, the data information of the memory application is recorded; If not satisfied, skip the memory request; In the statistical phase, the memory application information collected in the sampling phase is statistically analyzed to identify potential memory leaks and generate a memory leak report.
2. A memory leak detection method according to claim 1, characterized in that: The content of the memory leak report includes the memory application stack, the size of the leaked memory, the number of memory applications for the same application stack and the residence time of the leaked memory.
3. A memory leak detection method according to claim 2, characterized in that: The memory leak report only outputs the memory whose residence time of the leaked memory is greater than the preset residence time.
4. A memory leak detection method according to claim 3, characterized in that: When the program is started and initialized, a sampling delay time counter is started at the same time. When the sampling delay time counter reaches a preset time threshold, the sampling phase is officially entered.
5. A memory leak detection method according to claim 4, characterized in that: The statistical phase generates memory leak reports regularly according to a preset cycle time.
6. A memory leak detection method according to claim 5, characterized in that: The LD_PRELOAD library function hijacking technology is used to count the application and release of process memory.
7. A memory leak detection method according to claim 6, characterized in that: The sampling memory used for interval acquisition is 1MB.
8. A memory leak detection method according to claim 7, characterized in that: The memory leak detection method also includes an analysis phase; During the analysis phase, R&D personnel receive periodic memory leak reports and locate the specific code location based on the leaked memory application stack information in the report, analyze the cause of the memory leak and fix it.