Memory leakage detection method, device, equipment, storage medium and program product
By acquiring and storing memory snapshots in Unity games and analyzing them based on function call trees, the low efficiency of IL2CPP memory leak detection is solved, achieving efficient memory leak detection and avoiding memory crashes and the need for manual analysis.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- SHENZHEN TENCENT NETWORK INFORMATION TECH CO LTD
- Filing Date
- 2025-01-08
- Publication Date
- 2026-07-10
AI Technical Summary
In existing technologies, IL2CPP memory leak detection is inefficient. The UnityProfiler tool consumes a lot of memory when capturing memory snapshots, which can easily lead to memory crashes. It cannot effectively cover complex scenes and requires a lot of manual analysis.
By obtaining memory snapshots of the target application at the start and end times of the target memory detection period and storing them in a local file, memory leak-related data is determined based on the function call tree. Indirect references are filtered using memory dump flags, reducing memory usage and improving detection efficiency.
It reduces the memory footprint of memory snapshots, avoids memory crashes, improves the efficiency of memory leak detection, and simplifies the analysis process.
Smart Images

Figure CN122364048A_ABST
Abstract
Description
Technical Field
[0001] This disclosure relates to the field of computer technology, and more specifically, to a memory leak detection method, apparatus, device, storage medium, and program product. Background Technology
[0002] In existing technologies, IL2CPP (Intermediate Language to C++ Programming Language) is a technology and toolchain. The main purpose of IL2CPP is to improve the performance, security, and cross-platform compatibility of Unity games. IL2CPP memory leaks in the Unity engine refer to the phenomenon where, when using IL2CPP compilation technology, the program fails to release memory that is no longer in use, leading to a continuous increase in memory usage. Current technologies analyze IL2CPP memory leaks by capturing memory snapshots; however, capturing memory snapshots consumes a lot of memory, and it's easy to fail to capture snapshots, resulting in memory crashes and low efficiency in memory leak detection. Summary of the Invention
[0003] This disclosure addresses the shortcomings of existing methods by proposing a memory leak detection method, apparatus, device, computer-readable storage medium, and computer program product to solve the problem of how to improve the efficiency of memory leak detection.
[0004] Firstly, this disclosure provides a method for detecting memory leaks, including: Obtain memory snapshots of the target application at two target time points during the target memory detection period, and store the two memory snapshots in a local file; the two target time points include the start time point and the end time point of the target memory detection period; Based on two memory snapshots in the local file, determine the function call tree corresponding to each of the two memory snapshots; Based on the function call trees corresponding to the two memory snapshots, relevant data on memory leaks during the target application's operation in the target memory detection period are determined.
[0005] In one embodiment, obtaining memory snapshots of the target application at two target time points during the target memory detection period includes: If the preset engine dynamic library is loaded into memory, then the memory interface function set is obtained from the engine dynamic library, which includes the memory snapshot capture function; The memory snapshot capture function retrieves memory snapshots at the start and end times of the target application.
[0006] In one embodiment, obtaining memory snapshots at the start and end times for the target application using a memory snapshot capture function includes: Based on a preset memory dump flag, a memory snapshot of the start time and the end time of the target application is obtained through a memory snapshot capture function. Neither the memory snapshot of the start time nor the memory snapshot of the end time includes memory usage information related to indirect function calls.
[0007] In one embodiment, storing the two acquired memory snapshots to a local file includes: The two memory snapshots are written to a local file using the write function in the file operation class.
[0008] In one embodiment, for each memory snapshot at a target time point, the function call tree corresponding to the memory snapshot is determined in the following way: Determine multiple function call sequences in a memory snapshot; The function name of each function is determined based on the function address of each function in each function call sequence in multiple function call sequences; Based on the function names of each function and the calling relationships between functions in each function call sequence, determine the symbolic sequence corresponding to each function call sequence; Based on the symbolic sequence corresponding to each function call sequence in multiple function call sequences, the function call tree corresponding to the memory snapshot is determined.
[0009] In one embodiment, the function name of each function is determined based on the function address of each function corresponding to each function in a plurality of function call sequences, including: Based on the function addresses of each function in each function call sequence in multiple function call sequences, the function names of each function are obtained through an address dictionary; The address dictionary is obtained in the following way: Obtain a set of function address samples, which includes at least one function address sample; Based on each function address sample, the function is obtained through symbol information, and the function name corresponding to each function address sample is obtained; An address dictionary is obtained based on the set of function address samples and the function name corresponding to each function address sample.
[0010] In one embodiment, for each memory snapshot at a target time point, the function call tree corresponding to the memory snapshot is determined based on the symbolic sequence corresponding to each function call sequence in multiple function call sequences, including: Based on the symbolic sequence corresponding to each function call sequence in multiple function call sequences, determine the function call tree corresponding to the memory snapshot, the total number of calls to each function in multiple function call sequences, and the memory usage information corresponding to the calls to each function in multiple function call sequences.
[0011] In one embodiment, based on the function call trees corresponding to two memory snapshots, relevant data on memory leaks during the target application's execution in the target memory detection period are determined, including: Based on the function call trees corresponding to the two memory snapshots, the total number of calls to each function in multiple function call sequences, and the memory usage information corresponding to the calls to each function in multiple function call sequences, relevant data on memory leaks during the target application's operation during the target memory detection period are determined.
[0012] In one embodiment, based on the function call trees corresponding to two memory snapshots, the total number of calls to each function in multiple function call sequences, and the memory usage information corresponding to the calls to each function in multiple function call sequences, relevant data on memory leaks during the target application's operation in the target memory detection period are determined, including: Based on the function call tree corresponding to the memory snapshot at the start time, the total number of calls to each function in multiple function call sequences, and the memory usage information corresponding to the calls to each function in multiple function call sequences, determine the memory usage of each function in the function call tree corresponding to the memory snapshot at the start time. Based on the function call tree corresponding to the memory snapshot at the end time point, the total number of calls to each function in multiple function call sequences, and the memory usage information corresponding to the calls to each function in multiple function call sequences, determine the memory usage of each function in the function call tree corresponding to the memory snapshot at the end time point. If the memory usage of each function in the function call tree corresponding to the memory snapshot at the end time point is greater than the memory usage of each function in the function call tree corresponding to the memory snapshot at the start time point, then each function is identified as a memory leak item in the target application during the target memory detection period.
[0013] Secondly, this disclosure provides a memory leak detection device, comprising: The first processing module is used to obtain memory snapshots of the target application at two target time points during the target memory detection period, and store the two memory snapshots in a local file; wherein, the two target time points include the start time point and the end time point of the target memory detection period; The second processing module is used to determine the function call tree corresponding to each of the two memory snapshots based on the two memory snapshots in the local file. The third processing module is used to determine relevant data about memory leaks during the target application's operation in the target memory detection period, based on the function call trees corresponding to the two memory snapshots.
[0014] Thirdly, this disclosure provides an electronic device, including: a processor, a memory, and a bus; A bus is used to connect the processor and memory; Memory, used to store operation instructions; A processor is configured to execute the memory leak detection method of the first aspect of this disclosure by invoking operation instructions.
[0015] Fourthly, this disclosure provides a computer-readable storage medium storing a computer program that is used to execute the memory leak detection method of the first aspect of this disclosure.
[0016] Fifthly, this disclosure provides a computer program product, including a computer program that, when executed by a processor, implements the steps of the memory leak detection method in the first aspect of this disclosure.
[0017] The technical solutions provided in this disclosure have at least the following beneficial effects: The system acquires memory snapshots of the target application at two target time points during the target memory detection period and stores these snapshots in a local file. The two target time points include the start and end times of the target memory detection period. Based on the two memory snapshots in the local file, the system determines the function call tree corresponding to each snapshot. Based on the function call trees corresponding to the two memory snapshots, the system identifies relevant data about memory leaks during the target application's operation during the target memory detection period. By writing the memory snapshots to a local file, the system reduces the memory footprint of the snapshots, preventing memory crashes caused by excessive memory usage and thus improving the efficiency of memory leak detection. Attached Figure Description
[0018] To more clearly illustrate the technical solutions in the embodiments of this disclosure, the accompanying drawings used in the description of the embodiments of this disclosure will be briefly introduced below.
[0019] Figure 1 This is a schematic diagram of the architecture of a memory leak detection system provided in an embodiment of this disclosure; Figure 2 A schematic flowchart illustrating a memory leak detection method provided in an embodiment of this disclosure; Figure 3 A schematic diagram illustrating memory leak detection provided in an embodiment of this disclosure; Figure 4 A schematic diagram illustrating memory leak detection provided in an embodiment of this disclosure; Figure 5 A schematic diagram illustrating memory leak detection provided in an embodiment of this disclosure; Figure 6 A schematic diagram illustrating memory leak detection provided in an embodiment of this disclosure; Figure 7 A schematic flowchart illustrating a memory leak detection method provided in an embodiment of this disclosure; Figure 8 A schematic diagram illustrating memory leak detection provided in an embodiment of this disclosure; Figure 9 This is a schematic diagram of the structure of a memory leak detection device provided in an embodiment of the present disclosure; Figure 10 This is a schematic diagram of the structure of an electronic device provided in an embodiment of the present disclosure. Detailed Implementation
[0020] The embodiments of this disclosure are described below with reference to the accompanying drawings. It should be understood that the embodiments described below with reference to the accompanying drawings are exemplary descriptions for explaining the technical solutions of the embodiments of this disclosure, and do not constitute a limitation on the technical solutions of the embodiments of this disclosure.
[0021] Those skilled in the art will understand that, unless specifically stated otherwise, the singular forms “a,” “an,” “the,” and “the” used herein may also include the plural forms. It should be further understood that the terms “comprising” and “including” as used in embodiments of this disclosure mean that the corresponding feature can be implemented as the presented feature, information, data, step, operation, element, and / or component, but do not exclude implementation as other features, information, data, step, operation, element, component, and / or combinations thereof supported by the art. It should be understood that when we say that an element is “connected” or “coupled” to another element, the one element can be directly connected or coupled to the other element, or it can mean that the one element and the other element are connected through an intermediate element. Furthermore, “connected” or “coupled” as used herein can include wireless connection or wireless coupling. The term “and / or” as used herein indicates at least one of the items defined by the term; for example, “A and / or B” indicates implementation as “A,” or implementation as “B,” or implementation as “A and B.”
[0022] It is understood that in the specific embodiments of this disclosure, data related to memory leak detection is involved. When the above embodiments of this disclosure are applied to specific products or technologies, user permission or consent is required, and the collection, use and processing of related data must comply with the relevant laws, regulations and standards of the relevant countries and regions.
[0023] To make the objectives, technical solutions, and advantages of this disclosure clearer, the embodiments of this disclosure will be described in further detail below with reference to the accompanying drawings.
[0024] This disclosure provides a memory leak detection method for a memory leak detection system, which relates to fields such as memory leak detection.
[0025] To better understand and explain the solutions of the embodiments of this disclosure, some technical terms involved in the embodiments of this disclosure will be briefly explained below.
[0026] C#: C# is an object-oriented, component-oriented programming language. It is a safe, stable, and simple object-oriented programming language derived from C and C++.
[0027] C++: C++ can perform procedural programming like C, object-based programming with abstract data types, and object-oriented programming with inheritance and polymorphism. C++ excels in both object-oriented and procedural programming. C++ can create almost any type of program: games, device drivers, cloud applications, desktop applications, embedded and mobile applications, and even libraries and compilers for other programming languages are written in C++.
[0028] Unity: Unity is a cross-platform game engine that can be used to develop 2D and 3D games, supporting various personal computers, mobile devices, game consoles, web platforms, augmented reality, virtual reality, and more.
[0029] IL2CPP: IL2CPP converts the compiled intermediate language into cpp code (cpp files are C++ source code files) through the IL2CPP translator, and compiles the cpp code using compilers for different platforms to achieve cross-platform compatibility.
[0030] Tab Page: A tab page is an interface design method that uses a series of independent tabs at the top or side of the page to display different content areas on the same page.
[0031] libil2cpp: libil2cpp is a core dynamic library generated by the Unity IL2CPP compilation process. libil2cpp is responsible for converting C# code into C++ and executing it. libil2cpp is loaded into memory when the application starts so that code compiled based on IL2CPP can run.
[0032] FileStream: FileStream is a class in the System.IO namespace of the .NET Framework that provides the ability to read and write files.
[0033] Recursive calls to memory objects: Recursive calls to memory objects usually refer to a situation in a program where an object (or a group of objects) references itself directly or indirectly in some way, thus forming a recursive structure or call chain.
[0034] Function call sequence: The function call sequence refers to the order in which functions are called during program execution.
[0035] The dladdr function is a function in the POSIX standard that returns a Dl_info structure containing information about a given function address. The Dl_info structure typically contains the function name, the path to the function's shared object, and other relevant information.
[0036] isMemoryDumping: isMemoryDumping is a flag that is typically used to indicate whether a memory dump is in progress.
[0037] dynamic_array<uint8_t> : dynamic_array<uint8_t> It is a dynamic array data structure, where uint8_t indicates that the elements in the array are unsigned 8-bit integers (i.e., bytes).
[0038] UnityProfiler tool: UnityProfiler is a tool that can be used to obtain application performance information.
[0039] The System.String class is a fundamental and very important class in the .NET framework (including the Mono framework used by Unity). The System.String class represents a series of characters, i.e., text. In C# and other .NET languages, the String type is widely used to represent and process text data. In Unity, the System.String type is widely used in various aspects of game development.
[0040] Native objects: Native objects are objects created in Java through native code (such as C or C++ code) called via JNI (Java Native Interface). These objects are typically used to handle tasks that Java cannot directly process, such as direct memory operations and hardware access.
[0041] Memory snapshots: Memory snapshots include information about the application's memory usage at the snapshot point in time, such as object allocation, peak memory usage, and memory leaks. This data is saved in the form of snapshots and can reflect the application's memory state at a specific point in time.
[0042] JSON: JSON (JavaScript Object Notation) is an open standard file format and data exchange format. JSON is easy to read and write, and also easy for machines to parse and generate. JSON is language-independent, and many programming languages support data exchange in JSON format. JSON is a commonly used data format with various applications in electronic data interchange, including data exchange between web applications and servers. JSON's concise and clear hierarchical structure effectively improves network transmission efficiency, making it an ideal data exchange language.
[0043] In existing technologies, Unity game memory performance testing involves building a Release version that includes all game scene resources; using the PerfDog tool to collect performance data metrics, such as Persistent SetSize (PSS, the amount of memory continuously occupied and not released by the application); if a memory leak issue with continuously increasing memory is detected, a Debug version is built for Profiler data collection, the collected memory snapshots are analyzed in detail, and various targeted performance optimizations are implemented. After the version is released, the next round of version performance testing is conducted. The drawbacks of this current technology are that the UnityProfiler tool consumes a large amount of memory when capturing memory snapshots, easily leading to failure to capture snapshots and resulting in memory crashes; when capturing a large number of objects in complex scenes, the capture speed is slow, resulting in extremely long waiting times, often leading to short recorded scene durations that cannot effectively cover complex scenes; and the slow storage and retrieval time prevents comparison of two-frame snapshots, requiring more manual analysis time.
[0044] In existing technologies, the memory snapshot function in UnityProfiler allocates three large blocks of memory during the capture process, which can easily lead to crashes during memory snapshot capture. For example, retrieving memory-related information from the C# script layer using the interface function `cripting_capture_memory_snapshot` will allocate and occupy 121MB of memory. That is, calling the memory interface function `cripting_capture_memory_snapshot` to retrieve memory-related information from the C# script layer requires 121MB of memory; the interface function `CaptureNativeObjects( The snapshot method retrieves native objects and their reference relationships (e.g., direct references to native objects). The native objects and their reference relationships will occupy 380MB of memory allocation. This is achieved through the interface function SerializeSnapshot. The UnityProfiler tool (using a snapshot buffer) writes a captured memory snapshot to memory, allocating 512MB of memory. This snapshot includes memory-related information, native objects, and references from the C# script layer. These three memory blocks (121MB, 380MB, and 512MB) cumulatively account for nearly 1GB of memory usage beyond the game's overall memory usage. Therefore, the UnityProfiler tool is very prone to crashing when capturing memory snapshots. The memory snapshot includes memory-related information, native objects, and references from the C# script layer, and is captured using a dynamic array.<uint8_t> The method is stored in memory.
[0045] In existing technologies, when dealing with recursive calls to memory objects, the UnityProfiler tool calculates both direct and indirect references. It checks whether an object has indirect references, and if so, saves the data associated with those indirect references into a total reference array. This results in extremely long temporary arrays in the middle. For example, an object of the QuickMarkAnimChecker class may only have one direct reference. However, because indirect references are calculated, the final result after recursive calculations is an array containing 140,000 objects, which is then saved in memory.
[0046] Based on this, the present disclosure provides a memory leak detection method, apparatus, device, computer-readable storage medium, and program product, and the specific technical solutions will be described below.
[0047] The solutions provided in this disclosure relate to memory leak detection technology. Specific embodiments are described in detail below. These specific embodiments can be combined with each other, and the same or similar concepts or processes may not be repeated in some embodiments. The embodiments of this disclosure will now be described with reference to the accompanying drawings.
[0048] To better understand the solution provided in this disclosure, the solution will be described below in conjunction with a specific application scenario.
[0049] In one embodiment, Figure 1 The diagram illustrates the architecture of a memory leak detection system applicable to embodiments of this disclosure. It is understood that the memory leak detection method provided in these embodiments can be applied to, but is not limited to, applications such as... Figure 1 In the application scenarios shown.
[0050] In this example, as Figure 1As shown, the architecture of the memory leak detection system in this example may include, but is not limited to, server 10, terminal 20, and database 30. Server 10, terminal 20, and database 30 can interact via network 40.
[0051] Server 10 acquires memory snapshots of the target application at two target time points during the target memory detection period and stores the two memory snapshots in a local file. The two target time points include the start and end times of the target memory detection period. Based on the two memory snapshots in the local file, Server 10 determines the function call tree corresponding to each of the two memory snapshots. Based on the function call trees corresponding to the two memory snapshots, Server 10 determines relevant data regarding memory leaks during the target application's operation during the target memory detection period. Server 10 sends the relevant memory leak data to Terminal 20 for display and to Database 30 for storage.
[0052] It is understood that the above is only one example, and this embodiment is not limited here.
[0053] Terminals include, but are not limited to, smartphones (such as Android phones, iOS phones, etc.), mobile phone emulators, tablets, laptops, digital broadcast receivers, MIDs (Mobile Internet Devices), PDAs (Personal Digital Assistants), smart voice interaction devices, smart home appliances, and in-vehicle terminals.
[0054] A server can be a standalone physical server, a server cluster or distributed system consisting of multiple physical servers, or a cloud server or server cluster that provides basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, CDN (Content Delivery Network), and big data and artificial intelligence platforms.
[0055] The aforementioned networks may include, but are not limited to, wired networks and wireless networks. Wired networks include local area networks (LANs), metropolitan area networks (MANs), and wide area networks (WANs). Wireless networks include Bluetooth, Wi-Fi, and other networks that enable wireless communication. Specific details can be determined based on actual application scenario requirements and are not limited here.
[0056] See Figure 2 , Figure 2This diagram illustrates a flowchart of a memory leak detection method provided in an embodiment of this disclosure. This method can be executed by any electronic device, such as a server. As an optional implementation, the method can be executed by a server. For ease of description, the following description of some optional embodiments will use server execution as an example. Figure 2 As shown, the memory leak detection method provided in this embodiment includes the following steps: S201, obtain memory snapshots of the target application at two target time points during the target memory detection period, and store the two memory snapshots in a local file; wherein, the two target time points include the start time point and the end time point of the target memory detection period.
[0057] Specifically, the target application is, for example, a game, such as a Unity game. The target memory detection period is, for example, a certain time period during the game's execution, such as the time from the start to the end of the game. The start time of the target memory detection period is, for example, the start time of the game. The start time of the target memory detection period is, for example, a certain point in the game's execution. The end time of the target memory detection period is, for example, the end time of the game's execution. For example, memory snapshots are stored in local files on hardware devices, such as hard drives. In this way, writing memory snapshots to local files on hardware devices reduces the memory footprint of memory snapshots, avoiding memory crashes caused by excessive memory footprint of memory snapshots, thereby improving the efficiency of memory leak detection.
[0058] S202, based on two memory snapshots in the local file, determine the function call tree corresponding to each of the two memory snapshots.
[0059] Specifically, based on two memory snapshots in the local file, the function call trees corresponding to each of the two memory snapshots are determined by parsing the snapshot data.
[0060] For example, the function call tree is a tree structure. Since the top level of all function calls starts from the app_entry function, the app_entry function can be used as the top node (root node) of the entire tree structure. That is, the app_entry function is used as the root node of the function call tree, and each child node of the tree structure represents a function.
[0061] S203, based on the function call trees corresponding to the two memory snapshots, determines the relevant data of memory leaks during the target application's operation during the target memory detection period.
[0062] Specifically, for example, the function call tree corresponding to each of the two memory snapshots is compared with the function with the same name at each level. If the allocated memory of a function with the same name (e.g., the memory occupied by the function call with the same name during game operation) increases, then the function with the same name is identified as a leak item in memory leak.
[0063] In this embodiment, memory snapshots of the target application at two target time points during the target memory detection period are obtained, and the two memory snapshots are stored in a local file. The two target time points include the start time point and the end time point of the target memory detection period. Based on the two memory snapshots in the local file, the function call tree corresponding to each of the two memory snapshots is determined. Based on the function call tree corresponding to each of the two memory snapshots, relevant data on memory leaks during the operation of the target application in the target memory detection period are determined. In this way, writing the memory snapshots to the local file reduces the memory usage of the memory snapshots and avoids memory crashes caused by excessive memory usage of the memory snapshots, thereby improving the efficiency of memory leak detection.
[0064] In one embodiment, obtaining memory snapshots of the target application at two target time points during the target memory detection period includes: If the preset engine dynamic library is loaded into memory, then the memory interface function set is obtained from the engine dynamic library, which includes the memory snapshot capture function; The memory snapshot capture function retrieves memory snapshots at the start and end times of the target application.
[0065] Specifically, engine dynamic libraries include, for example, libil2cpp in IL2CPP. For instance, the memory interface function set includes functions such as the memory capture function il2cpp_capture_memory_snapshot, the memory capture and release function il2cpp_free_captured_memory_snapshot, the heap memory usage function il2cpp_gc_get_used_size, the overall heap memory size function il2cpp_gc_get_heap_size, the memory allocation size function il2cpp_gc_alloc_size, and the memory allocation count function il2cpp_gc_alloc_count. Among these, the memory snapshot capture function is the memory capture function il2cpp_capture_memory_snapshot. The frame function `il2cpp_capture_memory_snapshot` is used to capture a snapshot of the current memory heap. The memory snapshot release function `il2cpp_free_captured_memory_snapshot` is used to release the memory snapshot previously captured by the memory snapshot function `il2cpp_capture_memory_snapshot`. The heap memory usage function `il2cpp_gc_get_used_size` is used to get the size of the currently used heap memory. The overall heap memory size function `il2cpp_gc_get_heap_size` is used to get the size of the entire heap memory. The memory allocation size function `il2cpp_gc_alloc_size` is used to get the size of the memory allocated since the last GC (Garbage Collection). The memory allocation count function `il2cpp_gc_alloc_count` is used to get the number of memory allocations since the last GC.
[0066] For example, to determine whether the engine dynamic library libil2cpp in IL2CPP is loaded into memory, if it is determined that the engine dynamic library libil2cpp is loaded into memory, then six memory interface functions are obtained from the engine dynamic library libil2cpp. These six memory interface functions are: memory capture function il2cpp_capture_memory_snapshot, memory capture and release function il2cpp_free_captured_memory_snapshot, heap memory usage function il2cpp_gc_get_used_size, overall heap memory size function il2cpp_gc_get_heap_size, memory allocation size function il2cpp_gc_alloc_size, and memory allocation count function il2cpp_gc_alloc_count.
[0067] Memory snapshot capture functions include il2cpp_capture_memory_snapshot, which includes crippling_capture_memory_snapshot, crippling_capture_memory_snapshot, and SerializeSnapshot. Interface functions such as snapshot and buffer.
[0068] For example, calling the memory interface function `cripting_capture_memory_snapshot` to obtain memory-related information in the C# script layer requires 121MB of memory; using the interface function `CaptureNativeObjects(` A snapshot is used to obtain the native object and its reference relationships (e.g., direct references to native objects). Then, a file writing function (e.g., filewriter) in a file operation class (e.g., FileStream) is used to write the native object and its reference relationships to a local file, thus saving 380MB of memory allocation. This is based on the interface function SerializeSnapshot ( The snapshot (buffer) is written to a local file using file operation classes (such as FileStream) and file writing functions (such as filewriter), thus saving 512M of memory allocation. In this way, 380M + 512M = 892MB of memory usage is directly saved, thereby avoiding memory crashes during the memory snapshot capture process and improving the efficiency of memory leak detection.
[0069] Memory dump flags, such as `isMemoryDumping`, indicate whether to skip the retrieval of function indirect calls. If `isMemoryDumping` is set to 1, it means that the retrieval of function indirect calls is skipped, i.e., function indirect calls are not retrieved. For example, adding an `isMemoryDumping` flag at a point in a recursive call to a memory object (where this code represents a loop retrieving indirect references) will, when taking a memory snapshot, if `isMemoryDumping` is set to 1, the entire conditional evaluation result in the recursive call to the memory object will be false. This avoids the loop retrieving indirect references when the conditional evaluation result is true, effectively filtering out indirect references and skipping the extremely lengthy indirect reference retrieval process. This significantly improves reference retrieval efficiency, directly optimizing the previous 307 seconds of reference retrieval time to 3 seconds. Thus, by using `isMemoryDumping` to filter out indirect references to functions (objects), the speed of obtaining memory snapshots (which do not include indirect references) is improved, thereby increasing the efficiency of memory leak detection.
[0070] For example, by setting the memory dump flag isMemoryDumping to 1, a memory snapshot of the Unity game at the start and end times can be obtained through a memory snapshot interception function. Since isMemoryDumping is set to 1, indirect references to functions are filtered out. Therefore, the memory snapshots at the start and end times of the Unity game do not include indirect references, meaning that the memory snapshots at the start and end times of the Unity game do not include memory usage information associated with indirect function calls. This improves the speed of obtaining memory snapshots at the start and end times, thereby improving the efficiency of memory leak detection.
[0071] In one embodiment, obtaining memory snapshots at the start and end times for the target application using a memory snapshot capture function includes: Based on a preset memory dump flag, a memory snapshot of the start time and the end time of the target application is obtained through a memory snapshot capture function. Neither the memory snapshot of the start time nor the memory snapshot of the end time includes memory usage information related to indirect function calls.
[0072] Specifically, the memory dump flag is, for example, isMemoryDumping; the meaning of isMemoryDumping includes whether the acquisition of function indirect calls is skipped. If isMemoryDumping is set to 1, it means that the acquisition of function indirect calls is skipped, that is, function indirect calls are not acquired.
[0073] For example, by adding `isMemoryDumping` to a section of code within a recursive call to a memory object (which represents a loop that retrieves indirect references), when taking a memory snapshot, if `isMemoryDumping` is set to 1, the entire conditional evaluation result in the recursive call to the memory object will be false. This avoids the loop that retrieves indirect references when the conditional evaluation result is true, effectively filtering out indirect references and skipping the extremely lengthy indirect reference retrieval process. This significantly improves reference retrieval efficiency, reducing the previous 307 seconds to 3 seconds. Thus, by using `isMemoryDumping` to filter out indirect references to functions (objects), the speed of obtaining memory snapshots (which do not include indirect references) is improved, thereby increasing the efficiency of memory leak detection.
[0074] For example, by setting the memory dump flag isMemoryDumping to 1, a memory snapshot of the Unity game at the start and end times can be obtained through a memory snapshot interception function. Since isMemoryDumping is set to 1, indirect references to functions are filtered out. Therefore, the memory snapshots at the start and end times of the Unity game do not include indirect references, meaning that the memory snapshots at the start and end times of the Unity game do not include memory usage information associated with indirect function calls. This improves the speed of obtaining memory snapshots at the start and end times, thereby improving the efficiency of memory leak detection.
[0075] In one embodiment, storing the two acquired memory snapshots to a local file includes: The two memory snapshots are written to a local file using the write function in the file operation class.
[0076] Specifically, file operation classes include FileStream, and file writing functions include filewriter; for example, two memory snapshots can be written to a local file using FileStream within FileStream.
[0077] For example, calling the memory interface function `cripting_capture_memory_snapshot` to obtain memory-related information in the C# script layer requires 121MB of memory; using the interface function `CaptureNativeObjects(` A snapshot is used to obtain the native object and its reference relationships (e.g., direct references to native objects). Then, a file writing function (e.g., filewriter) in a file operation class (e.g., FileStream) is used to write the native object and its reference relationships to a local file, thus saving 380MB of memory allocation. This is based on the interface function SerializeSnapshot ( The snapshot (buffer) is written to a local file using file operation classes (such as FileStream) and file writing functions (such as filewriter), thus saving 512M of memory allocation. In this way, 380M + 512M = 892MB of memory usage is directly saved, thereby avoiding memory crashes during the memory snapshot capture process and improving the efficiency of memory leak detection.
[0078] In one embodiment, for each memory snapshot at a target time point, the function call tree corresponding to the memory snapshot is determined by the following method, which includes steps A1-A4: Step A1: Identify multiple function call sequences in the memory snapshot.
[0079] Specifically, determine multiple function call sequences for each of the two memory snapshots of the local file.
[0080] For example, after obtaining an object (function) by direct reference, multiple function call sequences are determined. Each function call sequence includes a call relationship between at least two functions, and each function call sequence contains function address values (function addresses). For example, a function call sequence may include address 1—address 2—address 3, meaning the function corresponding to address 1 calls the function corresponding to address 2, and the function corresponding to address 2 calls the function corresponding to address 3. An address dictionary can be established between function addresses and their corresponding function names. Using the address dictionary, the function addresses are aligned and parsed to restore them to name strings (function names). For example, addresses 1, 2, and 3 in the function call sequence can be parsed and restored to aa, bb, and cc using the address dictionary. Here, aa, bb, and cc are all function names, the function address of function aa is address 1, the function address of function bb is address 2, and the function address of function cc is address 3.
[0081] Step A2: Determine the function name of each function based on the function address of each function corresponding to each function call sequence in the multiple function call sequences.
[0082] Specifically, for example, based on the function addresses of each function in each function call sequence in multiple function call sequences, the function names of each function can be obtained through an address dictionary.
[0083] For example, a function call sequence might include address 1—address 2—address 3, meaning the function at address 1 calls the function at address 2, and the function at address 2 calls the function at address 3. An address dictionary can be created to align function addresses and their corresponding function names. Using this dictionary, the function addresses can be parsed and restored to their original name strings (function names). For instance, addresses 1, 2, and 3 in a function call sequence can be parsed using the address dictionary to become aa, bb, and cc, where aa, bb, and cc are all function names. The address of function aa is address 1, the address of function bb is address 2, and the address of function cc is address 3.
[0084] Step A3: Based on the function names of each function and the calling relationships between functions in each function call sequence, determine the symbolic sequence corresponding to each function call sequence.
[0085] Specifically, for example, a function call sequence includes address 1—address 2—address 3, where address 1 is the function address of function aa, address 2 is the function address of function bb, and address 3 is the function address of function cc. That is, function aa corresponding to address 1 calls function bb corresponding to address 2, and function bb corresponding to address 2 calls function cc corresponding to address 3. The symbolic sequence corresponding to this function call sequence includes aa-bb-cc, that is, function aa calls function bb, and function bb calls function cc.
[0086] Step A4: Based on the symbolic sequence corresponding to each function call sequence in multiple function call sequences, determine the function call tree corresponding to the memory snapshot.
[0087] Specifically, for example, a tree structure (function call tree) is built and stored for multiple symbolic sequences. Since all function calls start from the `app_entry` function at the top level, the `app_entry` function can be used as the top node (root node) of the entire tree structure. Each child node in the tree structure represents a function. Based on the successive symbolic sequences following `app_entry`, the function nodes (child nodes) of these symbolic sequences can be continuously inserted into the tree structure, and for each function... The function call tree is accumulated from multiple calls to obtain the accumulated memory allocation (the memory occupied by each function call, i.e., the memory usage information corresponding to each function call) and the total number of function calls. This tree structure is then serialized and compressed, i.e., serialized into a format that is easy to store and transmit, such as JSON. The JSON string is then compressed to obtain the final file. The final file contains the function call tree with app_entry as the root node, the total number of calls to each function, and the memory usage information corresponding to each function call. The final file is the final memory snapshot.
[0088] It should be noted that, based on two memory snapshots in a local file, the function call trees corresponding to the two memory snapshots are determined by parsing the snapshot data; based on each function call tree, the final memory snapshot is determined through serialization and compression; and memory leak detection is performed based on the final memory snapshot, thereby improving the efficiency of memory leak detection.
[0089] In one embodiment, the function name of each function is determined based on the function address of each function corresponding to each function in a plurality of function call sequences, including: Based on the function addresses of each function in each function call sequence in multiple function call sequences, the function names of each function are obtained through an address dictionary; The address dictionary is obtained in the following way: Obtain a set of function address samples, which includes at least one function address sample; Based on each function address sample, the function is obtained through symbol information, and the function name corresponding to each function address sample is obtained; An address dictionary is obtained based on the set of function address samples and the function name corresponding to each function address sample.
[0090] Specifically, for example, a function call sequence includes address 1—address 2—address 3, meaning the function corresponding to address 1 calls the function corresponding to address 2, and the function corresponding to address 2 calls the function corresponding to address 3. An address dictionary can be created between function addresses and their corresponding function names. Using this dictionary, the function addresses can be aligned and parsed to restore the name string (function name). For example, addresses 1, 2, and 3 in the function call sequence can be parsed using the address dictionary to restore them to aa, bb, and cc, where aa, bb, and cc are all function names. The address of function aa is address 1, the address of function bb is address 2, and the address of function cc is address 3.
[0091] For example, a symbol information retrieval function might be the `dladdr` function, and a function address sample might be address 1. Based on address 1 of function `aa`, the `dladdr` function obtains a `dl_info` structure. The `dl_info` structure includes a pointer `dli_sname`, which points to the name (function name) of the symbol (function) closest to the specified address (e.g., address 1). This symbol may share the same address, or it may be the closest symbol with a lower-order address. In this case, the obtained function name is still a low-level symbol name, which can be decoded into a user-level name (e.g., `aa`) using `abi::__cxa_demangle`, making the C++ type name (user-level name) readable. Based on multiple function address samples and the function name corresponding to each function address sample, an address dictionary is obtained. The `dl_info` structure is shown below: struct{ const char dli_fname; / The pathname of the shared object where the symbol is located / void dli_fbase; / Indicates the base address in memory where the shared object is loaded. / const char dli_sname; / The name of the most recent symbol / void dli_saddr; / The exact address of the nearest symbol / size_t dli_size; / The size of the symbol / int dli_bind; / Symbol binding properties / int dli_type; / Types of symbols / }
[0092] For example, such as Figure 3 As shown, the process involves: summarizing function call sequences, i.e., determining multiple function call sequences in a memory snapshot; building an address dictionary table, i.e., constructing an address dictionary based on a set of function address samples and the function name corresponding to each function address sample; obtaining the called function name, i.e., obtaining the function name (called function name) of each function based on the function address of each function corresponding to each function call sequence in multiple function call sequences through the address dictionary; and saving all symbolic sequences, i.e., determining the symbolic sequence corresponding to each function call sequence based on the function name of each function and the calling relationship between functions in each function call sequence, and determining the function call tree based on each symbolic sequence, thereby saving each symbolic sequence through a function call tree (tree structure).
[0093] It should be noted that by using the address dictionary to obtain the function names of each function in each function call sequence, the function call tree can be determined more quickly, thereby speeding up the subsequent determination of the function call tree and improving the efficiency of memory leak detection.
[0094] In one embodiment, for each memory snapshot at a target time point, the function call tree corresponding to the memory snapshot is determined based on the symbolic sequence corresponding to each function call sequence in multiple function call sequences, including: Based on the symbolic sequence corresponding to each function call sequence in multiple function call sequences, determine the function call tree corresponding to the memory snapshot, the total number of calls to each function in multiple function call sequences, and the memory usage information corresponding to the calls to each function in multiple function call sequences.
[0095] Specifically, for example, a tree structure (function call tree) is built and stored for multiple symbolic sequences. Since all function calls start from the `app_entry` function at the top level, the `app_entry` function can be used as the top node (root node) of the entire tree structure. Each child node in the tree structure represents a function. Based on the successive symbolic sequences following `app_entry`, the function nodes (child nodes) of these symbolic sequences can be continuously inserted into the tree structure, and multiple calls to each function can be processed. The calls are accumulated to obtain the accumulated memory allocation (the memory occupied by multiple calls to each function, i.e., the memory usage information corresponding to each function call) and the number of function calls (the total number of times the function is called). This tree structure is then serialized and compressed, that is, the tree structure is serialized into a format that is easy to store and transmit, such as JSON, and the JSON string is compressed to obtain the final file. The final file contains the function call tree with app_entry as the root node, the total number of calls to each function, the memory usage information corresponding to each function call, etc. The final file is the final memory snapshot, i.e., the symbolic memory snapshot.
[0096] It should be noted that, based on two memory snapshots in the local file, the function call trees corresponding to the two memory snapshots are determined by parsing the snapshot data; based on each function call tree, the final memory snapshot is determined by serialization compression; the two final memory snapshots are divided into a symbolic memory snapshot at the start time of the target memory detection period and a symbolic memory snapshot at the end time of the target memory detection period; memory leak detection is performed based on the two final memory snapshots, thereby improving the efficiency of memory leak detection.
[0097] In one embodiment, based on the function call trees corresponding to two memory snapshots, relevant data on memory leaks during the target application's execution in the target memory detection period are determined, including: Based on the function call trees corresponding to the two memory snapshots, the total number of calls to each function in multiple function call sequences, and the memory usage information corresponding to the calls to each function in multiple function call sequences, relevant data on memory leaks during the target application's operation during the target memory detection period are determined.
[0098] Specifically, for example, the two final memory snapshots are a symbolic memory snapshot at the start time of the target memory detection period and a symbolic memory snapshot at the end time of the target memory detection period. The root node of the function call tree corresponding to each of the two final memory snapshots, the app_entry function, is used to compare the functions with the same name at each level. If the allocated memory of a function with the same name (e.g., the memory occupied by the function call with the same name during game operation) increases, then the function with the same name is identified as a memory leak item. All leak items are saved and all leak items are summarized and reported.
[0099] It should be noted that the two final memory snapshots are a symbolic memory snapshot at the start time of the target memory detection period and a symbolic memory snapshot at the end time of the target memory detection period; memory leak detection is performed based on the two final memory snapshots, thereby improving the efficiency of memory leak detection.
[0100] In one embodiment, based on the function call trees corresponding to two memory snapshots, the total number of calls to each function in multiple function call sequences, and the memory usage information corresponding to the calls to each function in multiple function call sequences, relevant data on memory leaks during the target application's operation in the target memory detection period are determined, including: Based on the function call tree corresponding to the memory snapshot at the start time, the total number of calls to each function in multiple function call sequences, and the memory usage information corresponding to the calls to each function in multiple function call sequences, determine the memory usage of each function in the function call tree corresponding to the memory snapshot at the start time. Based on the function call tree corresponding to the memory snapshot at the end time point, the total number of calls to each function in multiple function call sequences, and the memory usage information corresponding to the calls to each function in multiple function call sequences, determine the memory usage of each function in the function call tree corresponding to the memory snapshot at the end time point. If the memory usage of each function in the function call tree corresponding to the memory snapshot at the end time point is greater than the memory usage of each function in the function call tree corresponding to the memory snapshot at the start time point, then each function is identified as a memory leak item in the target application during the target memory detection period.
[0101] Specifically, for example, such as Figure 4As shown, two final memory snapshots are obtained, namely, the symbolic memory snapshot at the start time of the target memory detection period and the symbolic memory snapshot at the end time of the target memory detection period. A step-by-step comparison is performed, that is, starting from the root node `app_entry` function of the function call tree corresponding to each of the two final memory snapshots, a step-by-step comparison of functions with the same name is performed. If the allocated memory of a function with the same name (e.g., function A) increases (the memory usage of function A in the function call tree corresponding to the symbolic memory snapshot at the end time is greater than the memory usage of function A in the symbolic memory snapshot at the start time), then this function with the same name is identified as a memory leak item. The memory leak items are saved. All leak items are then summarized and reported.
[0102] It should be noted that the two final memory snapshots are a symbolic memory snapshot at the start time of the target memory detection period and a symbolic memory snapshot at the end time of the target memory detection period; memory leak detection is performed based on the two final memory snapshots, thereby improving the efficiency of memory leak detection.
[0103] Applying the embodiments of this disclosure has at least the following beneficial effects: By using file operation classes (such as FileStream) and their file writing functions (such as filewriter), memory snapshots are written to local files, saving memory usage and avoiding memory crashes during the memory snapshot capture process, thus improving the efficiency of memory leak detection. By using the memory dump flag isMemoryDumping, indirect references to functions are filtered out. Therefore, neither the memory snapshot at the start time nor the memory snapshot at the end time includes indirect references. In other words, neither the memory snapshot at the start time nor the memory snapshot at the end time includes memory usage information associated with indirect function calls, which improves the speed of obtaining memory snapshots at the start time and the end time, thereby improving the efficiency of memory leak detection.
[0104] To better understand the methods provided in the embodiments of this disclosure, the solutions of the embodiments of this disclosure will be further explained below with reference to specific application scenarios.
[0105] In one embodiment, for example, such as Figure 5 The memory snapshot shown is a symbolic memory snapshot. Figure 5 The tabs on the left side of the page display various memory allocation details for the full memory allocation. Figure 5 The middle tab displays a detailed breakdown of various memory classes. Figure 5The right-hand tabs display specific reference allocations and functions. For example, the left-hand tabs show that the System.String class has been allocated a total of 126,538 times (the total number of times the System.String class is called during game runtime) and has a total allocated memory size of 4.04MB (the memory occupied by the System.String class when it is called). The middle tabs show the specific allocations for all System.String types; clicking on the second System.String shows its size as 23 bytes. The right-hand tabs show the function ApolloPufferUpdate that specifically allocates the System.String class, and the System.String class is called by the ApolloPufferUpdate function.
[0106] For example, such as Figure 6 The comparison of the two memory snapshots (symbolized memory snapshots) shown is as follows: Figure 6 The tabs on the left side of the page display the details of the newly added memory allocation, for example... Figure 6 The tab on the left shows the two newly added ApolloPufferUpdate memory type data, which together occupy a total of 40 bytes of memory. Figure 6 The middle tab shows that these two ApolloPufferUpdates were allocated a total of 3 times, and the total memory allocated was 32 bytes, meaning the total memory occupied was 32 bytes. Figure 6 The tab on the right shows that ApolloPufferUpdate is called by three functions, namely functions aa, bb, and cc, which together occupy 32 bytes of memory.
[0107] In a specific application scenario, such as a memory leak detection scenario, see [link to example]. Figure 7 This illustrates the processing flow of a memory leak detection method, such as... Figure 7 As shown, the processing flow of the memory leak detection method provided in this embodiment includes the following steps: S701, when the engine dynamic library is loaded into memory, the server obtains the memory interface function set from the engine dynamic library, which includes the memory snapshot capture function.
[0108] Specifically, this refers to engine dynamic libraries such as libil2cpp within IL2CPP. For instance, to determine if the libil2cpp engine dynamic library is loaded into memory, if it is, then the set of memory interface functions is retrieved from libil2cpp. This set includes six memory interface functions: il2cpp_capture_memory_snapshot (memory frame capture), il2cpp_free_captured_memory_snapshot (memory frame release), il2cpp_gc_get_used_size (heap memory usage), il2cpp_gc_get_heap_size (overall heap size), il2cpp_gc_alloc_size (memory allocation size), and il2cpp_gc_alloc_count (memory allocation count).
[0109] Memory snapshot capture functions include il2cpp_capture_memory_snapshot, which includes crippling_capture_memory_snapshot, crippling_capture_memory_snapshot, and SerializeSnapshot. Interface functions such as snapshot and buffer.
[0110] For example, such as Figure 8 As shown, the memory interface functions in the IL2CPP program are obtained, that is, the set of memory interface functions is obtained from the IL2CPP engine dynamic library libil2cpp.
[0111] S702, the server executes the target application.
[0112] Specifically, the target application is, for example, a Unity game. For example, such as... Figure 8 As shown, the game scene is executed, such as the Unity game execution; the game scene execution test case will continue to execute until the memory leak detection start point, which is the start time point of the Unity game detection period.
[0113] In S703, the server uses a memory dump flag to obtain memory snapshots at the start and end times for the target application through a memory snapshot capture function. Then, it writes the two memory snapshots to a local file using a file write function in the file operation class. Neither the memory snapshot at the start nor the memory snapshot at the end includes memory usage information associated with indirect function calls.
[0114] Specifically, the memory dump flag is, for example, isMemoryDumping; the meaning of isMemoryDumping includes whether the acquisition of function indirect calls is skipped. If isMemoryDumping is set to 1, it means that the acquisition of function indirect calls is skipped, that is, function indirect calls are not acquired.
[0115] For example, by adding `isMemoryDumping` to a section of code within a recursive call to a memory object (which represents a loop that retrieves indirect references), when taking a memory snapshot, if `isMemoryDumping` is set to 1, the entire conditional evaluation result in the recursive call to the memory object will be false. This avoids the loop that retrieves indirect references when the conditional evaluation result is true, effectively filtering out indirect references and skipping the extremely lengthy indirect reference retrieval process. This significantly improves reference retrieval efficiency, reducing the previous 307 seconds to 3 seconds. Thus, by using `isMemoryDumping` to filter out indirect references to functions (objects), the speed of obtaining memory snapshots (which do not include indirect references) is improved, thereby increasing the efficiency of memory leak detection.
[0116] For example, by setting the memory dump flag isMemoryDumping to 1, a memory snapshot of the Unity game at the start and end times can be obtained through a memory snapshot interception function. Since isMemoryDumping is set to 1, indirect references to functions are filtered out. Therefore, the memory snapshots at the start and end times of the Unity game do not include indirect references, meaning that the memory snapshots at the start and end times of the Unity game do not include memory usage information associated with indirect function calls. This improves the speed of obtaining memory snapshots at the start and end times, thereby improving the efficiency of memory leak detection.
[0117] For example, calling the memory interface function `cripting_capture_memory_snapshot` to obtain memory-related information in the C# script layer requires 121MB of memory; using the interface function `CaptureNativeObjects(` A snapshot is used to obtain the native object and its reference relationships (e.g., direct references to native objects). Then, a file writing function (e.g., filewriter) in a file operation class (e.g., FileStream) is used to write the native object and its reference relationships to a local file, thus saving 380MB of memory allocation. This is based on the interface function SerializeSnapshot ( The snapshot (buffer) is written to a local file using file operation classes (such as FileStream) and file writing functions (such as filewriter), thus saving 512M of memory allocation. In this way, 380M + 512M = 892MB of memory usage is directly saved, thereby avoiding memory crashes during the memory snapshot capture process and improving the efficiency of memory leak detection.
[0118] For example, such as Figure 8 As shown, memory snapshot capture, based on the memory snapshot capture instruction, uses the memory snapshot capture function to obtain memory snapshots at the start and end times for the target application, and then uses the write file function in the file operation class to write the two memory snapshots to a local file; memory object recursive acquisition, that is, by setting isMemoryDumping to 1, all referenced objects are recursively acquired during the memory snapshot capture process (acquiring direct references and filtering out indirect references).
[0119] S704: Based on two memory snapshots in a local file, the server determines the function call tree corresponding to the two memory snapshots, the total number of calls to each function in multiple function call sequences, and the memory usage information corresponding to the calls to each function in multiple function call sequences.
[0120] Specifically, multiple function call sequences are determined for each of the two memory snapshots of the local file. For example, after obtaining an object (function) by direct reference, multiple function call sequences are determined. Each function call sequence includes a call relationship between at least two functions, and each function call sequence contains function address values (function addresses). For example, a function call sequence may include address 1—address 2—address 3, meaning the function corresponding to address 1 calls the function corresponding to address 2, and the function corresponding to address 2 calls the function corresponding to address 3. An address dictionary can be established between function addresses and their corresponding function names. Using the address dictionary, the function addresses are aligned and parsed to restore the name string (function name). For example, addresses 1, 2, and 3 in the function call sequence can be parsed and restored to aa, bb, and cc using the address dictionary, where aa, bb, and cc are all function names, the function address of function aa is address 1, the function address of function bb is address 2, and the function address of function cc is address 3.
[0121] For example, based on the function addresses of each function in a series of function call sequences, the function names of each function can be obtained through an address dictionary. For instance, a function call sequence might include address 1—address 2—address 3, meaning the function at address 1 calls the function at address 2, and vice versa. An address dictionary can be created to align the function addresses and their corresponding names. Using this dictionary, the function addresses can be parsed and restored to their original name strings (function names). For example, addresses 1, 2, and 3 in a function call sequence can be parsed using an address dictionary to restore them to "aa," "bb," and "cc," where "aa," "bb," and "cc" are all function names, the address of function "aa" is address 1, the address of function "bb" is address 2, and the address of function "cc" is address 3.
[0122] For example, a function call sequence includes address 1—address 2—address 3, where address 1 is the address of function aa, address 2 is the address of function bb, and address 3 is the address of function cc. That is, function aa at address 1 calls function bb at address 2, and function bb at address 2 calls function cc at address 3. The symbolic sequence corresponding to this function call sequence includes aa-bb-cc, that is, function aa calls function bb, and function bb calls function cc.
[0123] For example, for multiple symbolic sequences, a tree structure (function call tree) can be built for storage. Since all function calls start from the `app_entry` function at the top level, the `app_entry` function can be used as the top node (root node) of the entire tree structure. Each child node in the tree structure represents a function. Based on the successive symbolic sequences following `app_entry`, the function nodes (child nodes) of these symbolic sequences can be continuously inserted into the tree structure, and the multiple calls to each function are accumulated to obtain the accumulated memory allocation (the memory occupied by multiple calls to each function). The tree structure is analyzed by serializing and compressing the function call tree (i.e., the memory usage information corresponding to each function call) and the total number of function calls. This tree structure is then serialized into a format that is easy to store and transmit, such as JSON, and the JSON string is compressed to obtain the final file. The final file contains the function call tree with `app_entry` as the root node, the total number of calls to each function, and the memory usage information corresponding to each function call. The final file is a final memory snapshot, which includes the function call tree, the total number of calls to each function in multiple function call sequences, and the memory usage information corresponding to each function call in multiple function call sequences.
[0124] It should be noted that, for example, such as Figure 8 As shown, based on two memory snapshots in a local file, the function call trees corresponding to the two memory snapshots are determined by parsing the snapshot data; based on each function call tree, the final memory snapshot is determined by serialization compression; and memory leak detection is performed based on the final memory snapshot, thereby improving the efficiency of memory leak detection.
[0125] S705: Based on the function call trees corresponding to the two memory snapshots, the total number of calls to each function in multiple function call sequences, and the memory usage information corresponding to the calls to each function in multiple function call sequences, the server determines the memory leak items in the target application during the target memory detection period.
[0126] Specifically, for example, the final memory snapshot includes a function call tree, the total number of calls to each function in multiple function call sequences, and memory usage information corresponding to the calls to each function in multiple function call sequences.
[0127] For example, such as Figure 4As shown, two final memory snapshots are obtained, namely, the symbolic memory snapshot at the start time of the target memory detection period and the symbolic memory snapshot at the end time of the target memory detection period. A step-by-step comparison is performed, that is, starting from the root node `app_entry` function of the function call tree corresponding to each of the two final memory snapshots, a step-by-step comparison of functions with the same name is performed. If the allocated memory of a function with the same name (e.g., function A) increases (the memory usage of function A in the function call tree corresponding to the symbolic memory snapshot at the end time is greater than the memory usage of function A in the symbolic memory snapshot at the start time), then this function with the same name is identified as a memory leak item. The memory leak items are saved. All leak items are then summarized and reported.
[0128] For example, such as Figure 8 As shown, snapshot data comparison involves comparing two final memory snapshots; after the snapshot data comparison, all leaks are summarized and reported.
[0129] S706, the server sends the leaked items of the memory leak to the terminal for display.
[0130] Specifically, the server sends the leaked items of the memory leak to the terminal, and the terminal displays the leaked items to the user, thereby facilitating the user's subsequent debugging.
[0131] Applying the embodiments of this disclosure has at least the following beneficial effects: By writing file functions (such as filewriter) in file operation classes (such as FileStream), memory snapshots are written to local files, saving 892MB of memory usage and avoiding memory crashes during the memory snapshot capture process, thus improving the efficiency of memory leak detection. By setting the memory dump flag isMemoryDumping to 1, indirect references to functions are filtered out. Therefore, neither the memory snapshot at the start time nor the memory snapshot at the end time includes indirect references, meaning that neither the memory snapshot at the start time nor the memory snapshot at the end time includes memory usage information associated with indirect function calls. This improves the speed of obtaining memory snapshots at the start time and the end time, thereby improving the efficiency of memory leak detection, and thus improving the efficiency of memory leak detection and analysis for various scenarios.
[0132] This disclosure also provides a memory leak detection device, the structural schematic diagram of which is shown below. Figure 9 As shown, the memory leak detection device 90 includes a first processing module 901, a second processing module 902, and a third processing module 903.
[0133] The first processing module 901 is used to obtain memory snapshots of the target application at two target time points during the target memory detection period, and store the two memory snapshots in a local file; wherein, the two target time points include the start time point and the end time point of the target memory detection period; The second processing module 902 is used to determine the function call tree corresponding to each of the two memory snapshots based on the two memory snapshots in the local file. The third processing module 903 is used to determine relevant data on memory leaks during the target application's operation in the target memory detection period based on the function call trees corresponding to the two memory snapshots.
[0134] In one embodiment, the first processing module 901 is specifically used for: If the preset engine dynamic library is loaded into memory, then the memory interface function set is obtained from the engine dynamic library, which includes the memory snapshot capture function; The memory snapshot capture function retrieves memory snapshots at the start and end times of the target application.
[0135] In one embodiment, the first processing module 901 is specifically used for: Based on a preset memory dump flag, a memory snapshot of the start time and the end time of the target application is obtained through a memory snapshot capture function. Neither the memory snapshot of the start time nor the memory snapshot of the end time includes memory usage information related to indirect function calls.
[0136] In one embodiment, the first processing module 901 is specifically used for: The two memory snapshots are written to a local file using the write function in the file operation class.
[0137] In one embodiment, for each memory snapshot at a target time point, the second processing module 602 is specifically used for: Determine multiple function call sequences in a memory snapshot; The function name of each function is determined based on the function address of each function in each function call sequence in multiple function call sequences; Based on the function names of each function and the calling relationships between functions in each function call sequence, determine the symbolic sequence corresponding to each function call sequence; Based on the symbolic sequence corresponding to each function call sequence in multiple function call sequences, the function call tree corresponding to the memory snapshot is determined.
[0138] In one embodiment, the second processing module 902 is specifically used for: Based on the function addresses of each function in each function call sequence in multiple function call sequences, the function names of each function are obtained through an address dictionary; The address dictionary is obtained in the following way: Obtain a set of function address samples, which includes at least one function address sample; Based on each function address sample, the function is obtained through symbol information, and the function name corresponding to each function address sample is obtained; An address dictionary is obtained based on the set of function address samples and the function name corresponding to each function address sample.
[0139] In one embodiment, for each memory snapshot at a target time point, the second processing module 602 is specifically used for: Based on the symbolic sequence corresponding to each function call sequence in multiple function call sequences, determine the function call tree corresponding to the memory snapshot, the total number of calls to each function in multiple function call sequences, and the memory usage information corresponding to the calls to each function in multiple function call sequences.
[0140] In one embodiment, the third processing module 903 is specifically used for: Based on the function call trees corresponding to the two memory snapshots, the total number of calls to each function in multiple function call sequences, and the memory usage information corresponding to the calls to each function in multiple function call sequences, relevant data on memory leaks during the target application's operation during the target memory detection period are determined.
[0141] In one embodiment, the third processing module 903 is specifically used for: Based on the function call tree corresponding to the memory snapshot at the start time, the total number of calls to each function in multiple function call sequences, and the memory usage information corresponding to the calls to each function in multiple function call sequences, determine the memory usage of each function in the function call tree corresponding to the memory snapshot at the start time. Based on the function call tree corresponding to the memory snapshot at the end time point, the total number of calls to each function in multiple function call sequences, and the memory usage information corresponding to the calls to each function in multiple function call sequences, determine the memory usage of each function in the function call tree corresponding to the memory snapshot at the end time point. If the memory usage of each function in the function call tree corresponding to the memory snapshot at the end time point is greater than the memory usage of each function in the function call tree corresponding to the memory snapshot at the start time point, then each function is identified as a memory leak item in the target application during the target memory detection period.
[0142] Applying the embodiments of this disclosure has at least the following beneficial effects: The system acquires memory snapshots of the target application at two target time points during the target memory detection period and stores these snapshots in a local file. The two target time points include the start and end times of the target memory detection period. Based on the two memory snapshots in the local file, the system determines the function call tree corresponding to each snapshot. Based on the function call trees corresponding to the two memory snapshots, the system identifies relevant data about memory leaks during the target application's operation during the target memory detection period. By writing the memory snapshots to a local file, the system reduces the memory footprint of the snapshots, preventing memory crashes caused by excessive memory usage and thus improving the efficiency of memory leak detection.
[0143] This disclosure also provides an electronic device, the structural schematic diagram of which is shown below. Figure 10 As shown, Figure 10 The illustrated electronic device 4000 includes a processor 4001 and a memory 4003. The processor 4001 and the memory 4003 are connected, for example, via a bus 4002. Optionally, the electronic device 4000 may further include a transceiver 4004, which can be used for data interaction between the electronic device and other electronic devices, such as sending and / or receiving data. It should be noted that in practical applications, the transceiver 4004 is not limited to one type, and the structure of the electronic device 4000 does not constitute a limitation on the embodiments of this disclosure.
[0144] Processor 4001 may be a CPU (Central Processing Unit), a general-purpose processor, a DSP (Digital Signal Processor), an ASIC (Application Specific Integrated Circuit), an FPGA (Field Programmable Gate Array), or other programmable logic devices, transistor logic devices, hardware components, or any combination thereof. It may implement or execute the various exemplary logic blocks, modules, and circuits described in conjunction with this disclosure. Processor 4001 may also be a combination that implements computational functions, such as including one or more microprocessor combinations, a combination of a DSP and a microprocessor, etc.
[0145] Bus 4002 may include a pathway for transmitting information between the aforementioned components. Bus 4002 may be a PCI (Peripheral Component Interconnect) bus or an EISA (Extended Industry Standard Architecture) bus, etc. Bus 4002 can be divided into address bus, data bus, control bus, etc. For ease of representation, Figure 10 The bus is represented by a single thick line, but this does not mean that there is only one bus or one type of bus.
[0146] The memory 4003 may be ROM (Read Only Memory) or other types of static storage devices capable of storing static information and instructions, RAM (Random Access Memory) or other types of dynamic storage devices capable of storing information and instructions, or EEPROM (Electrically Erasable Programmable Read Only Memory), CD-ROM (Compact Disc Read Only Memory) or other optical disc storage, optical disc storage (including compressed optical discs, laser discs, optical discs, digital universal optical discs, Blu-ray discs, etc.), magnetic disk storage media, other magnetic storage devices, or any other medium capable of carrying or storing computer programs and capable of being read by a computer, without limitation herein.
[0147] The memory 4003 is used to store computer programs that execute embodiments of the present disclosure, and is controlled by the processor 4001 to execute them. The processor 4001 is used to execute the computer programs stored in the memory 4003 to implement the steps shown in the foregoing method embodiments.
[0148] Electronic devices include, but are not limited to, servers.
[0149] Applying the embodiments of this disclosure has at least the following beneficial effects: The system acquires memory snapshots of the target application at two target time points during the target memory detection period and stores these snapshots in a local file. The two target time points include the start and end times of the target memory detection period. Based on the two memory snapshots in the local file, the system determines the function call tree corresponding to each snapshot. Based on the function call trees corresponding to the two memory snapshots, the system identifies relevant data about memory leaks during the target application's operation during the target memory detection period. By writing the memory snapshots to a local file, the system reduces the memory footprint of the snapshots, preventing memory crashes caused by excessive memory usage and thus improving the efficiency of memory leak detection.
[0150] This disclosure provides a computer-readable storage medium storing a computer program, which, when executed by a processor, can implement the steps and corresponding content of the aforementioned method embodiments.
[0151] This disclosure also provides a computer program product, including a computer program that, when executed by a processor, can implement the steps and corresponding content of the aforementioned method embodiments.
[0152] It should be understood that although arrows indicate various operation steps in the flowcharts of the embodiments of this disclosure, the order in which these steps are implemented is not limited to the order indicated by the arrows. Unless explicitly stated herein, in some implementation scenarios of the embodiments of this disclosure, the implementation steps in each flowchart can be executed in other orders as required. Furthermore, some or all of the steps in each flowchart may include multiple sub-steps or multiple stages based on the actual implementation scenario. Some or all of these sub-steps or stages can be executed at the same time, and each sub-step or stage can also be executed at different times. In scenarios where execution times differ, the execution order of these sub-steps or stages can be flexibly configured as required, and the embodiments of this disclosure do not limit this.
[0153] The above description is only an optional implementation method for some implementation scenarios of this disclosure. It should be noted that for those skilled in the art, other similar implementation methods based on the technical concept of this disclosure, without departing from the technical concept of this disclosure, also fall within the protection scope of the embodiments of this disclosure.
Claims
1. A method for detecting memory leaks, characterized in that, include: Obtain memory snapshots of the target application at two target time points during the target memory detection period, and store the two memory snapshots in a local file; wherein, the two target time points include the start time point and the end time point of the target memory detection period; Based on the two memory snapshots in the local file, determine the function call tree corresponding to each of the two memory snapshots; Based on the function call trees corresponding to the two memory snapshots, relevant data on memory leaks during the target application's operation in the target memory detection period are determined.
2. The method according to claim 1, characterized in that, The acquisition of memory snapshots of the target application at two target time points during the target memory detection period includes: If the preset engine dynamic library is loaded into memory, then the memory interface function set is obtained from the engine dynamic library, and the memory interface function set includes a memory snapshot capture function; The memory snapshot capture function obtains a memory snapshot of the target application at the start time and a memory snapshot of the end time.
3. The method according to claim 2, characterized in that, The step of obtaining the memory snapshots at the start time and the end time for the target application through the memory snapshot truncation function includes: Based on a preset memory dump flag, the memory snapshots at the start time and end time of the target application are obtained through the memory snapshot capture function. Neither the memory snapshot at the start time nor the memory snapshot at the end time includes memory usage information associated with indirect function calls.
4. The method according to claim 1, characterized in that, The step of storing the two acquired memory snapshots to a local file includes: The two memory snapshots are written to a local file using the write function in the file operation class.
5. The method according to claim 1, characterized in that, For each memory snapshot at the target time point, the function call tree corresponding to the memory snapshot is determined in the following way: Identify multiple function call sequences in the memory snapshot; Based on the function addresses of each function corresponding to each function call sequence in the plurality of function call sequences, the function names of each function are determined; Based on the function names of each function and the calling relationships between each function in each function call sequence, determine the symbolic sequence corresponding to each function call sequence; Based on the symbolic sequence corresponding to each function call sequence in the plurality of function call sequences, the function call tree corresponding to the memory snapshot is determined.
6. The method according to claim 5, characterized in that, The step of determining the function name of each function based on the function address of each function corresponding to each function in the plurality of function call sequences includes: Based on the function addresses of each function corresponding to each function call sequence in the plurality of function call sequences, the function names of each function are obtained through an address dictionary; The address dictionary is obtained in the following way: Obtain a set of function address samples, wherein the set of function address samples includes at least one function address sample; Based on each function address sample, the function is obtained through symbol information, and the function name corresponding to each function address sample is obtained; The address dictionary is obtained based on the set of function address samples and the function name corresponding to each function address sample.
7. The method according to claim 5, characterized in that, For each memory snapshot at the target time point, determining the function call tree corresponding to the memory snapshot based on the symbolic sequence corresponding to each function call sequence in the plurality of function call sequences includes: Based on the symbolic sequence corresponding to each function call sequence in the plurality of function call sequences, the function call tree corresponding to the memory snapshot, the total number of calls to each function in the plurality of function call sequences, and the memory usage information corresponding to the calls to each function in the plurality of function call sequences are determined.
8. The method according to claim 7, characterized in that, The determination of relevant data regarding memory leaks during the target application's execution in the target memory detection period, based on the function call trees corresponding to the two memory snapshots, includes: Based on the function call trees corresponding to the two memory snapshots, the total number of calls to each function in the multiple function call sequences, and the memory usage information corresponding to the calls to each function in the multiple function call sequences, relevant data on memory leaks during the target application's operation in the target memory detection period are determined.
9. The method according to claim 8, characterized in that, Based on the function call trees corresponding to the two memory snapshots, the total number of function calls in multiple function call sequences, and the memory usage information corresponding to the function calls in the multiple function call sequences, the relevant data on memory leaks during the target application's operation in the target memory detection period are determined, including: Based on the function call tree corresponding to the memory snapshot at the starting time point, the total number of calls to each function in multiple function call sequences, and the memory usage information corresponding to the calls to each function in the multiple function call sequences, the memory usage of each function in the function call tree corresponding to the memory snapshot at the starting time point is determined. Based on the function call tree corresponding to the memory snapshot at the end time point, the total number of calls to each function in the multiple function call sequences, and the memory usage information corresponding to the calls to each function in the multiple function call sequences, the memory usage of each function in the function call tree corresponding to the memory snapshot at the end time point is determined. If the memory usage of each function in the function call tree corresponding to the memory snapshot at the end time point is greater than the memory usage of each function in the function call tree corresponding to the memory snapshot at the start time point, then each function is identified as a memory leak item in the target application during the target memory detection period.
10. A memory leak detection device, characterized in that, include: The first processing module is used to acquire memory snapshots of the target application at two target time points during the target memory detection period, and store the two acquired memory snapshots in a local file; wherein, the two target time points include the start time point and the end time point of the target memory detection period; The second processing module is used to determine the function call tree corresponding to each of the two memory snapshots in the local file. The third processing module is used to determine, based on the function call trees corresponding to the two memory snapshots, relevant data on memory leaks during the target application's operation in the target memory detection period.
11. An electronic device comprising a memory, a processor, and a computer program stored in the memory, characterized in that, The processor executes the computer program to implement the steps of the method according to any one of claims 1-9.
12. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the steps of the method according to any one of claims 1-9.
13. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by a processor, it implements the steps of the method according to any one of claims 1-9.