Memory leak detection method, electronic device and computer readable storage medium
By recording the mapping relationship between memory addresses and call stacks using a detection library, generating and analyzing log files, the problem of low efficiency and poor compatibility in existing memory leak detection technologies is solved, achieving efficient memory leak localization across languages.
Patent Information
- Application Number
- CN202111614917.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-27
- Publication Date
- 2025-12-12
- Estimated Expiration
- 2041-12-27
AI Technical Summary
Existing memory leak detection methods are inefficient and the compiler tools have poor compatibility with different programming languages, making it difficult to efficiently detect the location of memory leaks.
The detection library records the mapping relationship between memory addresses and the corresponding call stacks of application modules, generates a log file, and analyzes these mapping relationships after a preset period to locate memory leaks. It is applicable to various programming languages.
It improves the adaptability and efficiency of memory leak detection, and can efficiently parse the location of memory leaks in different programming language environments.
Smart Images

Figure CN114546795B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of computer, in particular to a memory leak detection method, an electronic device and a computer readable storage medium. BACKGROUND
[0002] Memory leak refers to that in the process of application module calling memory running, the memory address of the applied memory is lost and cannot be released, or the memory that should be released is always held by the application module and the system memory tends to be full due to continuous application of memory by the application module. The existing memory leak detection method usually needs to repeatedly debug with the help of a compilation tool to find the location of memory leak. This method is low in efficiency and different compilation tools have poor compatibility for various types of programming languages. Therefore, how to improve the adaptability and efficiency of detecting memory leak becomes a problem to be solved. SUMMARY
[0003] The technical problem solved by the present application is to provide a memory leak detection method, an electronic device and a computer readable storage medium, which can improve the adaptability and efficiency of detecting memory leak.
[0004] To solve the above technical problem, the first aspect of the present application provides a memory leak detection method, comprising: in response to receiving a memory application instruction of an application module, detecting a library to obtain corresponding memory from a memory management library based on the memory application instruction and returning a memory address corresponding to the memory to the application module, recording a mapping relationship between the memory address and a call stack corresponding to the application module to obtain record data; in response to receiving a memory release instruction of an application module, the detection library acquires a memory address specified by the application module based on the memory release instruction, determines the memory released by the application module based on the memory address, and deletes the mapping relationship between the memory address corresponding to the released memory and the call stack corresponding to the application module from the current record data; in response to a preset period, the detection library generates a current record file based on the record data at the current time, so that a statistical unit obtains the current record file and determines the location of memory leak based on the current record file and a historical record file before the current record file.
[0005] To solve the above technical problems, the second aspect of the present application provides a memory leak detection method, comprising: in response to an application module sending a memory application instruction to a detection library, causing the detection library to obtain corresponding memory from a memory management library based on the memory application instruction and return the memory address corresponding to the memory to the application module, recording the mapping relationship between the memory address and the call stack corresponding to the application module to obtain record data; in response to the application module sending a memory release instruction to the detection library, causing the detection library to obtain the memory address specified by the application module based on the memory release instruction, determining the memory released by the application module based on the memory address, and deleting the mapping relationship between the memory address corresponding to the released memory and the call stack corresponding to the application module from the current record data; in response to a predetermined period of time, the application module sends a file acquisition instruction to the detection library, causing the detection library to generate a current record file based on the record data at the current time, and then a statistics unit obtains the current record file and determines the location of memory leakage based on the current record file and a historical record file before the current record file.
[0006] To solve the above technical problems, the third aspect of the present application provides a memory leak detection method, comprising: obtaining a current record file sent by an application module and extracting a historical record file before the current record file; wherein the current record file is obtained based on the method of the first aspect or the second aspect; in response to the current record file being greater than the last adjacent historical record file, extracting a call stack set composed of call stacks in at least one record file, and determining the number of times the call stacks in the call stack set appear in all the record files; arranging all the record files in chronological order, obtaining the call stacks with increasing number of appearances in the record files arranged in chronological order as target call stacks, and parsing the memory leakage position corresponding to the application module from the target call stacks.
[0007] To solve the above technical problems, the fourth aspect of the present application provides an electronic device, comprising: a memory and a processor coupled to each other, wherein the memory stores program data, and the processor invokes the program data to execute the method of the first aspect or the second aspect or the third aspect.
[0008] To solve the above technical problems, the fifth aspect of the present application provides a computer readable storage medium having program data stored thereon, wherein the program data is executed by a processor to implement the method of the first aspect or the second aspect or the third aspect.
[0009] The above scheme, if the detection library receives the memory application instruction of the application module, the detection library obtains the corresponding memory from the memory management library and returns the memory address corresponding to the memory to the application module, and records the mapping relationship between the memory address and the call stack corresponding to the application module to obtain the record data, if the detection library receives the memory release instruction of the application module, the detection library obtains the memory released by the application module, deletes the mapping relationship between the memory address corresponding to the released memory and the call stack corresponding to the application module from the current record data, so that the mapping relationship between the memory address corresponding to the memory not released by the application module and the call stack is always recorded in the record data at the current time, when a preset period is passed, the detection library generates a current record file based on the record data at the current time, and then the statistical unit obtains the current record file, and based on the change of the current record file and the historical record file before the current record file, the location of the memory leak is found. Therefore, no matter which type of programming language the application module is running on, after recording the mapping relationship between the memory address and the call stack corresponding to the application module and outputting the record file at a fixed time, the statistical unit can efficiently analyze the location of the memory leak, improving the adaptability and efficiency of detecting the memory leak. BRIEF DESCRIPTION OF DRAWINGS
[0010] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings needed in the embodiment description will be briefly introduced. Obviously, the drawings in the following description are only some embodiments of the present application, and other drawings can be obtained by those skilled in the art without creative labor. Among them:
[0011] Figure 1 is a flowchart of an embodiment of the memory leak detection method of the present application;
[0012] Figure 2 is a topological diagram of an embodiment of the memory leak detection method of the present application;
[0013] Figure 3 is a flowchart of another embodiment of the memory leak detection method of the present application;
[0014] Figure 4 is a flowchart of another embodiment of the memory leak detection method of the present application;
[0015] Figure 5 is a flowchart of another embodiment of the memory leak detection method of the present application;
[0016] Figure 6 is a structural diagram of an embodiment of the electronic device of the present application;
[0017] Figure 7is a structural schematic diagram of an embodiment of a computer storage medium of the present application. DETAILED DESCRIPTION
[0018] The technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, rather than all the embodiments. Based on the embodiments in the present application, all other embodiments obtained by those skilled in the art without creative efforts belong to the scope of protection of the present application.
[0019] The terms "system" and "network" are often used interchangeably herein. The term "and / or" herein is only used to describe the associated relationship of associated objects, which means that there can be three relationships, for example, A and / or B, which means that there are three cases of A alone, A and B together, and B alone. In addition, the character " / " herein generally represents an "or" relationship between the front and rear associated objects. In addition, "multiple" herein means two or more than two.
[0020] Please refer to Figure 1 , Figure 1 is a flowchart of an embodiment of a memory leak detection method of the present application, which takes a detection library set between an application module and a memory management library as an execution end, and includes:
[0021] S101: In response to receiving a memory application instruction of the application module, the detection library obtains corresponding memory from the memory management library based on the memory application instruction and returns the memory address corresponding to the memory to the application module, records the mapping relationship between the memory address and the call stack corresponding to the application module to obtain record data.
[0022] Specifically, in response to receiving a memory application instruction of the application module, the detection library obtains corresponding memory from the memory management library based on the memory application instruction and returns the memory address corresponding to the memory to the application module, obtains the mapping relationship between the memory address corresponding to the memory and the call stack corresponding to the application module and records it to obtain record data.
[0023] Optionally, before obtaining the mapping relationship between the memory address corresponding to the memory and the call stack corresponding to the application module, it further includes: judging whether the size of the memory applied by the application module exceeds a preset threshold, if yes, obtaining the mapping relationship between the memory address corresponding to the memory and the call stack corresponding to the application module and recording it to obtain record data, otherwise, not recording, so as to filter smaller memory occupation and reduce storage pressure and the memory called during storage.
[0024] Further, the detection library corresponds to a detection state, the detection state is by default in an unopened state when the application module starts running, and the detection state is opened after the application module is initialized and an interaction relationship with the detection module is established. The detection library records the detection state, and then determines whether to record the mapping relationship between the memory address corresponding to the memory and the call stack corresponding to the application module based on the detection state. The call stack corresponds to the application module, and the call stack corresponding to the application module remains unchanged when the application module runs between threads. When the detection state is opened and the memory application instruction of the application module is received, step S101 is entered.
[0025] In an application mode, please refer to Figure 2 , Figure 2 is a topological schematic diagram of an embodiment of the memory leak detection method in the application. When the detection state is opened, the application module sends a memory application instruction to the replacement module, the replacement module receives the memory application instruction, and applies for memory for the application module from the memory management library through a self-defined function, and feeds back the memory address corresponding to the memory applied from the memory management library to the application module. The process of applying for memory by the application module corresponds to the process of applying for memory returned to the application module in Figure 2 When the size of the memory applied by the application module exceeds a preset threshold, the replacement module applies for memory from the memory management library to enable the recording module to store the mapping relationship by using the memory applied by the replacement module, so as to obtain recording data. The process of applying for memory by the replacement module corresponds to the process of applying for memory returned to the replacement module in Figure 2 .
[0026] In a specific application scenario, the recording module applies for memory of 1024 bytes at least for recording memory when the detection library is compiled, and the preset threshold is set to 1024 bytes, so that only the call stack with the memory applied by the application module greater than 1024 bytes is collected in the recording data, thereby reducing the recording of the small memory application process and reducing the influence on the program performance. It can be understood that in other application scenarios, the preset threshold can also be set to other values, and the application does not make specific limitations on this.
[0027] S102: In response to receiving the memory release instruction of the application module, the detection library obtains the memory address specified by the application module based on the memory release instruction, determines the memory released by the application module based on the memory address, and deletes the mapping relationship between the memory address corresponding to the released memory and the call stack corresponding to the application module from the current recording data.
[0028] Specifically, the memory release instruction of the application module is received, the memory address specified by the application model is determined based on the memory release instruction by the library, the memory corresponding to the memory address is released from the memory management library, if the memory address corresponding to the released memory is recorded in the record data, the mapping relationship between the memory address corresponding to the released memory and the call stack corresponding to the application module is deleted from the current record data, if the memory address corresponding to the released memory is not recorded in the record data, it indicates that the memory size corresponding to the released memory is less than the preset threshold, and no processing is performed on the record data, so that the memory address corresponding to the memory not released and greater than the preset threshold at the current time is recorded in the record data.
[0029] In an application mode, please refer to Figure 2 , the detection library includes a replacement module and a recording module, when the detection state is started, the application module sends a memory release instruction to the replacement module, the replacement module releases the corresponding memory from the memory management library through the self-defined function after receiving the memory release instruction, the replacement module obtains the memory address corresponding to the released memory, and when the memory address corresponding to the released memory is recorded in the record data, the replacement module sends a deletion instruction to the recording module, so as to delete the mapping relationship between the memory address corresponding to the released memory and the call stack from the record data.
[0030] S103: In response to a preset period, the detection library generates a current record file based on the record data at the current time, so that the statistical unit obtains the current record file and determines the location of the memory leak based on the current record file and the historical record file before the current record file.
[0031] Specifically, the application module sends a file acquisition instruction to the detection library after a preset period, the detection library generates a current record file based on the record data at the current time, and then the statistical unit analyzes the location of the memory leak by using the current record file and the historical record file received before the current record file after obtaining the current record file. Wherein, the statistical unit can analyze the location of the memory leak based on the size of the record file and the number of times of the call stack appearing in the record file after obtaining multiple record files corresponding to the preset period in time sequence, and the content of data analysis of the statistical unit will be further described in the subsequent embodiments.
[0032] In an application mode, please refer to Figure 2 , the detection library includes a replacement module and a recording module, when a preset period is passed, the application module sends a file acquisition instruction to the replacement module, the replacement module sends an instruction to collect the call stack at the current time to the recording module, the recording module generates a current record file from the record data at the current time and saves it in the file system, and then the statistical unit obtains the current record file from the specified position in the file system.
[0033] The above scheme, if the detection library receives the memory application instruction of the application module, the detection library obtains the corresponding memory from the memory management library and returns the memory address corresponding to the memory to the application module, and records the mapping relationship between the memory address and the call stack corresponding to the application module to obtain the record data, if the detection library receives the memory release instruction of the application module, the detection library obtains the memory released by the application module, deletes the mapping relationship between the memory address corresponding to the released memory and the call stack corresponding to the application module from the current record data, so that the mapping relationship between the memory address corresponding to the memory not released by the application module and the call stack is always recorded in the record data at the current time, and when a preset period is passed, the detection library generates a current record file based on the record data at the current time, and then the statistical unit obtains the current record file, and based on the change of the current record file and the historical record file before the current record file, the location of the memory leak is found. Therefore, no matter which type of programming language the application module runs, after recording the mapping relationship between the memory address and the call stack corresponding to the application module and outputting the record file at a fixed time, the statistical unit can efficiently analyze the location of the memory leak, improving the adaptability and efficiency of detecting the memory leak.
[0034] Please refer to Figure 3 , Figure 3 is a flowchart of another embodiment of the memory leak detection method of the present application, which takes the detection library set between the application module and the memory management library as the execution end, and includes:
[0035] S301: in response to receiving the memory application instruction of the application module, the detection library is set to need to directly call the memory management library, and the corresponding memory is applied from the memory management library based on the memory application instruction, and the memory address corresponding to the applied memory is returned to the application module.
[0036] Specifically, the detection library corresponds to a detection state, when the application module is initially executed in the thread, the application module sends a loading instruction to the detection library, at this time the detection state is not started.
[0037] Further, before the step of setting the detection library to need to directly call the memory management library, applying the corresponding memory from the memory management library based on the memory application instruction, and returning the memory address corresponding to the applied memory to the application module, the detection library further includes: the detection library replaces the management interface of the memory management library with a self-defined interface, so that the detection library applies or releases the memory from the memory management library by using the self-defined interface.
[0038] Further, please refer to Figure 2application module, the replacement module calls the interface defined by the custom function, and then the detection library applies for memory or releases memory from the memory management library by using the custom interface, the detection library is used as a relay station between the application module and the memory detection library, and then it is convenient for the detection library to record the mapping relationship between the call stack corresponding to the application module and the memory. The memory management library includes but is not limited to ptmalloc2 of glibc, tcmalloc of google.
[0039] Further, the detection state is not started by default in the initial stage, when the application module sends a memory application instruction, the replacement module first judges whether the application module needs to directly call the memory management library, the initial value of the call corresponding to the application module is that the application module does not need to directly call the memory management library, if not, the detection state is started and step S301 is entered, if yes, the memory management library is directly called, and a memory application result is returned.
[0040] Specifically, when the detection state has been started and the memory application instruction of the application module is received, the detection library sets the corresponding thread local variable to need to directly call the memory management library, so as to apply for storage memory from the memory management library when data needs to be recorded. After the memory application instruction is obtained, the detection library obtains the corresponding memory from the memory management library by using the custom function, and returns the memory address corresponding to the memory to the application module.
[0041] S302: It is judged whether the size of the memory applied by the application module exceeds a preset threshold.
[0042] Specifically, the detection library judges whether the size of the memory applied by the application module exceeds a preset threshold, if yes, step S303 is entered, otherwise, step S304 is entered.
[0043] S303: In response to the size of the memory applied by the application module exceeding the preset threshold, storage memory is applied from the memory management library, so as to record the mapping relationship between the memory address corresponding to the memory applied by the application module and the call stack corresponding to the application module in the storage memory, and obtain the record data.
[0044] Specifically, when the size of the memory applied by the application module exceeds the preset threshold, the detection library applies storage memory from the memory management library, and then records the mapping relationship between the memory address corresponding to the memory applied by the application module and the call stack corresponding to the application module in the storage memory, and obtains the record data.
[0045] In an application scenario, please refer to Figure 2The detection library includes a replacement module and a record module. When the detection state is started and a memory application instruction of the application module is received, the replacement module sets a thread local variable corresponding to the detection library to need to directly call the memory management library. The replacement module obtains corresponding memory from the memory management library through a self-defined function and returns the memory to the application module. Meanwhile, the replacement module judges whether the size of the memory applied by the application module exceeds a preset threshold. When the size of the memory applied by the application module exceeds the preset threshold, the replacement module applies storage memory from the memory management library. The record module saves a mapping relationship between the memory address corresponding to the memory applied by the application module and the call stack corresponding to the application module in the storage memory.
[0046] S304: setting the detection library to not need to directly call the memory management library.
[0047] Specifically, after the application module obtains corresponding memory, the detection library is set to not need to directly call the memory management library. Thus, by setting whether the detection library needs to directly call the memory management library, storage memory is directly applied from the memory management library when the mapping relationship needs to be recorded, and no matter whether the mapping relationship is recorded or not, a dead loop is not caused.
[0048] S305: in response to receiving a memory release instruction of the application module, setting the detection library to need to directly call the memory management library, determining a memory address specified by the application module based on the memory release instruction, and releasing the memory corresponding to the memory address specified by the application module in the memory management library.
[0049] Specifically, when the memory release instruction of the application module is received, the detection library sets a corresponding thread local variable to need to directly call the memory management library, so as to obtain storage memory from the memory management library when data needs to be deleted. After the memory release instruction is obtained, the detection library determines the memory address specified by the application module to need to be released through a self-defined function, and releases the memory corresponding to the memory address from the memory management library.
[0050] S306: judging whether the released memory is recorded in the storage memory.
[0051] Specifically, the detection library judges whether the memory released by the application module is recorded in the storage memory. If yes, step S307 is entered, otherwise, step S308 is entered.
[0052] S307: in response to the memory address corresponding to the released memory being recorded in the storage memory, deleting the mapping relationship between the memory address corresponding to the released memory and the call stack corresponding to the application module, to obtain updated record data.
[0053] Specifically, when the memory address corresponding to the released memory is recorded in the storage memory, the mapping relationship between the memory address corresponding to the released memory and the call stack corresponding to the application module is deleted from the storage memory, the corresponding storage memory is released, and updated record data is obtained.
[0054] In an application scenario, referring again to Figure 2 , the detection library includes a replacement module and a record module, when the detection state has been started and the memory release instruction of the application module is received, the replacement module sets the thread local variable corresponding to the detection library to need to directly call the memory management library, the replacement module releases the corresponding memory from the memory management library through the self-defined function, and at the same time, the replacement module judges whether the memory released by the application module is recorded in the storage memory, when the memory address corresponding to the released memory is recorded in the storage memory, the replacement module sends the memory address corresponding to the released memory to the record module, the record module deletes the mapping relationship between the memory address corresponding to the released memory and the call stack corresponding to the application module from the storage memory, releases the corresponding storage memory, and obtains updated record data.
[0055] S308: Set the detection library to not need to directly call the memory management library.
[0056] Specifically, after the application module releases the corresponding memory, the detection library is set to not need to directly call the memory management library, so that by setting whether the detection library needs to directly call the memory management library, the memory address corresponding to the released memory is directly obtained from the memory management library when deleting the mapping relationship, and no matter whether the mapping relationship is deleted or not, the detection library will not fall into a dead loop.
[0057] S309: In response to a preset period, the detection library generates a current record file based on the record data at the current time, so that the statistical unit obtains the current record file and determines the location of the memory leak based on the current record file and the historical record file before the current record file.
[0058] Specifically, the detection library sets the compilation option, so that the file acquisition instruction is automatically triggered at a preset period when the detection library runs, and then the application module receives the record file generated at a preset period without modifying the code.
[0059] In an application mode, in response to the detection library triggering the file acquisition instruction at a preset period, the current record file corresponding to the current time is generated based on the mapping relationship between the call stack and the memory address corresponding to all application modules collected by the detection library at the current time.
[0060] Specifically, the detection library triggers a file acquisition instruction every preset period, generates a current record file corresponding to the current time from the record data recorded by the detection library and not yet deleted at the current time, thereby ensuring that the statistical unit can obtain record files at preset intervals every preset period, and can determine whether memory leakage occurs according to the size relationship of the record files in time sequence, and obtain the location of the memory leakage by analyzing the call stack.
[0061] In the embodiment, by setting whether the detection library needs to directly call the memory management library, the detection library does not fall into a dead loop when recording the mapping relationship or deleting the mapping relationship, and the mapping relationship between the memory addresses of the memory not released by the application module and the call stack is recorded in the record data at the current time. When a preset period elapses, the detection library generates a current record file based on the record data at the current time, so that the statistical unit can analyze the location of the memory leakage.
[0062] Please refer to Figure 4 , Figure 4 is a flowchart of another embodiment of the memory leakage detection method of the application. The method takes an application module as an execution end and includes the following steps.
[0063] S401: In response to the application module sending a memory application instruction to the detection library, the detection library obtains corresponding memory from the memory management library based on the memory application instruction and returns the memory address corresponding to the memory to the application module, records the mapping relationship between the memory address and the call stack corresponding to the application module to obtain record data.
[0064] Specifically, when the application module sends a memory application instruction to the detection library, the detection library obtains corresponding memory from the memory management library based on the memory application instruction and returns the memory address corresponding to the memory to the application module, records the mapping relationship between the memory address and the call stack corresponding to the application module to obtain record data.
[0065] Optionally, it is determined whether the size of the memory applied for by the application module exceeds a preset threshold. If yes, the mapping relationship between the memory address corresponding to the memory and the call stack corresponding to the application module is obtained and recorded to obtain record data.
[0066] S402: In response to the application module sending a memory release instruction to the detection library, the detection library obtains the memory address specified by the application module based on the memory release instruction, determines the memory released by the application module based on the memory address, and deletes the mapping relationship between the memory address corresponding to the released memory and the call stack corresponding to the application module from the current record data.
[0067] Specifically, when the application module sends a memory release instruction to the detection library, the detection library determines the memory address specified by the application model based on the memory release instruction and releases the memory corresponding to the memory address from the memory management library. If the memory address corresponding to the released memory is recorded in the record data, the mapping relationship between the memory address corresponding to the released memory and the call stack corresponding to the application module is deleted from the current record data. If the memory address corresponding to the released memory is not recorded in the record data, it means that the size of the memory corresponding to the released memory is less than a preset threshold. No processing is done on the record data, thus maintaining the record data at the current moment recording the memory address corresponding to the memory that has not been released and is greater than the preset threshold.
[0068] S403: In response to a preset period, the application module sends a file retrieval instruction to the detection library, so that the detection library generates the current record file based on the record data at the current moment. Then, the statistics unit obtains the current record file and determines the location of the memory leak based on the current record file and the historical record files before the current record file.
[0069] Specifically, after running a preset cycle, the application module sends a file retrieval command to the detection library. This allows the detection library to package the current recording data into a current record file based on the command. Then, the statistics unit, after obtaining the current record file, analyzes the location of the memory leak using the current record file and previously received historical record files. Specifically, after obtaining record files corresponding to multiple preset cycles with sequential timelines, the statistics unit can deduce the location of the memory leak based on the size of the record files and the number of times the call stack appears in the record files.
[0070] Furthermore, the detection library has a corresponding detection state. The detection state is not enabled by default and remains enabled once enabled. The steps to enable the detection state include: the application module sends a loading instruction to the detection library so that the detection library replaces the management interface of the memory management library with a custom interface after receiving the loading instruction; in response to the detection state not being enabled and the application module not needing to directly call the memory management library, the detection state is enabled so that the detection library saves the detection state.
[0071] Specifically, the loading of the detection library can be done by implicitly linking the detection library during compilation or by specifying the detection library using the highest priority environment variable. Both methods achieve the goal of automatically loading the detection library when the application module runs. After receiving the loading instruction, the detection library replaces the management interface of the memory management library with a custom interface. When the application module requests memory, it calls the interface defined by the custom function of the replacement module. Then, the detection library uses the custom interface to request or release memory from the memory management library. The detection library acts as a relay station between the application module and the memory detection library, which facilitates the detection library in recording the mapping relationship between the call stack and memory of the application module.
[0072] Further, the detection state is not turned on by default in the initial stage. When the application module sends a memory application instruction, the replacement module first judges whether the application module needs to directly call the memory management library. The initial value of the application module corresponding to the call is not needed. If the detection state is not turned on and the application module does not need to directly call the memory management library, the application module turns on the detection state, so that the detection library saves the detection state. If the application module needs to directly call the memory management library, the memory in the memory management library is directly called and the memory application result is returned, so as to ensure that the application module can apply for memory in two types of memory application modes.
[0073] In a specific application scenario, it is found that the process occupies the memory and grows during the running of the C / C++ program. Therefore, the detection library is loaded in an implicit linking manner, and the detection is started before the position where the memory leak may exist in the application program code. The record file is output once every 30 minutes, and the call stack of the memory leak is analyzed by the statistical unit.
[0074] In another specific application scenario, it is found that the JAVA process occupies the memory and grows during the running of the JAVA program. Therefore, the detection library is loaded in an LD_PRELOAD manner, and the detection is started before the position where the memory leak may exist in the code. The record file is output once every 30 minutes, and the call stack of the memory leak is analyzed by the statistical tool.
[0075] It can be understood that when the program corresponding to other types of programming languages runs, the statistical and analysis of the record file can also be realized by loading the detection library, and the position of the memory leak can be obtained.
[0076] The above scheme, when the detection state is started and memory needs to be applied, the application module sends a memory application instruction to the detection library to make the detection library obtain corresponding memory from the memory management library and return the memory address corresponding to the memory to the application module, and when the memory size applied by the application module exceeds the preset threshold, the mapping relationship between the memory address and the call stack corresponding to the application module is recorded to obtain record data, when the memory needs to be released, the application module sends a memory release instruction to the detection library to make the detection library obtain the memory address corresponding to the released memory of the application module, and delete the mapping relationship between the memory address corresponding to the released memory and the call stack corresponding to the application module from the current record data, so that the mapping relationship between the memory address corresponding to the memory not released by the application module and the call stack is always recorded in the record data at the current time. When a preset period is passed, the application module sends a file acquisition instruction to the detection library to make the detection library generate a current record file based on the record data at the current time, and then the statistical unit obtains the current record file and the change of the historical record file before the current record file to find the location of the memory leak. Therefore, no matter which type of programming language the application module runs, after recording the mapping relationship between the memory address and the call stack corresponding to the application module and outputting the record file at a fixed time, the statistical unit can efficiently analyze the location of the memory leak, improving the adaptability and efficiency of detecting memory leak.
[0077] Please refer to Figure 5 , Figure 5 is a flowchart of another embodiment of the memory leak detection method of the present application. The method takes the statistical unit as the execution end and includes:
[0078] S501: Obtain the current record file sent by the application module, and extract the historical record file before the current record file.
[0079] Specifically, the current record file is obtained based on the method described in any of the above embodiments. The statistical unit receives the record file every interval of the preset period, keeps the record file in the total period, until the time length of all saved record files has exceeded the total period, deletes the historical record file with the earliest time sequence, and receives the current record file sent by the application module, thereby reducing the load of the statistical unit.
[0080] S502: In response to the current record file being greater than the last adjacent historical record file, extract the call stack set composed of at least one call stack in the record file, and determine the number of times of occurrence of the call stack in the call stack set in all record files.
[0081] Specifically, if the current record file is larger than the last adjacent historical record file, the call stack set composed of the call stacks in the at least one record file is extracted, wherein the call stacks in the call stack set are different from each other and each corresponds to a different application module, and the number of occurrences of the call stacks in the call stack set in all record files is counted.
[0082] In an application mode, all different call stacks in the current record file are composed into a call stack set, and the number of occurrences of the call stacks in the call stack set in the respective corresponding record files is determined in all record files.
[0083] Specifically, the size of the current record file increases compared to the size of the last adjacent historical record file, which indicates that the application module corresponding to the call stack in the current record file may have a memory leak. All call stacks in the current record file are extracted and de-duplicated to obtain a call stack set composed of different call stacks, and the number of occurrences of each call stack in the call stack set in all record files is counted.
[0084] In another application mode, all different call stacks in the largest record file are composed into a call stack set, and the number of occurrences of the call stacks in the call stack set in the respective corresponding record files is determined in all record files.
[0085] Specifically, the largest record file records the most mapping relationships among all record files in the total period, and the call stacks recorded in the largest record file have a higher probability of being more comprehensive. All call stacks in the largest record file are extracted and de-duplicated to obtain a call stack set composed of different call stacks, and the number of occurrences of each call stack in the call stack set in all record files is counted.
[0086] In yet another application mode, all different call stacks in the current record file and the largest record file are composed into a call stack set, and the number of occurrences of the call stacks in the call stack set in the respective corresponding record files is determined in all record files.
[0087] It can be understood that a more comprehensive call stack set is obtained by extracting and de-duplicating the call stacks in the current record file and the largest record file, and the number of occurrences of each call stack in the call stack set in all record files is counted.
[0088] S503: Arranging all record files in chronological order, obtaining the call stack with increasing number of occurrences in the record files arranged in chronological order as a target call stack, and parsing the memory leak position of the application module corresponding to the target call stack.
[0089] Specifically, all the record files are arranged in time sequence, and then the call stacks with increasing occurrence times are searched in the record files arranged in time sequence, that is, the call stacks with non-decreasing occurrence times are taken as target call stacks, and the function names and code lines are parsed from the target call stacks by using the parsing rule, so as to locate the memory leak position of the application module corresponding to the call stacks.
[0090] In an application mode, all the record files are arranged in time sequence, and the occurrence times of the same call stacks in adjacent record files are deduplicated; the call stacks with continuously increasing occurrence times are taken as target call stacks, and the function names and code lines corresponding to the application module sending the memory application instruction are parsed, so as to obtain the memory leak position of the application module.
[0091] Specifically, by deduplicating the occurrence times of the same call stacks in adjacent record files, the trend of the occurrence times of the call stacks is more accurately judged, the call stacks with continuously increasing occurrence times are taken as target call stacks, and the function names and code lines corresponding to the target call stacks are parsed by using the ELF viewing tool, so as to accurately locate the memory leak position of the application module corresponding to the call stacks.
[0092] In an application scenario, the statistical unit includes a finding module, a statistical module and a parsing module, the statistical module judges whether the size of the current record file is greater than that of the last adjacent historical record file, if yes, the finding module finds the largest record file in all the record files, and if not, waits for the record file after the next preset period.
[0093] Further, the address query module sorts the call stacks in the largest record file and removes the duplicate call stacks to obtain a call stack set, and sends the call stack set to the statistical module, the statistical module first sorts all the record files in time sequence, determines the occurrence times of the call stacks in the call stack set in all the record files, deduplicates the occurrence times of the same call stacks in adjacent record files, filters out the call stacks with increasing occurrence times as target call stacks and sends them to the parsing module, and the parsing module parses the function names and code lines corresponding to the target call stacks by using the ELF viewing tool.
[0094] In a specific application scenario, the program memory without symbols continues to grow, and the detection library can still be used to collect record files, and the statistical tool can be used to analyze the call stacks corresponding to the growing memory, at this time, the library file before the symbols are removed or the library with symbols reserved by recompiling the same source code is needed to be found, and the function names and code lines are restored by the ELF viewing tool based on the call stacks analyzed by the statistical tool and the symbol-bearing library compiled from the same source code.
[0095] The scheme records the mapping relationship between the memory address and the call stack corresponding to the application module, and outputs the record file at a regular time. Therefore, the statistical unit can efficiently analyze the location of the memory leak after obtaining the current record file, and the adaptability and efficiency of detecting the memory leak are improved.
[0096] Please refer to Figure 6 , Figure 6 is a structural schematic diagram of an embodiment of an electronic device of the present application. The electronic device 60 comprises a memory 601 and a processor 602 coupled with each other. The memory 601 stores program data (not shown in the figure). The processor 602 invokes the program data to implement the memory leak detection method in any of the above embodiments. For related content, please refer to the detailed description of the method embodiments. Here, no further elaboration is given.
[0097] Please refer to Figure 7 , Figure 7 is a structural schematic diagram of an embodiment of a computer storage medium of the present application. The computer storage medium 70 stores program data 700. The program data 700 is executed by a processor to implement the memory leak detection method in any of the above embodiments. For related content, please refer to the detailed description of the method embodiments. Here, no further elaboration is given.
[0098] The above is only an embodiment of the present application, and does not limit the patent scope of the present application. Any equivalent structure or equivalent flow transformation using the content of the specification and drawings, or direct or indirect application in other related technical fields, is also included in the patent protection scope of the present application.
Claims
1. A memory leak detection method, characterized by, The method comprises: In response to receiving a memory application instruction of an application module, a detection library obtains corresponding memory from a memory management library based on the memory application instruction and returns a memory address corresponding to the memory to the application module, and a mapping relationship between the memory address and a call stack corresponding to the application module is recorded to obtain record data; the record data is obtained based on the following steps: the detection library is set to need to directly call the memory management library, corresponding memory is applied from the memory management library based on the memory application instruction, and a memory address corresponding to the applied memory is returned to the application module, and then the detection library is set to not need to directly call the memory management library; before the step of setting the detection library to not need to directly call the memory management library, the following further comprises: in response to the size of the memory applied by the application module exceeding a preset threshold, storage memory is applied from the memory management library to record a mapping relationship between a memory address corresponding to the memory applied by the application module and a call stack corresponding to the application module in the storage memory, and the record data is obtained; In response to receiving a memory release instruction of an application module, the detection library obtains a memory address specified by the application module based on the memory release instruction, determines memory released by the application module based on the memory address, and deletes a mapping relationship between a memory address corresponding to the released memory and a call stack corresponding to the application module from current record data; In response to a preset period being elapsed, the detection library generates a current record file based on record data at a current time, so that a statistical unit obtains the current record file and determines a location of memory leakage based on the current record file and a historical record file before the current record file.
2. The memory leak detection method of claim 1, wherein, The step of obtaining a memory address specified by the application module based on the memory release instruction, determining memory released by the application module based on the memory address, and deleting a mapping relationship between a memory address corresponding to the released memory and a call stack corresponding to the application module from current record data comprises: The detection library is set to need to directly call the memory management library, a memory address specified by the application module is determined based on a memory release instruction, memory corresponding to the memory address specified by the application module is released in the memory management library, and then the detection library is set to not need to directly call the memory management library; Before the step of setting the detection library to not need to directly call the memory management library, the following further comprises: In response to a memory address corresponding to the released memory being recorded in the storage memory, a mapping relationship between the memory address corresponding to the released memory and a call stack corresponding to the application module is deleted to obtain updated record data.
3. The memory leak detection method of claim 1, wherein, Before the step of setting the detection library to need to directly call the memory management library, corresponding memory is applied from the memory management library based on the memory application instruction, and a memory address corresponding to the applied memory is returned to the application module, the following further comprises: The detection library replaces the management interface of the memory management library with a custom interface, so that the detection library applies for memory or releases memory from the memory management library using the custom interface.
4. The memory leak detection method of claim 1, wherein, The step of generating a current record file based on the record data of the current time in response to the detection library passing through the preset period, comprises: In response to the detection library triggering a file acquisition instruction after passing through the preset period, the detection library generates the current record file corresponding to the current time based on the mapping relationship between the call stack corresponding to all the application modules collected by the detection library at the current time and the memory address.
5. A memory leak detection method, characterized by, The method comprises: In response to the application module sending a memory application instruction to the detection library, the detection library obtains the corresponding memory from the memory management library based on the memory application instruction and returns the memory address corresponding to the memory to the application module, and records the mapping relationship between the memory address and the call stack corresponding to the application module to obtain record data; the record data is obtained based on the following steps: setting the detection library to directly call the memory management library, applying for the corresponding memory from the memory management library based on the memory application instruction, and returning the memory address corresponding to the applied memory to the application module, and then setting the detection library to not directly call the memory management library; before the step of setting the detection library to not directly call the memory management library, it further comprises: in response to the size of the memory applied by the application module exceeding a preset threshold, applying for storage memory from the memory management library to record the mapping relationship between the memory address corresponding to the memory applied by the application module and the call stack corresponding to the application module in the storage memory, and obtain the record data; In response to the application module sending a memory release instruction to the detection library, the detection library obtains the memory address specified by the application module based on the memory release instruction, determines the memory released by the application module based on the memory address, and deletes the mapping relationship between the memory address corresponding to the released memory and the call stack corresponding to the application module from the current record data; In response to passing through a preset period, the application module sends a file acquisition instruction to the detection library, so that the detection library generates a current record file based on the record data of the current time, and then the statistical unit obtains the current record file and determines the location of memory leakage based on the current record file and the historical record file before the current record file.
6. The memory leak detection method of claim 5, wherein, The detection library corresponds to a detection state, which is not started by default and remains in the started state after being started, and the step of starting the detection state comprises: The application module sends a loading instruction to the detection library, so that the detection library replaces the management interface of the memory management library with a custom interface after obtaining the loading instruction; In response to the detection state not being started and the application module not directly calling the memory management library, the detection state is started to make the detection library save the detection state.
7. A memory leak detection method, characterized by, The method comprises: obtain a current record file sent by an application module, and extract a history record file before the current record file, wherein the current record file is obtained based on the method in any one of claims 1-6; in response to the current record file being greater than a last adjacent history record file, extract a call stack set composed of call stacks in at least one record file, and determine the number of times of occurrence of the call stacks in the call stack set in all the record files; arrange all the record files in time sequence, obtain a call stack with an increased number of times of occurrence in the record files arranged in time sequence as a target call stack, and parse a memory leak position of the application module from the target call stack.
8. The memory leak detection method of claim 7, wherein, The step of extracting a call stack set composed of call stacks in at least one record file and determining the number of times of occurrence of the call stacks in the call stack set in all the record files comprises: composing all the different call stacks in the current record file and / or the largest record file into the call stack set; determining the number of times of occurrence of the call stacks in the call stack set in the respective record files.
9. The memory leak detection method of claim 8, wherein, The step of arranging all the record files in time sequence, obtaining a call stack with an increased number of times of occurrence in the record files arranged in time sequence as a target call stack, and parsing a memory leak position of the application module from the target call stack comprises: arranging all the record files in time sequence, and removing the number of times of occurrence of the same call stack in adjacent record files; obtaining a call stack with a continuously increasing number of times of occurrence as the target call stack, parsing the function name and code line number corresponding to the application module sending a memory application instruction from the target call stack, and obtaining the memory leak position of the application module.
10. An electronic device, comprising: comprise: a memory and a processor coupled to each other, wherein the memory stores program data, and the processor invokes the program data to execute the method in any one of claims 1-9.
11. A computer readable storage medium having stored thereon program data, wherein, The program data is executed by the processor to implement the method in any one of claims 1-9.
Citation Information
Patent Citations
Memory leak positioning method and electronic equipment
CN106407031A
Method and apparatus for determining memory leakage position
CN106933733A