Memory leak detection method and device
By determining the set of memory allocations and releases within a target timeframe during program execution, and monitoring potential memory leaks in real time, this technology solves the problem of difficult detection of memory accumulation in smart containers in traditional technologies, thus improving the accuracy and reliability of memory leak detection.
Patent Information
- Application Number
- CN202510921334.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-07-03
- Publication Date
- 2025-10-24
AI Technical Summary
Traditional memory leak detection methods have difficulty identifying memory accumulation problems caused by smart containers with automatic memory management functions that fail to clean up expired elements in a timely manner during their life cycle. In particular, memory accumulation cannot be detected in time during program execution, resulting in performance degradation and system instability.
By determining the start and end times of the target time range within the time range from program startup to exit, and combining the memory allocation and release sets, the potential leak memory set is monitored and calculated in real time to verify whether it contains leaked memory, thereby improving the accuracy and reliability of detection.
It enables timely detection of memory accumulation problems during program execution, improving the accuracy and reliability of memory leak detection and preventing performance degradation and system instability.
Smart Images

Figure CN120832237A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a method and device for detecting memory leaks. Background Art
[0002] Memory leaks are a common problem in computer science. A memory leak occurs when a program's allocated memory isn't released promptly, leading to a continuous increase in memory usage. This wastes system resources and can cause performance degradation or even program crashes. Therefore, detecting memory leaks is crucial for ensuring stable program operation and optimizing performance.
[0003] Traditional memory leak detection methods record memory allocation and memory release operations during the entire program execution process, and calculate the leaked memory set based on the memory allocation set and memory release set when the program exits.
[0004] Traditional memory leak detection methods encounter the following problem: For smart containers with automatic memory management, if elements are continuously inserted during their lifecycle without timely clearing of expired elements, memory can gradually accumulate. Since these smart containers typically automatically release memory at the end of their lifecycle, expired elements that were not cleaned up in a timely manner are released. Traditional memory leak detection methods only calculate the leaked memory set when the program exits, making it difficult to identify memory accumulation issues during program execution. Summary of the Invention
[0005] The present invention provides a memory leak detection method and device to solve the problem of gradual memory accumulation caused by the continuous insertion of elements into an intelligent container with automatic memory management function during its life cycle without timely cleaning up expired elements in traditional technologies. Traditional memory leak detection methods calculate the leaked memory set only when the program exits, which makes it difficult to identify the memory accumulation problem during program operation.
[0006] The present invention provides a method for detecting memory leaks, comprising: determining a starting time and a target time of a target time range within a time range from program startup to program exit, and determining an end time of the target time range based on the target time and a set time threshold; determining a potential leak memory set based on a memory allocation set and a memory release set within the target time range; and the potential leak memory set is used to verify whether it contains leaked memory.
[0007] According to the memory leak detection method provided by the present invention, the starting time of the target time range is equal to or later than the time when the program is initialized.
[0008] The time point of the program initialization completion is determined by monitoring the memory usage of the program, and when the memory usage reaches a stable state, the time point of the program initialization completion is determined.
[0009] The time point of the program initialization completion is determined by monitoring the memory usage of the program, and when the memory usage reaches a stable state, the time point of the program initialization completion is determined.
[0010] The time point of the program initialization completion is determined by monitoring the memory usage of the program, and when the memory usage reaches a stable state, the time point of the program initialization completion is determined. The time point of the program initialization completion is determined by monitoring the memory usage of the program, and when the memory usage reaches a stable state, the time point of the program initialization completion is determined.
[0011] The time point of the program initialization completion is determined by monitoring the memory usage of the program, and when the memory usage reaches a stable state, the time point of the program initialization completion is determined. The time point of the program initialization completion is determined by monitoring the memory usage of the program, and when the memory usage reaches a stable state, the time point of the program initialization completion is determined. The time point of the program initialization completion is determined by monitoring the memory usage of the program, and when the memory usage reaches a stable state, the time point of the program initialization completion is determined.
[0012] The application provides a memory leakage detection device, comprising: A target time range determination module is configured to determine a start time point and a target time point of a target time range within a time range from program startup to program exit, and determine an end time point of the target time range according to the target time point and a set time threshold. A potential leakage memory determination module is configured to determine a potential leakage memory set according to a memory allocation set and a memory release set within the target time range, and verify whether the potential leakage memory set contains leakage memory.
[0013] The application further provides an electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the memory leakage detection method according to any of the above when executing the program.
[0014] The application further provides a non-transitory computer readable storage medium having a computer program stored thereon, wherein the computer program is executable on a processor to implement the memory leakage detection method according to any of the above.
[0015] The application further provides a computer program product comprising a computer program, wherein the computer program is executable on a processor to implement the memory leakage detection method according to any of the above.
[0016] The memory leakage detection method and device provided by the application determine a start time point and a target time point of a target time range, and determine an end time point of the target time range according to the target time point and a set time threshold, and then determine a potential leakage memory set according to a memory allocation set and a memory release set within the target time range, to verify and determine leakage memory, so as to determine the potential leakage memory set based on memory allocation and release behaviors within any time interval during program running, overcome the deficiency that memory accumulation caused by automatic memory release of the intelligent container in the conventional technology at program exit cannot be detected, and improve the accuracy and reliability of memory leakage detection. BRIEF DESCRIPTION OF DRAWINGS
[0017] In order to more clearly illustrate the technical solutions of the present application or prior art, the following will briefly introduce the drawings needed to be used in the embodiments or prior art description. Obviously, the drawings in the following description are some embodiments of the present application, and other drawings can be obtained by those skilled in the art without creative labor.
[0018] Figure 1 is a timing diagram for detecting memory leakage provided in the conventional technology.
[0019] Figure 2is one of the flow diagrams of the memory leakage detection method provided by the application.
[0020] Figure 3 is one of the timing diagrams of the memory leakage detection provided by the application.
[0021] Figure 4 is the second timing diagram of the memory leakage detection provided by the application.
[0022] Figure 5 is the second flow diagram of the memory leakage detection method provided by the application.
[0023] Figure 6 is the structural diagram of the memory leakage detection device provided by the application.
[0024] Figure 7 is the structural diagram of the electronic device provided by the application. DETAILED DESCRIPTION
[0025] In order to make the objects, technical solutions and advantages of the present application clearer, the technical solutions in the present application will be described clearly and completely below in combination with the drawings in the present application. Obviously, the described embodiments are part of the embodiments of the present application, rather than all the embodiments. Based on the embodiments in the present application, all other embodiments obtained by those skilled in the art without creative labor fall within the protection scope of the present application.
[0026] In order to schematically illustrate the memory leakage detection method in the prior art, Figure 1 a process of memory leakage detection in the prior art is shown, mainly including the following stages: 1. Program startup stage When the program starts, the program is loaded into the memory by the operating system, the initial memory space is allocated, and the executable file and the related dynamic link library are loaded.
[0027] The memory detection tool can be attached to the program process when the program starts, and intercept the memory allocation and release calls. When the program calls these memory allocation and release functions, the actual execution is the replacement function provided by the memory detection tool, so as to record the detailed information of each memory operation, such as the address and size of the allocated memory block, the time stamp of the allocation, the call stack, etc.
[0028] This step is the basis of the whole memory leakage detection. By intercepting the memory allocation and release calls, it can be ensured that all memory operations are recorded, providing data support for subsequent analysis.
[0029] 2. Program initialization stage In the initialization phase, the program performs a series of preparations, loading necessary resources such as configuration files, resource files, plugins, etc.
[0030] During the initialization process, the program allocates memory to store various initialization data and objects. These memory allocation operations are recorded by memory detection tools. There may also be some memory release operations, such as temporary buffers during resource loading, which are also recorded.
[0031] 3、Program running phase When the program initialization is completed, the memory usage reaches a basic stable state, and this moment is marked as t0'. The memory allocated before t0' is usually some persistent memory, which may need to be kept valid throughout the program's life cycle.
[0032] During the program running process, the allocation and release of memory are continuously monitored. Each memory allocation or release operation is recorded, including the memory block address and size, the timestamp of allocation, the call stack information, etc.
[0033] 4、Program exit phase From the start of the program, the memory blocks allocated by the program and the memory blocks released by the program are continuously recorded. Among them, the total memory blocks allocated by the program are recorded as the memory allocation set A = {a1, a2,..., a n}; The memory allocation set records all the memory blocks allocated from the program startup phase to the program exit phase; the total memory blocks released by the program are recorded as the memory release set F = {f1, f2,..., f m}, and the memory release set records all the memory blocks released from the program startup phase to the program exit phase.
[0034] At the program exit, according to the memory allocation set (A) and the memory release set (F), the potential leakage memory set P leak = Unreleased memory set L = A - F. If L ≠ ∅, there may be memory leakage. All elements in the unreleased memory set L are the potential leakage memory set (P leak ).
[0035] Smart containers are objects with automatic memory management functions. They automatically handle memory allocation and release during program running to simplify the developer's operations. For example, in C++ STL containers (such as list, map, etc.), when elements are inserted into the container, the container will automatically allocate memory to store these elements; when elements are deleted, the container will automatically release the corresponding memory.
[0036] However, in some cases, the smart container may have a problem of memory accumulation. For example, when the program continuously inserts elements into the smart container during its lifetime, but does not clean up the expired elements in time, the memory occupied by the container will gradually increase. Since the smart container automatically releases memory at the end of its lifetime, this memory accumulation is difficult to be discovered by traditional memory leak detection methods during the program running process.
[0037] Traditional memory leak detection methods mainly rely on comparing memory allocation and release records after the program normally exits to detect leaks. However, for the memory accumulation problem of smart containers, since the memory release of the container is performed only when the program exits, the traditional detection method cannot timely discover this memory accumulation during the program running process, resulting in the inability to timely handle the problem when it occurs.
[0038] In addition, the technical defects of the method in the prior art include: When the program uses mechanisms such as cache and object pool, the program may continuously apply for memory, but does not release it in time after the data expires, resulting in memory accumulation, which is released uniformly only when the process exits. The traditional memory leak detection method cannot timely discover the problem of memory accumulation during the program running process, thereby failing to effectively prevent the performance degradation and system instability caused by memory leaks.
[0039] It needs to be explained that cache is a technology used to improve data access speed and program running efficiency. It stores frequently accessed or computationally expensive data in memory, so that the program can quickly obtain the data when needed, without the need to recalculate or read from storage devices, thereby significantly improving performance. When the data in the cache is updated, the cache mechanism ensures that the data in the memory is consistent with the actual data source (such as a database or a file system).
[0040] Object pool is a design pattern that pre-creates and maintains a group of object instances for quick reuse when needed, rather than creating new objects each time. This approach is particularly useful in scenarios where objects are frequently created and destroyed, as it can reduce the overhead of object creation and destruction, improving program performance and resource utilization. Object pool needs to manage the lifecycle of objects, including object creation, acquisition, release, and destruction. At the same time, object pool also needs to handle the state of objects to ensure that objects are in the correct state when reused.
[0041] In the cache and object pool, if the data or object is not removed from the memory correctly, it may cause memory leakage. For example, the data in the cache may always occupy the memory because of improper eviction policy or update; the object in the object pool may cause high memory occupation because it is not put back into the pool correctly or the size of the pool is not set reasonably, thereby causing the performance of the system to decrease and the system to be unstable.
[0042] In order to solve the technical defects in the prior art, the memory leakage detection method provided by the embodiments of the present application can determine a potential leakage memory set based on memory allocation and release behaviors in any time interval during program running, without waiting for the program to exit to determine the potential leakage memory set, so that memory accumulation problems can be found in time, and the accuracy and reliability of memory leakage detection are improved. The following content describes the implementation process of the embodiments of the present application in detail.
[0043] First, the terms related to the embodiments of the present application are explained.
[0044] Initialization: Initialization refers to a series of preparation work after the program starts, including allocating initial memory, loading necessary resources (such as files, functions, plug-ins, etc.), until the program reaches a basic stable state and can start normal operation. For example, in a player program, initialization may include loading the user interface, reading the configuration file, allocating the initial memory buffer, etc. When these steps are completed, the player enters a stable state.
[0045] Memory allocation: Memory allocation refers to that, during the running of the program, a memory space is applied to the operating system through a system call function (such as malloc, calloc function, etc.) for storing data, variables, objects, etc.
[0046] Memory release: Memory release refers to that, after the program uses the memory, the memory that is no longer needed is returned to the operating system through a system call function (such as the free function), so that other programs or modules can reuse the memory.
[0047] Memory allocation set: Define the memory block set allocated by the program at time t as A(t), that is, A(t) = {a1, a2,…,a n}, where a i is the i-th memory block allocated at time [0-t].
[0048] Memory release set: Define the memory block set released by the program at time t as F(t), that is, F(t) = {f1,f2,…,f m}, where f i is the i-th memory block released at time [0-t].
[0049] Memory leak: Memory leak refers to the phenomenon that the memory allocated by a program is not released in time during the program's running process, leading to the continuous increase of memory usage and wasting of system resources, which may cause the program's performance to decline or even crash.
[0050] Potential leak memory: Potential leak memory refers to memory blocks that are not released in time during the program's running process and are likely to leak from the memory usage pattern or program logic.
[0051] Leak memory: Leak memory refers to memory blocks that are not released as expected during the program's running process. These memory blocks are not properly released during the program's running process, which may cause abnormal growth of memory usage and affect the program's performance.
[0052] Unreleased memory: Unreleased memory refers to memory blocks that are allocated by a program during its running process but have not been released at a certain time point. These memory blocks are still occupied by the program but may be released in the subsequent program running.
[0053] Unreleased memory set: The set of unreleased memory blocks at time t is defined as L(t), i.e., L(t) = A(t) - F(t).
[0054] Target time range: The target time range refers to a specific time interval selected during the program's running process to detect memory allocation and release within that interval to determine whether there is a memory leak. Suppose the program has been running for 100 seconds, and the time interval from 20 seconds to 80 seconds is selected as the target time range to detect memory allocation and release within that interval.
[0055] Time threshold: The time threshold is a time standard set to distinguish between memory leaks and normal memory usage during memory leak detection. Specifically, after determining the target time, the memory allocation close to the target time is excluded from the potential leak memory, which can reduce the false positives caused by recently allocated memory blocks. For example, if the time threshold is set to 10 seconds and the time interval from 20 seconds to 80 seconds is selected as the target time range, then when determining the potential leak memory set, the memory blocks allocated within the time range from 70 seconds to 80 seconds are excluded.
[0056] Figure 2 is one of the flowcharts of the memory leak detection method provided by the present application, as shown in Figure 2 The method comprises the following steps: Step 201, determining the starting time of the target time range and the target time within the time range from the program's startup to its exit, and determining the ending time of the target time range according to the target time and the set time threshold.
[0057] The starting time of the target time range can be any time after the program is started.
[0058] Preferably, the starting time of the target time range is equal to or later than the time when the program initialization is completed. Memory allocation during the program initialization phase has its own characteristics, and most of it is the persistent memory allocation necessary for the subsequent running of the program, which is significantly different from the memory allocation and possible leakage behavior during the normal running of the program. Setting the starting time of the target time range after the initialization is completed can effectively avoid misjudging the normal memory allocation behavior in the initialization phase as a potential memory leak, thereby reducing false positives and improving the accuracy of the detection results.
[0059] When the program is started, the memory detection tool or the custom memory monitoring module starts working, intercepts the memory allocation and release operations of the program, and records the memory usage in real time.
[0060] For the time when the program initialization is completed, the memory usage of the program can be monitored, and when the memory usage reaches a stable state, the time when the program initialization is completed is determined. Specifically, the change curve of the memory usage of the program over time can be analyzed in real time, and a memory usage change rate threshold (e.g., 0.5% / second) can be set to determine whether the memory has reached a stable state. When the memory usage change rate is less than the threshold for a continuous period of time, it is considered that the program initialization is completed, and the current time is recorded as the time when the program initialization is completed.
[0061] For example, in an office software, as the user interface, document templates, plug-ins, and other resources are loaded, the memory usage gradually increases. When these resources are loaded, the memory usage tends to be stable, and the time when the program initialization is completed is determined.
[0062] The starting time of the target time range (i.e., t0) can be the time when the program initialization is completed, or any time later than the time when the program initialization is completed, which mainly depends on the specific detection requirements and scenarios. For example, if you want to detect memory leaks during the execution of a specific business logic, you can set the starting time of the target time range to the time when the business logic starts executing. This can more accurately focus on the memory allocation and release behavior in a specific time period, improving the relevance and efficiency of the detection.
[0063] To reduce misjudgment, a time threshold T threshold is set to limit the target time range to distinguish between memory leaks and normal memory usage. This threshold can be set according to the running characteristics of the program or empirical data, for example, set to 10 seconds.
[0064] To facilitate understanding, Figure 3A schematic diagram of the various moments mentioned in this embodiment is shown, including the target time t, the time when initialization is completed, the starting time t0 of the target time range, the end time t end , set the time threshold T threshold wait.
[0065] According to the target time t and the set time threshold T threshold , calculate the end time t of the target time range end =t−T threshold For example, if the target time t is 100 seconds for the program to run, set the time threshold T threshold is 10 seconds, then the end time of the target time range is t end is 90 seconds.
[0066] Through step 201, it can be specifically explained how to determine the starting time and target time of the target time range within the time range from program startup to exit, and determine the end time of the target time range according to the set time threshold, thereby providing a clear time interval for subsequent memory leak detection.
[0067] Step 202: Determine a potential memory leak set based on a memory allocation set and a memory release set within a target time range.
[0068] Specifically, in step 202, the memory allocation set A(t0) corresponding to the starting time of the target time range and the memory allocation set A(tT threshold ), the memory release set F(tT corresponding to the end time of the target time range threshold ) and the memory release set F(t) corresponding to the target time t; then the memory allocation set A(tT corresponding to the end time of the target time range threshold ), the memory release set F(tT corresponding to the end time of the target time range threshold ), the difference between the memory release set F(t) corresponding to the target time and the memory allocation set A(t0) corresponding to the starting time of the target time range is used as the potential leak memory set.
[0069] See also Figure 4 , Figure 4 The timing diagram of memory leak detection in this embodiment is shown. Assume that the starting time of the target time range is t0 = 10 seconds, the target time is t = 100 seconds, and the time threshold T is set. threshold = 10 seconds, so the target time range is [10,90] seconds. The following memory allocation and deallocation operations are recorded: Memory allocation record: At 5 seconds, allocate memory block a0, size 100 bytes, call stack main -> functionZ -> malloc.
[0070] At 15 seconds, allocate memory block a1, size 100 bytes, call stack main -> functionA -> malloc.
[0071] At 20 seconds, allocate memory block a2, size 200 bytes, call stack main -> functionB -> malloc.
[0072] At 25 seconds, allocate memory block a3, size 150 bytes, call stack main -> functionC -> malloc.
[0073] At 85 seconds, allocate memory block a4, size 50 bytes, call stack main -> functionD -> malloc.
[0074] At 90 seconds, allocate memory block a5, size 80 bytes, call stack main -> functionE -> malloc.
[0075] Memory release records: At 30 seconds, release memory block a1, size 100 bytes, call stack main -> functionF -> free.
[0076] At 35 seconds, release memory block a2, size 200 bytes, call stack main -> functionG -> free.
[0077] At 80 seconds, release memory block a3, size 150 bytes, call stack main -> functionH -> free.
[0078] At 95 seconds, release memory block a4, size 50 bytes, call stack main -> functionI -> free.
[0079] According to the memory allocation set A(t) and the memory release set F(t) within the target time range, calculate the unreleased memory set L(t-T threshold ): L(t-T threshold )=A(t-T threshold )−F(t-T threshold ) Example: In the target time range [10, 90]: Memory allocation set A(t-T threshold ) = {a1, a2, a3, a4, a5}; Memory release set F(t-T threshold ) = {a1, a2, a3}; Calculate the unreleased memory set: L(t-T threshold ) = A(t-T threshold ) - F(t-T threshold ) = {a1, a2, a3, a4, a5} - {a1, a2, a3} = {a4, a5}.
[0080] Then according to the unreleased memory set L(t-T threshold ) in the target time range and the memory release set F(t) corresponding to the target time and the memory allocation set A(t0) corresponding to the start time of the target time range, calculate the potential leakage memory set P leak (t-T threshold ).
[0081] During the running of the program, memory will be continuously allocated and released. In order to exclude the memory allocation close to the target time from the potential leakage memory after determining the target time, and reduce the false positives due to the recently allocated memory block, by setting a time threshold T threshold , the recently allocated memory block, i.e. the memory block satisfying t-T allocate <T threshold , is excluded from the leakage detection. Wherein, T threshold is the time threshold, and T allocate is the application time of the memory block.
[0082] In addition, most of the persistent memory (i.e. the memory that is released after the program exits after being applied) is applied in the program initialization phase. By setting the start time t0 of the target time range to be equal to or later than the time when the program initialization is completed, the memory allocation set corresponding to the start time t0 can be excluded.
[0083] At this time, the potential leakage memory set can be expressed as: P leak (t-T threshold ) = L(t-T threshold ) - F(t) - A(t0) It should be noted that the memory block that is not released in the target time range [t0, t-T threshold ] may be released at a later time (t-T thresholdt) are released. If these memory blocks are released in the subsequent time range, they should not be considered as potential leakage memory. In this embodiment, by subtracting F(t), it can be ensured that these memory blocks released in the subsequent time range are excluded from the potential leakage memory set, thereby improving the accuracy of detection.
[0084] As can be seen from the formula, the coverage of the potential leakage memory set in the embodiment of the application is not the same as that of the potential leakage memory set in the prior art: In the prior art, the potential leakage memory set P leak = the unreleased memory set L = A - F. Wherein, A is the memory allocation set, and F is the memory release set.
[0085] In the application, the potential leakage memory set P leak (t-T threshold ) is a subset of the unreleased memory set L(t-T threshold ), and they are not necessarily identical.
[0086] For example, the target time t = 100 seconds, the time threshold T threshold = 10 seconds, so the end time of the target time range t end = 90 seconds. At this time, the unreleased memory set L(90) = {a4, a5}, and the memory release set F(100) = {a1, a2, a3, a4}.
[0087] Calculate the potential leakage memory set: P leak (90) = L(90) - F(100) - A(10) = {a0, a1, a2, a3, a4, a5} - {a1, a2, a3} - {a1, a2, a3, a4} - {a0} = {a5}.
[0088] It should be noted here that although {a1, a2, a3} is subtracted twice, the memory release set will not be subtracted to a negative number, so for F(100), the essence is equivalent to subtracting the memory release set in the [t-T threshold , t] time period. Through the above calculation process, the potential leakage memory can be more accurately identified, and the misjudgment can be reduced.
[0089] Then, the potential leakage memory set is verified to determine the leakage memory.
[0090] After determining the potential leakage memory set, each memory block in the set needs to be verified one by one by manual analysis to determine whether it is a real leakage memory.
[0091] For each memory block in the potential leakage memory set, as an optional embodiment of the present application, first check the time stamp of its allocation to determine how long the memory block has existed in the program. Then find the specific location where the memory block is allocated through the call stack information, and then analyze the code logic to determine whether the memory block should have been released. If it is found that there is a logical error in the code, such as the memory should be released but is not released, or the release condition is not met, then the memory block is likely to be a leakage memory.
[0092] In addition, it can also be checked whether these potential leakage memory blocks are released within the next time range. If it is found that a certain memory block has a corresponding release operation within the subsequent time range, it can be determined that the memory block is not a real leakage memory, but is temporarily included in the potential leakage set due to the limitation of the detection time point. For such memory blocks, they can be excluded from the potential leakage memory set.
[0093] The memory leakage detection method provided by the embodiment of the present application determines the starting moment of the target time range and the target moment, and determines the ending moment of the target time range according to the target moment and the set time threshold. Then, according to the memory allocation set and the memory release set within the target time range, the potential leakage memory set is determined, and then the potential leakage memory set is verified to determine the leakage memory, so as to realize the determination of the potential leakage memory set based on the memory allocation and release behaviors within any time interval during the program running process, overcome the deficiency that the memory accumulation caused by the automatic release of the smart container in the traditional technology when the program exits cannot be detected, and improve the accuracy and reliability of the memory leakage detection.
[0094] Secondly, for the situation that in the traditional technology, when the program uses mechanisms such as cache and object pool, the program may continuously apply for memory, but does not release the memory in time after the data expires, causing memory accumulation, the method of the embodiment of the present application can detect at any time point during the program running process, and find abnormal situations of memory accumulation in time by analyzing the behavior mode of memory allocation and release. In this way, measures can be taken in time for optimization or repair before the performance is degraded or the system is unstable due to memory accumulation, so as to effectively prevent the occurrence of memory leakage problems.
[0095] Referring to Figure 5 In order to realize the acquisition of the multiple sets in step 202, the following steps are implemented: 501, by intercepting the memory allocation function and the memory release function of the system, recording each memory allocation operation and memory release operation, obtaining the information of each memory allocation operation and the information of the memory release operation, wherein the information of the memory allocation operation and the information of the memory release operation both include: call stack, operation type, time stamp, and operation memory block address and size.
[0096] By intercepting memory allocation functions (such as malloc, calloc, etc.) and memory release functions (such as free, etc.) in the system. Whenever the program calls these functions, trigger the pre-set recording logic. For example, the program calls the malloc function to allocate memory during running, at this time, intercept the call and trigger the recording logic.
[0097] And for each memory allocation and release operation, record the following detailed information: Call stack: record the hierarchical relationship of function calls to trace the code location of memory allocation.
[0098] Operation type: mark whether it is a memory allocation operation or a memory release operation.
[0099] Allocation ID: generate a unique identifier for each memory allocation, which is convenient for subsequent tracking and matching release operations.
[0100] Timestamp: record the specific time when the operation occurs, which is used to analyze the time sequence of memory usage.
[0101] Memory block address and size: record the address and size of the allocated or released memory block.
[0102] Suppose at 15 seconds, the program calls the malloc function to allocate a block of 100 bytes of memory, and the call stack is main -> functionA -> malloc. Among them, main is the entry function of the program, located at the top of the stack; functionA is the function called by main function; malloc is the memory allocation function called by functionA function, used to allocate memory. Then record the following information: Call stack: main -> functionA -> malloc Operation type: memory allocation; Allocation ID: 1; Timestamp: 15 seconds; Memory block size: 100 bytes; Address information: 0x00005555555552d0.
[0103] 502、According to the recorded timestamp, filter out the information of memory allocation operations and memory release operations from the starting time of the program to the end time of the target time range, and according to the filtered information of memory allocation operations and memory release operations, determine the memory allocation set and the memory release set corresponding to the end time of the target time range.
[0104] For example, the target time t = 100 seconds, and the time threshold T threshold= 10 seconds, the target time range is [10, 90] seconds.
[0105] Record memory allocation operations: allocate memory block a1 at 10 seconds, size 100 bytes; allocate memory block a2 at 20 seconds, size 200 bytes; allocate memory block a3 at 65 seconds, size 150 bytes; allocate memory block a4 at 85 seconds, size 50 bytes; Record memory release operations: release memory block a1 at 30 seconds, size 100 bytes; release memory block a2 at 35 seconds, size 200 bytes; release memory block a3 at 80 seconds, size 150 bytes; release memory block a4 at 95 seconds, size 50 bytes.
[0106] From the program startup time to the end time of the target time range, the following memory allocation and release operation information is filtered: Memory allocation operations: allocate memory block a1 at 10 seconds, size 100 bytes; allocate memory block a2 at 20 seconds, size 200 bytes; allocate memory block a3 at 65 seconds, size 150 bytes; allocate memory block a4 at 85 seconds, size 50 bytes; Memory release operations: release memory block a1 at 30 seconds, size 100 bytes; release memory block a2 at 35 seconds, size 200 bytes; release memory block a3 at 80 seconds, size 150 bytes.
[0107] Then the memory allocation set corresponding to the end time of the target time range (90 seconds) is {a1, a2, a3, a4}, and the memory release set is {a1, a2, a3}.
[0108] 503、According to the recorded timestamps, filter the memory release operation information from the program startup time to the target time, determine the memory release set corresponding to the target time; filter the memory allocation operation information from the program startup time to the start time of the target time range, determine the memory allocation set corresponding to the start time of the target time range.
[0109] In this step, the information of memory release operations from the program startup time to the target time is filtered out according to the recorded timestamps. For example, if the program startup time is 0 seconds and the target time is 100 seconds, the information of memory release operations with all timestamps between 0 seconds and 100 seconds is filtered out.
[0110] Then, based on the filtered memory release operation information, determine the memory release set F(t) corresponding to the target time. For example, between 0 and 100 seconds, the following memory release operation information is filtered: Release memory block a1 of size 100 bytes in 30 seconds; Free memory block a2 of 200 bytes in 35 seconds; Free memory block a3 of 150 bytes in 80 seconds; Memory block a4 of size 50 bytes is released at 95 seconds.
[0111] Then the memory release set F(t) corresponding to the target time (100 seconds) is {a1, a2, a3, a4}.
[0112] Next, filter the information about memory allocation operations from the program startup time to the starting time. Based on the recorded timestamps, filter out the information about memory allocation operations from the program startup time to the starting time. Assuming the program startup time is 0 seconds and the starting time is 10 seconds, filter out all memory allocation operations with timestamps between 0 and 10 seconds. Based on the filtered memory allocation operation information, determine that the memory allocation set A(t0) corresponding to the starting point of the target time range is {a1}.
[0113] Through the above steps, the information of memory allocation and release operations can be recorded and analyzed in detail, providing accurate data support for subsequent memory leak detection.
[0114] In order to facilitate understanding of the implementation of this embodiment, Figure 4 Taking the timing diagram shown as an example, the implementation process of the method of the embodiment of the present invention is further sorted out.
[0115] 1) Program startup: When the program starts running, the memory detection tool or module is also started to prepare for subsequent memory allocation and release monitoring.
[0116] 2) Intercepting Memory Allocation / Release Calls: At program startup, the memory inspection tool intercepts all memory allocation and release calls. By intercepting system memory allocation and release functions (such as malloc, calloc, and free), it ensures that all memory allocation and release operations are recorded. Each time memory is allocated or released, the tool records detailed information, including the call stack, operation type, allocation ID, timestamp, and memory block address and size.
[0117] 3) Program initialization: During the initialization process, the program allocates memory to store various initialization data and objects, and these memory allocation operations are recorded by the memory detection tool. There may also be some memory release operations, such as temporary buffers when loading resources, and these memory release operations are also recorded.
[0118] 4) Record the starting time (t0): When the program initialization is completed, the memory usage reaches a stable state, and the time when the program initialization is completed is marked as the starting time t0. Then determine the starting time t0 of the target time range, preferentially select the starting time of the target time range equal to or later than the time when the program initialization is completed.
[0119] 5) Determine the target time range: According to the program running situation or user demand, select a suitable target time t, and combine the set time threshold T threshold , determine the target time range as [t0, t- T threshold ]. This time range is the key interval for memory leak detection.
[0120] 6) Calculate the un-released memory set in the target time range (L(t- T threshold) ): In the target time range [t0, t- T threshold ], by comparing the memory allocation set and the memory release set, the un-released memory set L(t- T threshold ) in the time range is calculated. Specifically, L(t- T threshold ) = A(t- T threshold ) - F(t-T threshold ), where A(t- T threshold ) is the memory allocation set corresponding to the end time of the target time range, and F(t-T threshold ) is the memory release set corresponding to the end time of the target time range.
[0121] 7) Calculate the potential leak memory set (P leak (t- T threshold )): Further consider the time threshold T threshold and the starting time t0, calculate the potential leak memory set. The specific formula is: P leak (t- T threshold ) = L(t- T threshold ) - F(t) - A(t0). Where F(t) is the memory release set from program startup to target time t, and A(t0) is the memory allocation set corresponding to the starting time t0. The purpose of this is to exclude the allocated memory blocks that are close to the target time (satisfying t- Tallocate <T threshold ), the memory allocation set corresponding to the starting moment of the target time range, and the memory allocation set corresponding to the subsequent time range (t−T threshold ,t) The memory released in the release set can improve the accuracy of detection and reduce false positives.
[0122] 8) Verify leaked memory: Manually verify each memory block in the potential leaked memory set to determine whether it is a real leaked memory.
[0123] 9) Program Exit: After the program finishes running, you can compare the memory allocation and release status again to perform a final memory leak detection. If there are still unreleased memory blocks at this time, you can confirm that these memory blocks are leaked memory.
[0124] In the embodiment of the present invention, memory leak detection can be performed according to the above steps at any time during program execution without waiting for the program to exit, which overcomes the limitations of traditional memory leak detection methods.
[0125] This embodiment can detect memory leaks at any time during program execution, allowing timely detection of memory leaks without having to wait until the program exits. This is particularly important for long-running programs or those that require real-time monitoring of memory usage, effectively reducing the performance degradation and potential program crash risks associated with memory leaks.
[0126] Furthermore, the method of the present invention is highly sensitive to abnormal increases in memory usage patterns, particularly when dealing with continuous memory requests and releases, or when allocating progressively larger memory blocks. This capability enables the method of the present invention to accurately identify abnormalities in memory usage, effectively detecting memory leaks.
[0127] Furthermore, the method of the embodiment of the present invention allows the user to detect memory leaks within any time interval of program execution. This flexibility allows the user to perform detailed memory usage analysis for a specific time period, thereby more accurately locating and identifying memory leak problems.
[0128] The following describes a memory leak detection device provided by an embodiment of the present invention. The memory leak detection device described below and the memory leak detection method described above can refer to each other.
[0129] The embodiment of the present invention provides a memory leak detection device, see Figure 6 ,include: The target time range determination module 601 is configured to determine a starting time point of a target time range and a target time point in a time range from program starting to program exiting, and determine an ending time point of the target time range according to the target time point and a set time threshold. The potential leakage memory determination module 602 is configured to determine a potential leakage memory set according to a memory allocation set and a memory release set in the target time range, and verify whether the potential leakage memory set contains a leakage memory.
[0130] The memory leakage detection device provided by the application determines a starting time point of a target time range and a target time point, and determines an ending time point of the target time range according to the target time point and a set time threshold, and then determines a potential leakage memory set according to a memory allocation set and a memory release set in the target time range, and verifies the potential leakage memory set to determine a leakage memory, so that the potential leakage memory set is determined based on memory allocation and release behaviors in any time interval during program running, the deficiency that memory accumulation caused by automatic memory release of an intelligent container in the prior art when a program exits cannot be detected is overcome, and the accuracy and reliability of memory leakage detection are improved.
[0131] Figure 7 An example of an entity structure diagram of an electronic device is shown in Figure 7 As shown in the figure, the electronic device can include a processor 710, a communications interface 720, a memory 730 and a communications bus 740, wherein the processor 710, the communications interface 720 and the memory 730 complete mutual communication through the communications bus 740. The processor 710 can call logical instructions in the memory 730 to execute a memory leakage detection method, which includes determining a starting time point of a target time range and a target time point in a time range from program starting to program exiting, and determining an ending time point of the target time range according to the target time point and a set time threshold; determining a potential leakage memory set according to a memory allocation set and a memory release set in the target time range; and verifying whether the potential leakage memory set contains a leakage memory.
[0132] Furthermore, the logic instructions in the aforementioned memory 730 can be implemented as software functional units and, when sold or used as independent products, can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the portion that contributes to the prior art, or a portion of the technical solution, can be embodied in the form of a software product. This computer software product, stored in a storage medium, includes instructions for enabling a computer device (which can be a personal computer, server, or network device, etc.) to perform all or part of the steps of the methods described in various embodiments of the present invention. The aforementioned storage medium includes various media capable of storing program code, such as a USB flash drive, a mobile hard drive, a read-only memory (ROM), a random access memory (RAM), a magnetic disk, or an optical disk.
[0133] On the other hand, the present invention also provides a computer program product, which includes a computer program, which can be stored on a non-transitory computer-readable storage medium. When the computer program is executed by a processor, the computer can execute the memory leak detection method provided by the above methods, which includes: determining the starting time and target time of the target time range within the time range from program startup to program exit, and determining the end time of the target time range based on the target time and a set time threshold; determining a potential leak memory set based on a memory allocation set and a memory release set within the target time range; the potential leak memory set is used to verify whether it contains leaked memory.
[0134] On the other hand, the present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the memory leak detection method provided by the above-mentioned methods, the method comprising: determining the starting time and target time of a target time range within the time range from program startup to program exit, and determining the end time of the target time range based on the target time and a set time threshold; determining a potential leak memory set based on a memory allocation set and a memory release set within the target time range; the potential leak memory set is used to verify whether it contains leaked memory.
[0135] The device embodiments described above are merely illustrative, wherein the units described as separate components can or can not be physically separate, and the components displayed as units can or can not be physical units, i.e., can be located in one place, or can be distributed to multiple network units. Part or all of the modules can be selected to achieve the purposes of the embodiments according to actual needs. Those skilled in the art can understand and implement without creative labor.
[0136] Through the description of the above embodiments, those skilled in the art can clearly understand that the embodiments can be realized by means of software and the necessary general hardware platform, and of course can also be realized by hardware. Based on such understanding, the above technical solutions can be embodied in the form of a software product, which can be stored in a computer readable storage medium, such as a ROM / RAM, a magnetic disk, an optical disk, etc., and includes a number of instructions to make a computer device (which can be a personal computer, a server, or a network device, etc.) execute the methods described in each embodiment or some parts of the embodiments.
[0137] Finally, it should be noted that: the above embodiments are only used to illustrate the technical solutions of the present application, and not to limit them; although the present application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that: it can still modify the technical solutions recorded in the foregoing embodiments, or make equivalent replacement for part of the technical features; and these modifications or replacements do not make the essence of the corresponding technical solutions deviate from the spirit and scope of the technical solutions of the embodiments of the present application.
Claims
1. A method for detecting memory leaks, the method comprising: The method comprises the following steps: determining a starting time point of a target time range and a target time point within a time range from program starting to program exiting, and determining an ending time point of the target time range according to the target time point and a set time threshold; determining a potential leakage memory set according to a memory allocation set and a memory release set within the target time range, wherein the potential leakage memory set is used to verify whether it contains a leakage memory.
2. The method of claim 1, wherein, The starting time point of the target time range is equal to or later than a time point at which the program initialization is completed.
3. The method of claim 2, wherein, The time point at which the program initialization is completed is determined by monitoring memory usage of the program, and determining the time point at which the program initialization is completed when the memory usage reaches a stable state.
4. The method of claim 1, wherein, The ending time point of the target time range is determined by subtracting the set time threshold from the target time point.
5. The method of claim 1, wherein, The step of determining the potential leakage memory set according to the memory allocation set and the memory release set within the target time range comprises the following steps: determining a memory allocation set corresponding to the starting time point of the target time range, a memory allocation set corresponding to the ending time point of the target time range, a memory release set corresponding to the ending time point of the target time range, and a memory release set corresponding to the target time point; determining a difference between the memory allocation set corresponding to the ending time point of the target time range and the memory release set corresponding to the ending time point of the target time range, the memory release set corresponding to the target time point, and the memory allocation set corresponding to the starting time point of the target time range as the potential leakage memory set.
6. The method of claim 5, wherein, The step of determining the memory allocation set corresponding to the starting time point of the target time range, the memory allocation set corresponding to the ending time point of the target time range, the memory release set corresponding to the ending time point of the target time range, and the memory release set corresponding to the target time point comprises the following steps: intercepting memory allocation functions and memory release functions of the system to record each memory allocation operation and memory release operation, and obtaining information of each memory allocation operation and information of each memory release operation, wherein the information of each memory allocation operation and the information of each memory release operation both comprise a call stack, an operation type, a timestamp, and an operation memory block address and size; screening the information of the memory allocation operation and the information of the memory release operation from a program starting time point to the ending time point of the target time range according to the recorded timestamp, and determining the memory allocation set corresponding to the ending time point of the target time range and the memory release set according to the screened information of the memory allocation operation and the information of the memory release operation; screening the information of the memory release operation from the program starting time point to the target time point according to the recorded timestamp to determine the memory release set corresponding to the target time point, and screening the information of the memory allocation operation from the program starting time point to the starting time point of the target time range to determine the memory allocation set corresponding to the starting time point of the target time range.
7. A memory leak detection apparatus, comprising: The method comprises the following steps: a target time range determination module, configured to determine a start time point of a target time range and a target time point within a time range from program starting to program exiting, and determine an end time point of the target time range according to the target time point and a set time threshold; a potential leaking memory determination module, configured to determine a potential leaking memory set according to a memory allocation set and a memory release set within the target time range, the potential leaking memory set being used to verify whether the potential leaking memory set contains a leaking memory.
8. An electronic device comprising a memory, a processor, and a computer program stored on the memory and executable on the processor, characterized in that, The processor implements the memory leakage detection method according to any one of claims 1 to 6 when executing the program. 9.A non-transitory computer-readable storage medium having stored thereon a computer program, characterized in that, The computer program implements the memory leakage detection method according to any one of claims 1 to 6 when executed by the processor.
10. A computer program product comprising a computer program, characterized in that, The computer program implements the memory leakage detection method according to any one of claims 1 to 6 when executed by the processor.