Periodic memory leak detection method, apparatus and system

By generating a full pointer list and screening out the target pointer list with regularly increasing timestamps, combined with communication protocol analysis, the efficiency and accuracy issues of periodic memory leak detection are solved, ensuring the stable operation of the system.

CN119807012BActive Publication Date: 2025-10-21HANGZHOU TUYA INFORMATION TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202411906678.6
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-12-23
Publication Date
2025-10-21
Estimated Expiration
2044-12-23

AI Technical Summary

Technical Problem

Existing technologies have difficulty in efficiently and accurately detecting periodic memory leaks, especially when the memory leak location cannot be predicted in advance, resulting in system performance degradation or even crash.

Method used

By counting memory requests, a full pointer list is generated, grouped into sub-pointer lists, and a target pointer list with regularly increasing timestamps is screened out. These lists and memory data are transferred to a second electronic device for further analysis using a communication protocol to identify pointer values ​​of memory leaks.

Benefits of technology

The accuracy and efficiency of memory leak detection are improved, the operating pressure of the first electronic device is reduced, and the stability and performance of the system are guaranteed.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119807012B_ABST
    Figure CN119807012B_ABST
Patent Text Reader

Abstract

The application provides a detection method, device and system applied to periodic memory leakage, the method is applied to a first electronic device, a full pointer list is generated by counting memory application, containing a first feature and a second feature. Records with the same second feature are divided into sub-pointer lists, and then a target pointer list set with regularly increasing time stamps is determined. Through a communication protocol with a second electronic device, the target pointer list and memory data are transmitted to identify memory leakage pointer values. The first electronic device can effectively screen and transmit the target pointer list and memory data to the second electronic device, the method concentrates on analyzing the pointer list with regularly increasing time stamps, the regularly increasing time stamps are obvious signs of periodic memory leakage, the accuracy and efficiency of detection are improved, and specific analysis is performed on the second electronic device side, thereby reducing the operation pressure of the first electronic device and ensuring the operation efficiency of the first electronic device.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computers, and in particular to a memory leak detection method, device, and system. Background Art

[0002] Memory leak refers to a program requesting dynamic storage space from the system, but failing to release the requested memory space after use, causing the system to be unable to reclaim the memory in time and allocate it to other programs. Since computer system memory resources are limited, memory leaks can cause high system memory usage, resulting in a waste of resources, slowing down the system and even causing the system to crash. Based on the leak pattern, it can be divided into single leaks and periodic leaks. A single leak occurs when a program allocates memory but does not release it, but this only happens once. Since the amount of leaked memory is usually fixed, the long-term impact on the system is relatively small. Periodic leaks occur when a program leaks memory every time it performs an operation or scheduled task. Over time, this type of leak will cause the available memory to gradually decrease until the system memory is exhausted, which may eventually lead to degraded system performance or even a system crash.

[0003] Related technologies for monitoring periodic leaks require pre-setting the target object and collecting all pointer variables in the target area; this also requires pre-collecting stack information. However, in most cases, the location of the memory leak cannot be predicted, and pre-collecting stack information is cumbersome, inefficient, and requires high device performance. Summary of the Invention

[0004] Based on this, it is necessary to propose a method, device and system that can efficiently and accurately detect memory leaks.

[0005] In a first aspect, the present application provides a method for detecting periodic memory leaks, which is applied to a first electronic device. The method includes:

[0006] Counting memory requests and generating a full pointer list, wherein the full pointer list includes multiple fields, each field includes context features of each memory request, the context features include a first feature and a second feature, the first feature and the second feature each include at least one sub-feature, the first feature includes at least a timestamp, and the second feature includes at least a memory request length, a memory request calling function, and a thread name;

[0007] Group the fields with the same second characteristic in the full pointer list into a group to generate N groups of sub-pointer lists; wherein each group of sub-pointer lists includes at least one field;

[0008] Determine a target pointer list set; wherein the target pointer list set includes at least one target pointer list, and the target pointer list is a sub-pointer list whose timestamps increase regularly;

[0009] Based on the communication protocol with the second electronic device, at least part of the target pointer list and memory data in the target pointer list set are passed to the second electronic device on the first electronic device side, so that the second electronic device determines whether there is a pointer value of a memory leak based on the pointer value in the field of the target pointer list and the memory data.

[0010] In one embodiment, determining the target pointer list includes:

[0011] For any sub-pointer list, count the timestamps in the sub-pointer list to generate a timestamp list corresponding to the sub-pointer list, where the timestamp list corresponding to the sub-pointer list includes multiple timestamps;

[0012] For any timestamp list, determine whether the timestamps in the timestamp list increase regularly;

[0013] The sub-pointer lists corresponding to the timestamp lists whose timestamps increase regularly are used as target pointer lists, and a target pointer list set is determined based on each target pointer list.

[0014] In one embodiment, for any timestamp list, determining whether timestamps in the timestamp list increase regularly includes:

[0015] For any timestamp list, determine the time length corresponding to the timestamp list, where the time length is the difference between the maximum value corresponding to the latest timestamp in the timestamp list and the minimum value corresponding to the minimum timestamp;

[0016] Sort each timestamp list based on the length of time corresponding to each timestamp list;

[0017] Starting from the timestamp list with the largest time length, determine whether the timestamps in each timestamp list increase regularly.

[0018] In one embodiment, for any timestamp list, determining whether the timestamps in the timestamp list increase regularly includes:

[0019] For any timestamp list, determine the number of timestamps in each timestamp list;

[0020] Sort each timestamp list based on the number of timestamps corresponding to each timestamp list;

[0021] Starting from the timestamp list with the largest number of timestamps, determine whether the timestamps in each timestamp list increase regularly.

[0022] In one embodiment, the timestamp increases regularly, including at least one of the following:

[0023] Timestamps increase in an arithmetic progression;

[0024] The timestamp increases periodically.

[0025] The intervals between timestamps gradually increase;

[0026] The intervals between timestamps gradually decrease.

[0027] In one embodiment, fields with the same second characteristic in the full pointer list are grouped together to generate N groups of sub-pointer lists, including:

[0028] Using the second feature as the index, search the full pointer list;

[0029] The fields with the same second characteristic in the full pointer list are matched into a group to generate N groups of sub-pointer lists.

[0030] In one embodiment, the second electronic device determines whether there is a pointer value of a memory leak based on the pointer value in the field of the target pointer list and the memory data, including:

[0031] The second electronic device is enabled to determine whether there is a pointer value of a memory leak based on the pointer value in the field of the target pointer list and the memory data, and the second electronic device may be enabled to extract the pointer value in the field of the target pointer list based on any received target pointer list, and define the extracted pointer value as the target pointer value;

[0032] causing the second electronic device to search the memory data to determine, for any search object, whether there is an object in the memory data that matches the target pointer value;

[0033] The second electronic device is enabled to mark the target pointer value for which no matching object is found as a memory leak pointer value, and to generate a memory leak pointer list based on each memory leak pointer value.

[0034] In one embodiment, the method further comprises:

[0035] Determine the code segment list by finding the start and end addresses of the code segment in the link script;

[0036] At the current code calling position, the addresses that match the code segment list are polled and retrieved from the top of the stack to the bottom of the stack. The retrieved addresses form a callstack. At the current code calling position, the current thread name, memory request calling function and request length are obtained according to the context of the current code. Based on these three data, it is determined whether there is a second feature in the memory leak pointer list that matches the current thread name, memory request calling function and request length.

[0037] In a second aspect, the present application provides a detection device for periodic memory leaks. The device is applied to a first electronic device and includes:

[0038] A first generating module is configured to count memory requests and generate a full pointer list, wherein the full pointer list includes a plurality of fields, each field including a context feature of each memory request, the context feature including a first feature and a second feature, each of the first feature and the second feature including at least one sub-feature, the first feature including at least a timestamp, and the second feature including at least a memory request length, a memory request calling function, and a thread name;

[0039] The second generating module is configured to group fields having the same second characteristic in the full pointer list to generate N groups of sub-pointer lists; wherein each group of sub-pointer lists includes at least one field; the first generating module is further configured to determine a target pointer list set; wherein the target pointer list set includes at least one target pointer list, which is a sub-pointer list whose timestamps increase regularly;

[0040] A transmission module is used to transmit at least part of the target pointer list and memory data in the target pointer list set to the second electronic device based on the communication protocol between the first electronic device and the second electronic device, so that the second electronic device determines whether there is a pointer value of a memory leak based on the pointer value in the field of the target pointer list and the memory data.

[0041] In a third aspect, the present application provides a detection system for periodic memory leaks. The detection system includes a first electronic device and a second electronic device;

[0042] The first electronic device includes a memory and a processor, the memory stores a computer program, and when the processor executes the computer program, the steps of the method for detecting periodic memory leaks in the first aspect are implemented.

[0043] The above-mentioned method, device, and system for detecting periodic memory leaks are applied to a first electronic device. The method generates a full pointer list by counting memory requests. The full pointer list contains multiple fields, each of which contains contextual features. The contextual features include a first feature and a second feature. The first feature includes at least a timestamp, and the second feature includes at least the length of the memory request, the function calling the memory request, and the thread name. Fields with the same second feature are divided into sub-pointer lists, and a set of target pointer lists with regularly increasing timestamps is determined. The target pointer lists and memory data are transmitted to a second electronic device via a communication protocol to identify memory leak pointer values. The first electronic device can effectively screen and transmit the target pointer lists and memory data to the second electronic device. Because the method focuses on analyzing pointer lists with regularly increasing timestamps, which are a clear sign of periodic memory leaks, the accuracy and efficiency of detection are improved. The specific analysis is performed on the second electronic device, effectively reducing the operating pressure on the first electronic device and ensuring its operational efficiency. BRIEF DESCRIPTION OF THE DRAWINGS

[0044] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the following briefly introduces the drawings required for describing the embodiments.

[0045] Figure 1 FIG. 1 is a diagram of an application environment for detecting periodic memory leaks in one embodiment;

[0046] Figure 2 is a flow chart of a method for detecting periodic memory leaks in one embodiment;

[0047] Figure 3 A flowchart of determining a target pointer list set in one embodiment;

[0048] Figure 4 Schematic diagram of a full pointer list, N groups of sub-pointer lists, and a target pointer list set in one embodiment;

[0049] Figure 5 A flowchart of determining whether timestamps in a timestamp list increase regularly in one embodiment;

[0050] Figure 6 A flowchart of determining whether timestamps in a timestamp list increase regularly in another embodiment;

[0051] Figure 7 A flowchart of generating N groups of sub-pointer lists in one embodiment;

[0052] Figure 8 A flowchart of a pointer value for determining whether a memory leak exists in one embodiment;

[0053] Figure 9 A flowchart of finding a pointer request source that causes a memory leak in one embodiment;

[0054] Figure 10 is a module diagram of a detection device for periodic memory leaks in one embodiment;

[0055] Figure 11 A module diagram of a detection system for periodic memory leaks in one embodiment. DETAILED DESCRIPTION

[0056] To make the purpose, technical solutions, and advantages of the embodiments of the present application more clear, the technical solutions of the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present application, not all of the embodiments. Based on the embodiments of the present application, all other embodiments obtained by ordinary technicians in this field without making creative efforts are within the scope of protection of this application.

[0057] In the computer field, memory management has always been a crucial topic. Memory management includes stack memory management and heap memory management.

[0058] Stack memory is a computer memory area used to store function calls and local variables. It primarily manages contextual information during function calls, including local variables, function parameters, and return addresses. Stack memory is automatically allocated and released by the computer system and managed by the compiler.

[0059] Heap memory is primarily used to store data outside of stack memory, such as global variables and newly created objects. It is memory space dynamically allocated by electronic devices during process execution. Electronic device processes can flexibly allocate required space in heap memory by calling specialized memory allocation functions. When the allocated heap memory is no longer needed, the process can effectively return it to the system by calling the corresponding memory release function. Due to this characteristic, heap memory is prone to memory leaks.

[0060] The present invention provides a method for detecting periodic memory leaks, which can be applied to Figure 1In the application environment shown. The terminal 101 communicates with the server 102 through the network, and the terminal 101 can also communicate with the server 102 through a wired interface (such as a uart interface). The terminal 101 can be a first electronic device, and the server 102 is a second electronic device. It should be noted that the terminal 101 can also be a second electronic device, and the server 102 is a first electronic device. In this embodiment, the terminal 101 is the first electronic device and the server 102 is the second electronic device. Specifically, the terminal 101 counts the memory applied for and generates a full pointer list, wherein the full pointer list includes multiple fields, each field includes context features for each memory application, and the context features include a first feature and a second feature. The fields with the same second feature in the full pointer list are grouped together, and N groups of sub-pointer lists are generated and screened to obtain a set of sub-pointer lists whose timestamps show regular growth. Based on the communication protocol, the target sub-pointer list and the memory data of the entire terminal 101 (which can be composed of the bss segment of the current program, the global data segment and the static data segment, and the stack memory of all tasks) are all transmitted to the server 102. The server 102 intercepts the pointer value of the sub-pointer list and searches the memory data for the pointer value. If the pointer value cannot be retrieved, it is considered that there is a memory leak in the pointer value. For example, the pointer value of the intercepted sub-pointer list is 0x12345678, and it is necessary to search the memory data to see whether there is 0x12345678 stored in big-endian or little-endian format. If it exists, it indicates that the pointer is being referenced and it can be considered that there is no memory leak; if it does not exist, it means that the pointer has no references and it can be considered that the pointer is no longer usable, which can be determined as a memory leak. Among them, the terminal 101 can be, but is not limited to, various personal computers, laptops, smart phones, tablets, Internet of Things devices, etc. The server 102 can be implemented as an independent server or a server cluster consisting of multiple servers.

[0061] In one embodiment, Figure 2 As shown, a method for detecting periodic memory leakage applied to a first electronic device is provided, the method comprising the following steps:

[0062] Step 201: Count memory requests and generate a full pointer list, where the full pointer list includes multiple fields, each field includes context features of each memory request, the context features include a first feature and a second feature, each of the first feature and the second feature includes at least one sub-feature, the first feature includes at least a timestamp, and the second feature includes at least a memory request length, a memory request calling function, and a thread name;

[0063] Specifically, the first electronic device can be various personal computers, laptops, smart phones, tablet computers, Internet of Things devices, etc., which need to run for a long time and have high requirements for stability and performance. These devices are more susceptible to memory leaks. When the first electronic device needs to make a memory application, the first electronic device will record the fields of the current application. The field includes the context features of the memory application. The context features can be used to represent some information of the current memory application. It should be noted that the first feature in the context feature can include a timestamp, etc., and the second feature in the context feature can include the memory application length, the memory application calling function, and the thread name, etc.; in addition, the context feature also includes a pointer value. After obtaining all the required field information, create a table structure and store all pointer information in the table to generate a full pointer list.

[0064] Step 202: Group the fields with the same second characteristic in the full pointer list into one group to generate N groups of sub-pointer lists; wherein each group of sub-pointer lists includes at least one field;

[0065] Specifically, the second characteristic can be used as a grouping basis to group fields in the full pointer list that share the same second characteristic into a set of sub-pointer lists, generating a total of N sets of sub-pointer lists. Each generated set of sub-pointer lists must contain at least one field, ensuring that each set of sub-pointer lists contains sufficient information for subsequent analysis.

[0066] Step 203: Determine a target pointer list set; wherein the target pointer list set includes at least one target pointer list, and the target pointer list is a sub-pointer list whose timestamps increase regularly;

[0067] Specifically, a timestamp analysis is performed on each group of sub-pointer lists to identify which groups of sub-pointer lists have timestamps that increase regularly. It should be noted that it is specified in advance what time intervals or growth patterns are considered regular. For example, if the difference between timestamps is always a fixed value or follows a predictable pattern (such as an arithmetic progression), then the timestamps are considered to increase regularly. Traverse all sub-pointer lists and filter out sub-pointer lists whose timestamps meet the regular growth criteria. These sub-pointer lists will be marked as target pointer lists. All filtered target pointer lists are aggregated to form a target pointer list set. The target pointer list set includes at least one target pointer list.

[0068] Step 204: Based on the communication protocol with the second electronic device, at the first electronic device end, at least part of the target pointer list in the target pointer list set and the memory data are passed to the second electronic device, so that the second electronic device determines whether there is a pointer value of a memory leak based on the pointer value in the field of the target pointer list and the memory data.

[0069] Specifically, first determine the communication protocol to be used between the first electronic device and the second electronic device. This may be a network-based protocol (such as HTTP, FTP, SSH, etc.), a serial communication protocol (such as UART, RS-232, etc.), or any other applicable communication protocol. Select at least one target pointer list and related memory data from the target pointer list set and prepare for transmission. This data may include pointer values, timestamps, memory request lengths, etc. Establish a communication connection with the second electronic device on the first electronic device to ensure that the data can be successfully sent. After receiving the data, the second electronic device analyzes the pointer values ​​and memory data in the target pointer list to determine whether there are pointer values ​​that indicate a memory leak.

[0070] The above-mentioned method for detecting periodic memory leaks generates a full pointer list by counting memory requests, and groups the fields with the same second characteristic in the full pointer list into N groups to generate N groups of sub-pointer lists; uses the sub-pointer lists with regularly increasing timestamps as target pointer lists, and aggregates the target pointer lists to form a target pointer list set; based on a communication protocol with a second electronic device, transmits at least part of the target pointer lists and memory data in the target pointer list set to the second electronic device, so that the second electronic device determines whether there is a pointer value of a memory leak based on the pointer values ​​in the fields of the target pointer list and the memory data. The first electronic device can effectively screen and transmit the target pointer list and memory data to the second electronic device. Because the method focuses on analyzing the pointer lists with regularly increasing timestamps, which are a clear sign of memory leaks within a period, the accuracy and efficiency of detection are improved, and the specific analysis part is performed on the second electronic device, effectively reducing the operating pressure of the first electronic device and ensuring the operating efficiency of the first electronic device.

[0071] In one embodiment, Figure 3 As shown, determining the target pointer list set includes the following steps:

[0072] Step 301: For any sub-pointer list, count the timestamps in the sub-pointer list to generate a timestamp list corresponding to the sub-pointer list, where the timestamp list corresponding to the sub-pointer list includes multiple timestamps;

[0073] Specifically, for any group of sub-pointer lists, statistics are collected according to the timestamps in the sub-pointer lists to generate a timestamp list corresponding to the sub-pointer lists. It can be understood that each group of sub-pointer lists corresponds to a group of timestamp lists.

[0074] Step 302: for any timestamp list, determine whether the timestamps in the timestamp list increase regularly;

[0075] Specifically, for any list of timestamps, a criterion is set to determine whether the timestamps increase regularly, including whether the difference is consistently constant or follows a predictable pattern (e.g., an arithmetic or geometric progression). For each timestamp in the timestamp list, the interval between it and the previous one is calculated. The calculated interval is checked to see if it meets the criteria for regular growth. If the interval is consistent or follows a predictable pattern, the timestamps are considered to increase regularly.

[0076] For example, suppose we have a timestamp list containing the following timestamps: timestamplist=[2024−11−2010:00:00,2024−11−20 10:05:00,2024−11−20 10:10:00,2024−11−20 10:15:00].

[0077] Calculating time intervals: First interval: 2024-11-20 10:05:00-2024-11-20 10:00:00 = 5 minutes. Second interval: 2024-11-20 10:10:00-2024-11-20 10:05:00 = 5 minutes. Third interval: 2024-11-20 10:15:00-2024-11-20 10:10:00 = 5 minutes. All time intervals are 5 minutes, which is a constant interval, indicating that the timestamps increase regularly.

[0078] Another example is: t1, t2, t3, t1+2, t2+2, t3+2, t1+4, t2+4, t3+4. Among them, t1, t2, t3 are irregular, but t1, t2, t3, t1+2, t2+2, t3+2, t1+4, t2+4, t3+4 will cycle periodically, and the difference is 2.

[0079] Step 303: taking the sub-pointer lists corresponding to the timestamp lists with regularly increasing timestamps as target pointer lists, and determining a target pointer list set based on each target pointer list.

[0080] Specifically, at the first electronic device end, the sub-pointer list corresponding to the timestamp list with regularly increasing timestamps is used as the target pointer list, and outputted as For example, if there are N groups of timestamp lists corresponding to N groups of sub-pointer lists and 10 of them meet the condition of regular growth, then these 10 groups of sub-pointer lists are marked as having possible memory leaks. This corresponds to the target pointer set of these 10 groups of sub-pointer lists.

[0081] In this embodiment, the timestamps within each sub-pointer list are counted to generate a corresponding timestamp list. These timestamp lists are then analyzed to determine whether the timestamps show a regular increase. Sub-pointer lists with regularly increasing timestamps are then used as target pointer lists, and a target pointer list set is determined based on these target pointer lists. This method improves the targetedness and accuracy of detection, specifically by accurately identifying sub-pointer lists with regularly increasing timestamps from a large number of memory request records, effectively screening out potential periodic memory leaks.

[0082] It should be noted that if Figure 4 As shown in the figure, the full pointer list contains multiple fields, each of which contains contextual features for each memory request. The contextual features include a first feature and a second feature. The first feature includes a timestamp; the second feature includes the request length, the function calling the memory request, and the current thread name. Each field in the figure has its own corresponding first and second features. In this example, the full pointer list contains 15 fields.

[0083] The sub-pointer lists are generated by grouping fields in the full pointer list that share the same second characteristic. This means that the sub-pointer lists are indexed by "length, memory allocation calling function, and current thread name," generating a total of N groups of sub-pointer lists. In this embodiment, five groups of sub-pointer lists are generated: the first group contains fields 1, 3, and 5; the second group contains fields 2, 4, and 6; the third group contains fields 7, 9, and 11; the fourth group contains fields 8, 10, and 12; and the fifth group contains fields 13, 14, and 15.

[0084] The target pointer list is generated by generating a timestamp list corresponding to each sub-pointer list based on the timestamp in each group of sub-pointer lists. The sub-pointer lists corresponding to the timestamp lists whose timestamps increase regularly are used as the target pointer lists, and the target pointer list set is determined based on each target pointer list. In this embodiment, if the timestamps in the sub-pointer lists corresponding to the first, third, and fourth groups of timestamp lists increase regularly, the first, third, and fourth groups of sub-pointer lists are used as the target pointer lists, and the target pointer list set is determined based on these three groups of target pointer lists.

[0085] In one embodiment, Figure 5 As shown, for any timestamp list, determining whether the timestamps in the timestamp list increase regularly includes the following steps:

[0086] Step 501: for any timestamp list, determine the time length corresponding to each timestamp list, wherein the time length is the difference between the maximum value corresponding to the latest timestamp in the timestamp list and the minimum value corresponding to the minimum timestamp;

[0087] Specifically, a timestamp list is randomly selected from the timestamp lists corresponding to each group of sub-pointer lists. In the selected timestamp list, the earliest and latest timestamps are found, where the latest timestamp corresponds to the maximum value, and the smallest timestamp corresponds to the minimum value. The difference between the maximum value corresponding to the latest timestamp and the minimum value corresponding to the smallest timestamp is calculated to obtain the time length. For each timestamp list, the time length corresponding to each timestamp list is calculated.

[0088] Assume that one of the timestamp lists contains the following timestamps, timestamp list = [2024−11−20 10:00:00, 2024−11−20 10:05:00, 2024−11−20 10:10:00, 2024−11−20 10:15:00].

[0089] Earliest timestamp: 2024-11-20 10:00:00. Latest timestamp: 2024-11-20 10:15:00. Time duration: The difference between the earliest and latest timestamps is 15 minutes.

[0090] Step 502: sorting the timestamp lists based on the length of time corresponding to each timestamp list;

[0091] Specifically, for each timestamp list, the time length corresponding to each timestamp list is calculated, that is, the difference between the latest timestamp and the earliest timestamp in each timestamp list is calculated. Each calculated timestamp list and its corresponding time length are sorted by size, which can be sorted from largest to smallest time length.

[0092] Step 503: Starting from the timestamp list with the largest time length, determine in turn whether the timestamps in each timestamp list increase regularly.

[0093] Specifically, according to step 502, a timestamp list is obtained, sorted by time length from largest to smallest. Starting with the timestamp list with the largest time length, the timestamps therein are checked to see if they show a regular increase. This can be accomplished by calculating the intervals between consecutive timestamps and checking whether these intervals are consistent or follow a certain pattern. After completing the check for the timestamp list with the largest time length, the same check is performed on the timestamp list with the next longest time length in the sort order until all timestamp lists have been checked. The check results for each timestamp list are recorded to determine which timestamp lists show a regular increase in timestamps.

[0094] In this embodiment, the time length is determined by calculating the difference between the earliest and latest timestamps in each timestamp list, and all timestamp lists are sorted based on these time lengths. Then, starting from the list with the longest time length, the timestamps in each list are checked in turn to see if they show a regular growth, thereby effectively identifying periodic memory leaks.

[0095] In one embodiment, Figure 6 As shown, for any timestamp list, determining whether the timestamps in the timestamp list increase regularly includes:

[0096] Step 501: for any timestamp list, determine the number of timestamps in each timestamp list;

[0097] Specifically, a timestamp list is randomly selected from the timestamp lists corresponding to each group of sub-pointer lists, and the number of timestamps contained in the selected timestamp list is counted to calculate the number of timestamps contained in the timestamp list. For each timestamp list, the number of timestamps in each timestamp list is counted.

[0098] Assume a timestamp list containing the following timestamps, timestamp list = [2024−11−20 10:00:00, 2024−11−20 10:05:00, 2024−11−20 10:10:00, 2024−11−20 10:15:00]. This timestamp list contains 4 timestamps.

[0099] Step 602: sorting each time stamp list based on the number of timestamps corresponding to each time stamp list;

[0100] Specifically, for each timestamp list, the number of timestamps in each timestamp list is counted, and the timestamps in each timestamp list obtained by counting are mathematically sorted by size, which can be sorted from largest to smallest in terms of the number of timestamps.

[0101] Step 603: Starting from the timestamp list with the largest number of timestamps, determine in turn whether the timestamps in each timestamp list increase regularly.

[0102] Specifically, according to step 602, a timestamp list is obtained, sorted by the number of timestamps from largest to smallest. Starting with the timestamp list with the largest number of timestamps, the timestamps therein are checked to see if they show a regular increase. This can be accomplished by calculating the intervals between consecutive timestamps and checking whether these intervals are consistent or follow a certain pattern. After completing the check for the timestamp list with the largest number of timestamps, the same check is performed on the timestamp list with the next largest number of timestamps in the sorted order until all timestamp lists have been checked. The check results for each timestamp list are recorded to determine which timestamp lists show a regular increase in timestamps.

[0103] In this embodiment, by counting the number of timestamps in each timestamp list and sorting the timestamp lists based on the number of timestamps, starting with the list with the largest number of timestamps, the timestamps in each timestamp list are analyzed in turn to see whether they show a regular growth, so as to identify periodic memory leaks, thereby improving the efficiency and accuracy of detecting periodic memory leaks.

[0104] In one embodiment, to determine whether the timestamps in the timestamp list show regular growth, one of the following dimensions may be considered: the timestamps grow in an arithmetic progression, the timestamps grow in a periodic cycle, the intervals between timestamps gradually increase, or the intervals between timestamps gradually decrease.

[0105] Specifically, the timestamps grow in an arithmetic manner, and check whether the difference between consecutive timestamps is constant. If the time difference between any two consecutive timestamps is equal, then the timestamps grow in an arithmetic manner; the timestamps grow in a periodic cycle, and analyze whether there is a fixed cycle for the timestamps so that the timestamps grow in this cycle, even if the amount of each increase may be different; the interval between timestamps gradually increases, and observe whether the interval between timestamps has an increasing trend, that is, as time goes by, the time interval between each memory request gradually becomes longer; the interval between timestamps gradually decreases, and check whether the interval between timestamps has a decreasing trend, that is, as time goes by, the time interval between each memory request gradually becomes shorter.

[0106] Assume a timestamp list, timestamplist = [2024−11−20 10:00:00,2024−11−20 10:03:00,2024−11−20 10:06:00,2024−11−20 10:12:00].

[0107] Timestamps increase in an arithmetic progression: the calculation interval is 3 minutes, 3 minutes, and 6 minutes. Timestamps do not increase in an arithmetic progression.

[0108] Timestamps increase periodically: Observe whether there is a repeating pattern, such as a long gap after every 3 requests. In this example, it is not easy to identify a clear periodic pattern.

[0109] The intervals between timestamps gradually increase: Checks whether the intervals increase over time: 3 minutes, 3 minutes, 6 minutes. The intervals gradually increase, meeting the condition for a gradually increasing interval.

[0110] The interval between timestamps gradually decreases: Since the time interval is gradually increasing, the condition of gradually decreasing interval is not met.

[0111] In this embodiment, regular growth is identified by examining the timestamp list. Specifically, this includes checking whether the timestamps increase in an arithmetic progression, whether they exhibit periodic cyclic growth, or whether the intervals gradually increase or decrease. Meeting at least one of these conditions is considered regular growth. This method effectively identifies periodic memory leaks by determining whether the timestamp list exhibits regular growth based on multiple conditions. Meeting at least one of these conditions is sufficient for regular growth to be satisfied.

[0112] In one embodiment, Figure 7 As shown, the fields with the same second characteristic in the full pointer list are grouped together to generate N groups of sub-pointer lists, including the following steps:

[0113] Step 701: Using the second feature as an index, search the full pointer list;

[0114] Specifically, in the full pointer list, a search is performed based on the second feature (memory request length, memory request calling function, and thread name) as an index to find a field with the same second feature.

[0115] Step 702: Match the fields with the same second characteristic in the full pointer list into a group to generate N groups of sub-pointer lists.

[0116] Specifically, after the first electronic device obtains the full pointer list, it uses the request length, the calling function of the memory request, and the thread name as indexes to find fields with the same characteristics. Fields with the same second characteristic in the search results are grouped together to form a sub-pointer list. A total of N sub-pointer lists are generated.

[0117] In this embodiment, N groups of sub-pointer lists are generated by grouping fields with the same second characteristic in the full pointer list. Specifically, the full pointer list is searched using the second characteristic as an index, where the second characteristic includes the memory request length, the memory request calling function, and the thread name; fields with the same second characteristic are matched into a group to form a group of sub-pointer lists, and a total of N groups of sub-pointer lists are generated. This method can centrally analyze fields with the same second characteristic, thereby improving the accuracy and efficiency of detecting periodic memory leaks; grouping fields with the same second characteristic makes it easier to identify potential memory leak patterns.

[0118] In one embodiment, Figure 8 As shown, the second electronic device determines whether there is a pointer value of a memory leak according to the pointer value in the field of the target pointer list and the memory data, including the following steps:

[0119] Step 801: enabling the second electronic device to extract a pointer value in a field of any received target pointer list, and define the extracted pointer value as a target pointer value;

[0120] Specifically, based on the communication protocol between the first electronic device and the second electronic device, the first electronic device end mark The target pointer lists and the overall memory data are transmitted to the second electronic device. In the second electronic device, the target pointer lists contain filtered pointer values ​​that may be related to memory leaks according to each target pointer category received.

[0121] Step 802: The second electronic device searches the memory data to determine, for any target pointer value, whether there is an object matching the target pointer value in the memory data.

[0122] Specifically, the second electronic device has received the target pointer list and related memory data from the first electronic device. For each target pointer value in the target pointer list, the second electronic device searches the memory data to find out whether there is an object matching these target pointer values.

[0123] Step 803: The second electronic device is enabled to mark the target pointer value for which no matching object is found as a memory leak pointer value, and generate a memory leak pointer list based on each memory leak pointer value.

[0124] Specifically, after the second electronic device completes the memory data search, it marks the target pointer values ​​that do not find a matching object in the memory data as memory leak pointer values. Based on all the pointer values ​​marked as memory leaks, the second electronic device generates a memory leak pointer list, which includes all pointer values ​​suspected of memory leaks.

[0125] In this embodiment, the second electronic device extracts pointer values ​​from the received target pointer list as target pointer values, and searches the memory data for these target pointer values ​​to determine whether a matching object exists. If the search results indicate that a target pointer value does not have a matching object in the memory data, it is marked as a memory leak pointer value, and a memory leak pointer list is generated based on these memory leak pointer values. This method can accurately identify pointer values ​​that may cause memory leaks, thereby improving the accuracy and efficiency of memory leak detection. By generating a memory leak pointer list, memory leak problems can be quickly located and resolved, enhancing the stability and reliability of the system.

[0126] In one embodiment, Figure 9 As shown, the following steps are also included:

[0127] Step 901: Determine a code segment list by finding the start and end addresses of the code segments in the linker script;

[0128] Specifically, after determining that the target pointer list has a memory leak, we further locate the source of the pointer request that caused the memory leak. We define a code segment list by analyzing the linker script to determine the identifier of the code segment and the start and end addresses of the code segment. This code segment list records the locations of all code segments in the program.

[0129] Step 902: At the current code calling position, at the current code calling position, poll and retrieve the addresses that match the code segment list from the top of the stack to the bottom of the stack in sequence, and the retrieved addresses form a callstack. At the current code calling position, obtain the current thread name, memory request calling function and request length according to the context of the current code. Based on these three data, determine whether there is a second feature in the memory leak pointer list that matches the current thread name, memory request calling function and request length.

[0130] Specifically, the current code call location refers to the point in program execution where a memory allocation occurs, typically the point where a memory allocation function (such as malloc or calloc) is called. A program's call stack is a last-in, first-out (LIFO) data structure. The top of the stack is the last function called, the currently executing function; the bottom of the stack is the oldest function called, the starting point of program execution (usually the main function). Polling begins at the top of the stack, checking each function pointer individually to see if they fall within the code segment's bounds. If so, they are added to the call stack list. If the current thread name, memory allocation calling function, and allocation length, obtained based on the current code context, match the second feature of the memory leak pointer list (i.e., the memory leak pointer list contains a matching second feature with the current thread name, memory allocation calling function, and allocation length, where the current thread name, memory allocation calling function, and allocation length are obtained based on the current code context), the call stack list is output. Using the call stack list helps identify the source of the memory leak.

[0131] In this embodiment, a code segment list is determined by finding the start and end addresses of the code segments in the linker script. At the current code call location, the second features in the memory leak pointer list are sequentially polled from the top of the stack to the bottom of the stack to determine whether the memory leak pointer list contains a second feature that matches the current thread name, memory request calling function, and request length. This method can accurately identify which second features in the memory leak pointer list point to the actual code segments of the program, thereby eliminating those second features that point to code segments as potential memory leak candidates, helping to improve the accuracy of memory leak detection and reduce false positives.

[0132] Based on the same inventive concept, an embodiment of the present application further provides a device for implementing the aforementioned periodic memory leak detection method. The solution provided by the device is similar to the solution described in the aforementioned method. Therefore, the specific limitations of one or more embodiments of the periodic memory leak detection device provided below can be found in the limitations of the periodic memory leak detection method described above and will not be repeated here.

[0133] In one embodiment, Figure 10 As shown, a detection device for periodic memory leak is provided, which is applied to a first electronic device. The detection device includes: a first generation module 1001, a second generation module 1002 and a transmission module 1003, wherein:

[0134] The first generation module 1001 is used to count memory applications and generate a full pointer list, wherein the full pointer list includes multiple fields, each field includes context features of each memory application, the context features include a first feature and a second feature, the first feature and the second feature each include at least one sub-feature, the first feature includes at least a timestamp, and the second feature includes at least a memory application length, a memory application calling function, and a thread name.

[0135] The second generation module 1002 is used to group the fields with the same second characteristic in the full pointer list to generate N groups of sub-pointer lists; wherein, any group of sub-pointer lists includes at least one field; the second generation module is also used to determine a target pointer list set; wherein, the target pointer list set includes at least one target pointer list, and the target pointer list is a sub-pointer list whose timestamps increase regularly.

[0136] The transmission module 1003 is used to transmit at least part of the target pointer list in the target pointer list set and the memory data to the second electronic device based on the communication protocol with the second electronic device, so that the second electronic device can determine whether there is a pointer value of a memory leak based on the pointer value in the field of the target pointer list and the memory data.

[0137] In one embodiment, the second generating module 1002 determines a target pointer list set, and is specifically configured to: for any sub-pointer list, count the timestamps in the sub-pointer list, and generate a timestamp list corresponding to the sub-pointer list, wherein the timestamp list corresponding to the sub-pointer list includes multiple timestamps;

[0138] For any timestamp list, determine whether the timestamps in the timestamp list increase regularly;

[0139] The sub-pointer lists corresponding to the timestamp lists whose timestamps increase regularly are used as target pointer lists, and a target pointer list set is determined based on each target pointer list.

[0140] In one embodiment, the second generation module 1002 determines, for any timestamp list, whether the timestamps in the timestamp list show regular growth, specifically for: determining, for any timestamp list, the time length corresponding to the timestamp list, wherein the time length is the difference between the maximum value corresponding to the latest timestamp in the timestamp list and the minimum value corresponding to the minimum timestamp in the timestamp list; sorting each timestamp list based on the size of the time length corresponding to each timestamp list; starting from the timestamp list with the largest time length, determining in turn whether the timestamps in each timestamp list show regular growth.

[0141] In one embodiment, the second generation module 1002 determines, for any timestamp list, whether the timestamps in the timestamp list show a regular increase, specifically for: determining, for any timestamp list, the number of timestamps in the timestamp list; sorting each timestamp list based on the number of timestamps corresponding to each timestamp list; starting from the timestamp list with the largest number of timestamps, determining in turn whether the timestamps in each timestamp list show a regular increase.

[0142] In one embodiment, the timestamps increase regularly, including at least one of the following: the timestamps increase in an arithmetic progression; the timestamps increase in a periodic cycle; the intervals between the timestamps gradually increase; the intervals between the timestamps gradually decrease.

[0143] In one embodiment, the second generation module 1002 groups the fields with the same second feature in the full pointer list into a group to generate N groups of sub-pointer lists, which are specifically used to: search the full pointer list with the second feature as the index; match the fields with the same second feature in the full pointer list into a group to generate N groups of sub-pointer lists.

[0144] In one embodiment, the transmission module 1003 enables the second electronic device to determine whether there is a pointer value of a memory leak based on the pointer value in the field of the target pointer list and the memory data, and is specifically used to: enable the second electronic device to extract the pointer value in the field of the target pointer list based on any received target pointer list, and define the extracted pointer value as the target pointer value; enable the second electronic device to search in the memory data to determine whether there is an object matching the target pointer value in the memory data for any target pointer value; enable the second electronic device to mark the target pointer value retrieved without a matching object as a memory leak pointer value, and generate a memory leak pointer list based on each memory leak pointer value.

[0145] In one embodiment, the transmission module 1003 determines the code segment list by finding the start and end addresses of the code segment in the link script; at the current code call position, the address that matches the code segment list is polled and retrieved from the top of the stack to the bottom of the stack in sequence, and the retrieved addresses form a callstack. At the current code call position, the current thread name, memory request calling function and request length are obtained according to the context of the current code, and based on the three data, it is determined whether there is a second feature in the memory leak pointer list that matches the current thread name, memory request calling function and request length.

[0146] Each module used in the periodic memory leak detection device can be implemented in whole or in part through software, hardware, or a combination thereof. Each module can be embedded in or independent of a processor in a computer device in hardware form, or can be stored in a memory in the computer device in software form, so that the processor can call and execute the corresponding operations of each module.

[0147] Based on the same inventive concept, the present application also provides a system for implementing the aforementioned periodic memory leak detection method. The solution provided by this system is similar to the solution described in the aforementioned method. Therefore, the specific limitations in the following embodiment of the periodic memory leak detection system can be referred to as the limitations of the periodic memory leak detection method described above, and will not be repeated here.

[0148] In one embodiment, a detection system for periodic memory leaks is provided, and its specific implementation structure is as follows: Figure 11 As shown, it may specifically include a first electronic device and a second electronic device;

[0149] The first electronic device includes a memory and a processor, the memory stores a computer program, and the processor implements the steps of a method for detecting periodic memory leaks when executing the computer program.

[0150] Furthermore, the first electronic device also includes a communication interface, a display screen, and an input device connected via a system bus. The processor of the first electronic device is configured to provide computing and control capabilities. The memory of the first electronic device includes a non-volatile storage medium and internal memory. The non-volatile storage medium stores an operating system and computer programs. The internal memory provides an environment for the operation of the operating system and computer programs in the non-volatile storage medium. The communication interface of the electronic device is configured to communicate with an external terminal via wired or wireless communication. Wireless communication can be achieved via Wi-Fi, a mobile cellular network, NFC (near-field communication), or other technologies. When executed by the processor, the computer program implements a method for detecting periodic memory leaks. The display screen of the electronic device can be a liquid crystal display or an electronic ink display. The input device of the electronic device can be a touchscreen layer covering the display screen, buttons, a trackball, or a touchpad provided on the electronic device housing, or an external keyboard, touchpad, or mouse.

[0151] The technical features of the above embodiments can be combined arbitrarily. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.

[0152] The above embodiments merely illustrate several implementation methods of the present application. While the descriptions are relatively specific and detailed, they should not be construed as limiting the scope of the present invention. It should be noted that a person skilled in the art may make various modifications and improvements without departing from the spirit of the present invention, all of which fall within the scope of protection of the present application. Therefore, the scope of protection of the present application shall be determined by the appended claims.

Claims

1. A detection method for periodic memory leaks, characterized in that: Applied to a first electronic device, the method includes: Counting memory requests and generating a full pointer list, wherein the full pointer list includes multiple fields, each field includes context features of each memory request, the context features include a first feature and a second feature, the first feature and the second feature each include at least one sub-feature, the first feature includes at least a timestamp, and the second feature includes at least a memory request length, a memory request calling function, and a thread name; Grouping the fields with the same second characteristic in the full pointer list into one group to generate N groups of sub-pointer lists; wherein each group of sub-pointer lists includes at least one field; Determine a target pointer list set; wherein the target pointer list set includes at least one target pointer list, and the target pointer list is a sub-pointer list whose timestamp increases regularly; Based on the communication protocol with the second electronic device, at least part of the target pointer list in the target pointer list set and the memory data are passed to the second electronic device, so that the second electronic device determines whether there is a pointer value of a memory leak based on the pointer value in the field of the target pointer list and the memory data.

2. The detection method for periodic memory leaks according to claim 1, wherein: Determine the target pointer list set including: For any of the sub-pointer lists, counting the timestamps in the sub-pointer list to generate a timestamp list corresponding to the sub-pointer list, wherein the timestamp list corresponding to the sub-pointer list includes multiple timestamps; For any of the timestamp lists, determining whether the timestamps in the timestamp list increase regularly; The sub-pointer lists corresponding to the timestamp list with regularly increasing timestamps are used as target pointer lists, and the target pointer list set is determined based on each of the target pointer lists.

3. The detection method for periodic memory leak according to claim 2, characterized in that: For any of the timestamp lists, determining whether the timestamps in the timestamp list increase regularly includes: For any of the timestamp lists, determine a time length corresponding to the timestamp list, wherein the time length is a difference between a maximum value corresponding to the latest timestamp in the timestamp list and a minimum value corresponding to the minimum timestamp; sorting the timestamp lists based on the length of time corresponding to each timestamp list; Starting from the timestamp list with the largest time length, it is determined in sequence whether the timestamps in each timestamp list increase regularly.

4. The method for detecting periodic memory leaks according to claim 2, wherein: For any of the timestamp lists, determining whether the timestamps in the timestamp list increase regularly includes: For any of the timestamp lists, determining the number of timestamps in the timestamp list; sorting the timestamp lists based on the number of timestamps corresponding to the timestamp lists; Starting from the timestamp list with the largest number of timestamps, it is determined in sequence whether the timestamps in each of the timestamp lists increase regularly.

5. The method for detecting periodic memory leaks according to any one of claims 1 to 4, wherein: The timestamp increases regularly, including at least one of the following: Timestamps increase in an arithmetic progression; The timestamp increases periodically. The intervals between timestamps gradually increase; The intervals between timestamps gradually decrease.

6. The method for detecting periodic memory leaks according to claim 1, wherein: The fields with the same second characteristic in the full pointer list are grouped together to generate N groups of sub-pointer lists, including: Using the second feature as an index, searching the full pointer list; Fields with the same second characteristic in the full pointer list are matched into a group to generate N groups of sub-pointer lists.

7. The method for detecting periodic memory leaks according to claim 1, wherein: The method enables the second electronic device to determine whether there is a pointer value of a memory leak according to the pointer value in the field of the target pointer list and the memory data, including: enabling the second electronic device to extract a pointer value in a field of any received target pointer list according to the target pointer list, and define the extracted pointer value as a target pointer value; causing the second electronic device to search the memory data to determine, for any target pointer value, whether there is an object in the memory data that matches the target pointer value; The second electronic device is enabled to mark the target pointer value for which no matching object is found as a memory leak pointer value, and to generate a memory leak pointer list based on each memory leak pointer value.

8. The method for detecting periodic memory leaks according to claim 7, wherein: The method further comprises: Determine the code segment list by finding the start and end addresses of the code segment in the link script; At the current code calling position, addresses that match the code segment list are polled and retrieved in sequence from the top of the stack to the bottom of the stack, and the retrieved addresses form a callstack. At the current code calling position, the current thread name, memory request calling function and request length are obtained according to the context of the current code. Based on the three data, it is determined whether there is a second feature in the memory leak pointer list that matches the current thread name, memory request calling function and request length.

9. A detection device for periodic memory leaks, characterized in that: Applied to a first electronic device, the device includes: a first generating module, configured to count memory requests and generate a full pointer list, wherein the full pointer list includes a plurality of fields, each field including a context feature of each memory request, the context feature including a first feature and a second feature, each of the first feature and the second feature including at least one sub-feature, the first feature including at least a timestamp, and the second feature including at least a memory request length, a memory request calling function, and a thread name; a second generating module, configured to group the fields having the same second characteristic in the full pointer list into a group to generate N groups of sub-pointer lists; wherein each group of sub-pointer lists includes at least one field; and the second generating module is further configured to determine a set of target pointer lists; wherein the set of target pointer lists includes at least one target pointer list, wherein the target pointer list is a sub-pointer list whose timestamps increase regularly. A transmission module is used to transmit at least part of the target pointer list in the target pointer list set and the memory data to the second electronic device based on a communication protocol with the second electronic device, so that the second electronic device determines whether there is a pointer value of a memory leak based on the pointer value in the field of the target pointer list and the memory data.

10. A detection system for periodic memory leaks, characterized in that: The detection system includes a first electronic device and a second electronic device; The first electronic device includes a memory and a processor, the memory stores a computer program, and the processor implements the steps of the method for detecting periodic memory leaks according to any one of claims 1 to 8 when executing the computer program.

Citation Information

Patent Citations

  • Memory leak detection method and device, equipment and storage medium

    CN115952088A

  • Memory monitoring method and system

    CN116204378A